diff --git a/Assets/FlashTools/Scripts/FlashAnim.cs b/Assets/FlashTools/Scripts/FlashAnim.cs index 28636da..1d17bef 100644 --- a/Assets/FlashTools/Scripts/FlashAnim.cs +++ b/Assets/FlashTools/Scripts/FlashAnim.cs @@ -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 _vertices = new List(); - List _triangles = new List(); - List _uvs = new List(); - - Mesh _mesh = null; - Vector3[] _vertices_arr = new Vector3[0]; - int[] _triangles_arr = new int[0]; - Vector2[] _uvs_arr = new Vector2[0]; + List _uvs = new List(); + List _vertices = new List(); + List _triangles = new List(); 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().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().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().mesh = _mesh; + var mesh_filter = GetComponent(); + 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; } } } diff --git a/Assets/FlashTools/Scripts/FlashAnimAsset.cs b/Assets/FlashTools/Scripts/FlashAnimAsset.cs index f133957..3dba9cd 100644 --- a/Assets/FlashTools/Scripts/FlashAnimAsset.cs +++ b/Assets/FlashTools/Scripts/FlashAnimAsset.cs @@ -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 Strings = new List(); } 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; } } diff --git a/Assets/FlashTools/Scripts/Internal/Editor/FlashAnimAssetEditor.cs b/Assets/FlashTools/Scripts/Internal/Editor/FlashAnimAssetEditor.cs index 0f538e8..5516b79 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/FlashAnimAssetEditor.cs +++ b/Assets/FlashTools/Scripts/Internal/Editor/FlashAnimAssetEditor.cs @@ -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(); + 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().sharedMesh = null; + anim_go.AddComponent().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(); - flash_anim.Asset = _asset; - - var mesh_filter = anim_go.AddComponent(); - mesh_filter.mesh = null; - - var material = new Material(Shader.Find("Sprites/Default")); - material.SetTexture("_MainTex", _asset.Data.Atlas); - - var mesh_renderer = anim_go.AddComponent(); - 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(); } } } diff --git a/Assets/FlashTools/Scripts/Internal/Editor/FlashAnimAssetPostprocessor.cs b/Assets/FlashTools/Scripts/Internal/Editor/FlashAnimAssetPostprocessor.cs index 4cefc6c..095d3c0 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/FlashAnimAssetPostprocessor.cs +++ b/Assets/FlashTools/Scripts/Internal/Editor/FlashAnimAssetPostprocessor.cs @@ -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(atlas_path); + fa_asset.Atlas = AssetDatabase.LoadAssetAtPath(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(); + 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]); } } } diff --git a/unityflash.userprefs b/unityflash.userprefs index 82d3bca..9096586 100644 --- a/unityflash.userprefs +++ b/unityflash.userprefs @@ -2,13 +2,14 @@ - - - + + - + + + \ No newline at end of file