fix some postprocessor problems

This commit is contained in:
2016-02-27 20:18:00 +06:00
parent 684e392801
commit 864f23c1de
5 changed files with 158 additions and 71 deletions

View File

@@ -3,20 +3,16 @@ using System.Collections.Generic;
namespace FlashTools {
[ExecuteInEditMode]
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class FlashAnim : MonoBehaviour {
public FlashAnimAsset Asset = null;
int _current_frame = 0;
float _frame_timer = 0.0f;
int _current_frame = 0;
float _frame_timer = 0.0f;
List<Vector3> _vertices = new List<Vector3>();
List<int> _triangles = new List<int>();
List<Vector2> _uvs = new List<Vector2>();
Mesh _mesh = null;
Vector3[] _vertices_arr = new Vector3[0];
int[] _triangles_arr = new int[0];
Vector2[] _uvs_arr = new Vector2[0];
List<Vector2> _uvs = new List<Vector2>();
List<Vector3> _vertices = new List<Vector3>();
List<int> _triangles = new List<int>();
public void Play() {
}
@@ -137,6 +133,20 @@ namespace FlashTools {
}
}
// ------------------------------------------------------------------------
//
// Messages
//
// ------------------------------------------------------------------------
void Start() {
if ( Asset && Asset.Atlas ) {
var material = new Material(Shader.Find("Sprites/Default"));
material.SetTexture("_MainTex", Asset.Atlas);
GetComponent<MeshRenderer>().sharedMaterial = material;
}
}
void Update() {
_frame_timer += 25.0f * Time.deltaTime;
while ( _frame_timer > 1.0f ) {
@@ -157,7 +167,10 @@ namespace FlashTools {
RenderSymbol(
GetCurrentSymbol(),
_current_frame,
Matrix4x4.Scale(new Vector3(1,-1,1)));
Matrix4x4.Scale(new Vector3(
1.0f / Asset.PixelsPerUnit,
-1.0f / Asset.PixelsPerUnit,
1.0f / Asset.PixelsPerUnit)));
/*
if ( _vertices_arr.Length < _vertices.Count ) {
@@ -183,17 +196,17 @@ namespace FlashTools {
mesh.RecalculateNormals();
GetComponent<MeshFilter>().mesh = mesh;*/
if ( !_mesh ) {
_mesh = new Mesh();
}
if ( _mesh ) {
_mesh.Clear();
_mesh.SetVertices(_vertices);
_mesh.SetTriangles(_triangles, 0);
_mesh.SetUVs(0, _uvs);
_mesh.RecalculateNormals();
GetComponent<MeshFilter>().mesh = _mesh;
var mesh_filter = GetComponent<MeshFilter>();
if ( mesh_filter ) {
var mesh = mesh_filter.sharedMesh
? mesh_filter.sharedMesh
: new Mesh();
mesh.Clear();
mesh.SetVertices(_vertices);
mesh.SetTriangles(_triangles, 0);
mesh.SetUVs(0, _uvs);
mesh.RecalculateNormals();
mesh_filter.sharedMesh = mesh;
}
}
}

View File

@@ -103,15 +103,17 @@ namespace FlashTools {
[System.Serializable]
public class FlashAnimData {
public Texture2D Atlas = null;
public FlashAnimSymbolData Stage = new FlashAnimSymbolData();
public FlashAnimLibraryData Library = new FlashAnimLibraryData();
public List<string> Strings = new List<string>();
}
public class FlashAnimAsset : ScriptableObject {
public FlashAnimData Data = new FlashAnimData();
public int MaxAtlasSize = 1024;
public int AtlasPadding = 1;
[HideInInspector]
public FlashAnimData Data = new FlashAnimData();
public Texture2D Atlas = null;
public int MaxAtlasSize = 1024;
public int AtlasPadding = 1;
public int PixelsPerUnit = 100;
}
}

View File

@@ -2,45 +2,57 @@
using UnityEngine.Rendering;
using UnityEditor;
using System;
using System.IO;
namespace FlashTools.Internal {
[CustomEditor(typeof(FlashAnimAsset))]
public class FlashAnimAssetEditor : Editor {
FlashAnimAsset _asset = null;
void ApplySettings() {
static void ApplySettings(FlashAnimAsset asset) {
if ( asset.Atlas ) {
AssetDatabase.DeleteAsset(
AssetDatabase.GetAssetPath(asset.Atlas));
}
AssetDatabase.ImportAsset(
AssetDatabase.GetAssetPath(_asset),
ImportAssetOptions.ForceUpdate);
AssetDatabase.GetAssetPath(asset),
ImportAssetOptions.ForceUncompressedImport);
}
void CreateFlashAnimOnScene() {
static void CreateFlashAnim(FlashAnimAsset asset, GameObject anim_go) {
var mesh_renderer = anim_go.AddComponent<MeshRenderer>();
mesh_renderer.sharedMaterial = null;
mesh_renderer.useLightProbes = false;
mesh_renderer.receiveShadows = false;
mesh_renderer.shadowCastingMode = ShadowCastingMode.Off;
mesh_renderer.reflectionProbeUsage = ReflectionProbeUsage.Off;
anim_go.AddComponent<MeshFilter>().sharedMesh = null;
anim_go.AddComponent<FlashAnim>().Asset = asset;
}
// ------------------------------------------------------------------------
//
// Public
//
// ------------------------------------------------------------------------
public static void CreateFlashAnimPrefab(FlashAnimAsset asset) {
var prefab_path = Path.ChangeExtension(AssetDatabase.GetAssetPath(asset), ".prefab");
var flash_anim_go = CreateFlashAnimOnScene(asset);
PrefabUtility.CreatePrefab(prefab_path, flash_anim_go);
GameObject.DestroyImmediate(flash_anim_go, true);
}
public static GameObject CreateFlashAnimOnScene(FlashAnimAsset asset) {
var anim_go = new GameObject("FlashAnim");
try {
CreateFlashAnim(anim_go);
CreateFlashAnim(asset, anim_go);
} catch ( Exception e ) {
Debug.LogErrorFormat("Create animation error: {0}", e.Message);
DestroyImmediate(anim_go, true);
}
Undo.RegisterCreatedObjectUndo(anim_go, "Create Animation");
}
void CreateFlashAnim(GameObject anim_go) {
var flash_anim = anim_go.AddComponent<FlashAnim>();
flash_anim.Asset = _asset;
var mesh_filter = anim_go.AddComponent<MeshFilter>();
mesh_filter.mesh = null;
var material = new Material(Shader.Find("Sprites/Default"));
material.SetTexture("_MainTex", _asset.Data.Atlas);
var mesh_renderer = anim_go.AddComponent<MeshRenderer>();
mesh_renderer.sharedMaterial = material;
mesh_renderer.useLightProbes = false;
mesh_renderer.receiveShadows = false;
mesh_renderer.shadowCastingMode = ShadowCastingMode.Off;
mesh_renderer.reflectionProbeUsage = ReflectionProbeUsage.Off;
return anim_go;
}
// ------------------------------------------------------------------------
@@ -56,11 +68,16 @@ namespace FlashTools.Internal {
public override void OnInspectorGUI() {
DrawDefaultInspector();
if ( GUILayout.Button("Apply settings") ) {
ApplySettings();
}
if ( GUILayout.Button("Create animation on scene") ) {
CreateFlashAnimOnScene();
ApplySettings(_asset);
}
GUILayout.BeginHorizontal();
if ( GUILayout.Button("Create animation prefab") ) {
CreateFlashAnimPrefab(_asset);
}
if ( GUILayout.Button("Create animation on scene") ) {
CreateFlashAnimOnScene(_asset);
}
GUILayout.EndHorizontal();
}
}
}

View File

@@ -3,6 +3,8 @@ using UnityEditor;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
namespace FlashTools.Internal {
public class FlashAnimAssetPostprocessor : AssetPostprocessor {
@@ -22,14 +24,17 @@ namespace FlashTools.Internal {
static void FaAssetProcess(string fa_asset_path, FlashAnimAsset fa_asset) {
try {
if ( !MarkAllBitmapsReadable(fa_asset_path, fa_asset) ) {
AssetDatabase.ImportAsset(fa_asset_path, ImportAssetOptions.ForceUpdate);
return;
}
RemoveDuplicatedBitmaps(fa_asset_path, fa_asset);
if ( !PackBitmapsAtlas(fa_asset_path, fa_asset) ) {
AssetDatabase.ImportAsset(fa_asset_path, ImportAssetOptions.ForceUpdate);
return;
if ( !fa_asset.Atlas ) {
if ( !MarkAllBitmapsReadable(fa_asset_path, fa_asset) ) {
AssetDatabase.ImportAsset(fa_asset_path, ImportAssetOptions.ForceUncompressedImport);
return;
}
RemoveDuplicatedBitmaps(fa_asset_path, fa_asset);
if ( !PackBitmapsAtlas(fa_asset_path, fa_asset) ) {
AssetDatabase.ImportAsset(fa_asset_path, ImportAssetOptions.ForceUncompressedImport);
return;
}
ConfigureBitmapsAtlas(fa_asset_path, fa_asset);
}
} catch ( Exception e ) {
Debug.LogErrorFormat("Postprocess flash anim asset error: {0}", e.Message);
@@ -41,7 +46,9 @@ namespace FlashTools.Internal {
var readable = importer.isReadable;
if ( !readable ) {
importer.isReadable = true;
AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate);
AssetDatabase.ImportAsset(
importer.assetPath,
ImportAssetOptions.ForceUncompressedImport);
}
return readable && acc;
});
@@ -73,16 +80,44 @@ namespace FlashTools.Internal {
var atlas_rects = atlas.PackTextures(
textures.ToArray(), fa_asset.AtlasPadding, fa_asset.MaxAtlasSize);
File.WriteAllBytes(atlas_path, atlas.EncodeToPNG());
GameObject.DestroyImmediate(atlas);
AssetDatabase.ImportAsset(atlas_path, ImportAssetOptions.ForceUpdate);
GameObject.DestroyImmediate(atlas, true);
AssetDatabase.ImportAsset(
atlas_path,
ImportAssetOptions.ForceUncompressedImport);
for ( var i = 0; i < textures.Count; ++i ) {
var bitmap_data = fa_asset.Data.Library.Bitmaps[i];
bitmap_data.RealSize = new Vector2(textures[i].width, textures[i].height);
bitmap_data.SourceRect = atlas_rects[i];
}
fa_asset.Data.Atlas = AssetDatabase.LoadAssetAtPath<Texture2D>(atlas_path);
fa_asset.Atlas = AssetDatabase.LoadAssetAtPath<Texture2D>(atlas_path);
EditorUtility.SetDirty(fa_asset);
return fa_asset.Data.Atlas != null;
return fa_asset.Atlas != null;
}
static void ConfigureBitmapsAtlas(string fa_asset_path, FlashAnimAsset fa_asset) {
var meta_data = new List<SpriteMetaData>();
var atlas_importer = GetBitmapsAtlasImporter(fa_asset_path);
var atlas_size = GetSizeFromTextureImporter(atlas_importer);
var unique_bitmaps = fa_asset.Data.Library.Bitmaps
.GroupBy(p => p.ImageSource)
.Select(p => p.First());
foreach ( var bitmap_data in unique_bitmaps ) {
var meta_elem = new SpriteMetaData();
meta_elem.name = bitmap_data.Id.ToString();
meta_elem.rect = new Rect(
bitmap_data.SourceRect.xMin * atlas_size.x,
bitmap_data.SourceRect.yMin * atlas_size.y,
bitmap_data.SourceRect.width * atlas_size.x,
bitmap_data.SourceRect.height * atlas_size.y);
meta_data.Add(meta_elem);
atlas_importer.spritesheet = meta_data.ToArray();
atlas_importer.textureType = TextureImporterType.Sprite;
atlas_importer.spriteImportMode = SpriteImportMode.Multiple;
atlas_importer.spritePixelsPerUnit = fa_asset.PixelsPerUnit;
AssetDatabase.ImportAsset(
GetBitmapsAtlasPath(fa_asset_path),
ImportAssetOptions.ForceUncompressedImport);
}
}
// -----------------------------
@@ -174,7 +209,26 @@ namespace FlashTools.Internal {
}
static string GetBitmapsAtlasPath(string fa_asset_path) {
return Path.ChangeExtension(fa_asset_path, "atlas.png");
return Path.ChangeExtension(fa_asset_path, ".png");
}
static TextureImporter GetBitmapsAtlasImporter(string fa_asset_path) {
var atlas_path = GetBitmapsAtlasPath(fa_asset_path);
var importer = AssetImporter.GetAtPath(atlas_path) as TextureImporter;
if ( !importer ) {
throw new UnityException(string.Format(
"atlas texture importer not found ({0})",
atlas_path));
}
return importer;
}
static Vector2 GetSizeFromTextureImporter(TextureImporter importer) {
var method_args = new object[2]{0,0};
typeof(TextureImporter)
.GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance)
.Invoke(importer, method_args);
return new Vector2((int)method_args[0], (int)method_args[1]);
}
}
}

View File

@@ -2,13 +2,14 @@
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" PreferredExecutionTarget="Unity.Instance.Unity Editor" />
<MonoDevelop.Ide.Workbench ActiveDocument="Assets/FlashTools/Scripts/Internal/Editor/FlashAnimAssetPostprocessor.cs">
<Files>
<File FileName="Assets/FlashTools/Scripts/Internal/Editor/FlashAnimAssetPostprocessor.cs" Line="148" Column="44" />
<File FileName="Assets/FlashTools/Scripts/Internal/Editor/FlashAnimFtaPostprocessor.cs" Line="25" Column="5" />
<File FileName="Assets/FlashTools/Scripts/FlashAnimAsset.cs" Line="118" Column="1" NotebookId="1" />
<File FileName="Assets/FlashTools/Scripts/Internal/Editor/FlashAnimAssetPostprocessor.cs" Line="15" Column="1" />
<File FileName="Assets/FlashTools/Scripts/Internal/Editor/FlashAnimFtaPostprocessor.cs" Line="24" Column="36" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore />
<BreakpointStore>
<Breakpoint file="/Users/matov/Programming/Projects/unityflash/Assets/FlashTools/Scripts/Internal/Editor/FlashAnimFtaPostprocessor.cs" line="15" column="1" />
</BreakpointStore>
</MonoDevelop.Ide.DebuggingService.Breakpoints>
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
</Properties>