From 64a08b57da3caa05eba978268b565ce88301649f Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Mon, 22 Aug 2016 01:06:34 +0700 Subject: [PATCH] default convert settings and little refactor --- Assembly-CSharp-Editor.csproj | 6 +- Assembly-CSharp.csproj | 1 + .../FlashTools/Examples/Scenes/Scene00.unity | 2 +- .../Resources/SwfConverterSettings.asset | 21 +++ .../Resources/SwfConverterSettings.asset.meta | 8 ++ .../Editor/SwfAnimationAssetEditor.cs | 125 +++++++++++++----- .../Editor/SwfAnimationAssetPostprocessor.cs | 42 +++--- .../Internal/Editor/SwfPostprocessor.cs | 9 +- .../SwfTools/SwfTags/RemoveObjectTag.cs | 2 +- Assets/FlashTools/Scripts/SwfAnimation.cs | 6 +- .../FlashTools/Scripts/SwfAnimationAsset.cs | 77 +++++------ .../Scripts/SwfConverterSettings.cs | 64 +++++++++ .../Scripts/SwfConverterSettings.cs.meta | 12 ++ 13 files changed, 273 insertions(+), 102 deletions(-) create mode 100644 Assets/FlashTools/Resources/SwfConverterSettings.asset create mode 100644 Assets/FlashTools/Resources/SwfConverterSettings.asset.meta create mode 100644 Assets/FlashTools/Scripts/SwfConverterSettings.cs create mode 100644 Assets/FlashTools/Scripts/SwfConverterSettings.cs.meta diff --git a/Assembly-CSharp-Editor.csproj b/Assembly-CSharp-Editor.csproj index 0c9b45c..2b34368 100644 --- a/Assembly-CSharp-Editor.csproj +++ b/Assembly-CSharp-Editor.csproj @@ -115,6 +115,9 @@ /Applications/Unity/Unity.app/Contents/UnityExtensions/Unity/TreeEditor/Editor/UnityEditor.TreeEditor.dll + + /Users/matov/Programming/Projects/unityflash/Assets/FlashTools/Plugins/Ionic.Zip.Unity.dll + /Applications/Unity/Unity.app/Contents/Frameworks/Managed/UnityEditor.Graphs.dll @@ -145,9 +148,6 @@ /Applications/Unity/PlaybackEngines/MacStandaloneSupport/UnityEditor.OSXStandalone.Extensions.dll - - /Users/matov/Programming/Projects/unityflash/Assets/FlashTools/Plugins/Ionic.Zip.Unity.dll - /Applications/Unity/PlaybackEngines/AppleTVSupport/UnityEditor.iOS.Extensions.Xcode.dll diff --git a/Assembly-CSharp.csproj b/Assembly-CSharp.csproj index ed9362f..b170016 100644 --- a/Assembly-CSharp.csproj +++ b/Assembly-CSharp.csproj @@ -47,6 +47,7 @@ + diff --git a/Assets/FlashTools/Examples/Scenes/Scene00.unity b/Assets/FlashTools/Examples/Scenes/Scene00.unity index fc2b6d8..c4e037f 100644 --- a/Assets/FlashTools/Examples/Scenes/Scene00.unity +++ b/Assets/FlashTools/Examples/Scenes/Scene00.unity @@ -145,7 +145,7 @@ Camera: far clip plane: 1000 field of view: 60 orthographic: 1 - orthographic size: 6 + orthographic size: 2.37 m_Depth: 0 m_CullingMask: serializedVersion: 2 diff --git a/Assets/FlashTools/Resources/SwfConverterSettings.asset b/Assets/FlashTools/Resources/SwfConverterSettings.asset new file mode 100644 index 0000000..76174a4 --- /dev/null +++ b/Assets/FlashTools/Resources/SwfConverterSettings.asset @@ -0,0 +1,21 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 6da591d87f967451298a275621e1fd5d, type: 3} + m_Name: SwfConverterSettings + m_EditorClassIdentifier: + DefaultSettings: + MaxAtlasSize: 1024 + AtlasPadding: 1 + PixelsPerUnit: 100 + AtlasPowerOfTwo: 0 + GenerateMipMaps: 1 + AtlasFilterMode: 1 + AtlasImporterFormat: -3 diff --git a/Assets/FlashTools/Resources/SwfConverterSettings.asset.meta b/Assets/FlashTools/Resources/SwfConverterSettings.asset.meta new file mode 100644 index 0000000..32b9594 --- /dev/null +++ b/Assets/FlashTools/Resources/SwfConverterSettings.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: cda4fb87dc8624946bdaed871b60f0b8 +timeCreated: 1471787769 +licenseType: Free +NativeFormatImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetEditor.cs b/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetEditor.cs index 76334a5..d0fed91 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetEditor.cs +++ b/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetEditor.cs @@ -9,38 +9,109 @@ namespace FlashTools.Internal { public class SwfAnimationAssetEditor : Editor { SwfAnimationAsset _asset = null; - static void ApplySettings(SwfAnimationAsset asset) { - if ( asset.Atlas ) { + void Reconvert() { + if ( _asset && _asset.Atlas ) { AssetDatabase.DeleteAsset( - AssetDatabase.GetAssetPath(asset.Atlas)); - asset.Atlas = null; + AssetDatabase.GetAssetPath(_asset.Atlas)); + _asset.Atlas = null; + } + if ( !_asset.OverrideSettings ) { + _asset.OverriddenSettings = + SwfConverterSettings.LoadOrCreate().DefaultSettings; } - var swf_asset_path = Path.ChangeExtension( - AssetDatabase.GetAssetPath(asset), "swf"); AssetDatabase.ImportAsset( - swf_asset_path, - ImportAssetOptions.ForceUncompressedImport); + GetSwfPath(), + ImportAssetOptions.ForceUpdate); + } + + GameObject CreateAnimationGO() { + if ( _asset ) { + var anim_go = new GameObject(_asset.name); + anim_go.AddComponent(); + anim_go.AddComponent(); + anim_go.AddComponent().Asset = _asset; + return anim_go; + } + return null; } void CreateAnimationPrefab() { - //TODO: IMPLME + var anim_go = CreateAnimationGO(); + var prefab_path = GetPrefabPath(); + if ( anim_go && !string.IsNullOrEmpty(prefab_path) ) { + var prefab = AssetDatabase.LoadMainAssetAtPath(prefab_path); + if ( !prefab ) { + prefab = PrefabUtility.CreateEmptyPrefab(prefab_path); + } + PrefabUtility.ReplacePrefab( + anim_go, + prefab, + ReplacePrefabOptions.ConnectToPrefab); + Undo.RegisterCreatedObjectUndo(anim_go, "Create SwfAnimation"); + } } void CreateAnimationOnScene() { - if ( _asset ) { - var anim_go = new GameObject(_asset.name); - 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; + var anim_go = CreateAnimationGO(); + if ( anim_go ) { Undo.RegisterCreatedObjectUndo(anim_go, "Create SwfAnimation"); } } + string GetAssetPath() { + return _asset + ? AssetDatabase.GetAssetPath(_asset) + : string.Empty; + } + + string GetSwfPath() { + var asset_path = GetAssetPath(); + return string.IsNullOrEmpty(asset_path) + ? string.Empty + : Path.ChangeExtension(asset_path, ".swf"); + } + + string GetPrefabPath() { + var asset_path = GetAssetPath(); + return string.IsNullOrEmpty(asset_path) + ? string.Empty + : Path.ChangeExtension(asset_path, ".prefab"); + } + + void DrawGUIControls() { + if ( GUILayout.Button("Reconvert") ) { + Reconvert(); + } + GUILayout.BeginHorizontal(); + { + if ( GUILayout.Button("Create animation prefab") ) { + CreateAnimationPrefab(); + } + if ( GUILayout.Button("Create animation on scene") ) { + CreateAnimationOnScene(); + } + } + GUILayout.EndHorizontal(); + } + + void DrawGUIProperties() { + serializedObject.Update(); + EditorGUILayout.PropertyField(serializedObject.FindProperty("Atlas")); + EditorGUILayout.PropertyField(serializedObject.FindProperty("OverrideSettings")); + if ( _asset.OverrideSettings ) { + EditorGUILayout.PropertyField( + serializedObject.FindProperty("OverriddenSettings"), true); + } else { + GUI.enabled = false; + var so = new SerializedObject(SwfConverterSettings.LoadOrCreate()); + EditorGUILayout.PropertyField(so.FindProperty("DefaultSettings"), true); + GUI.enabled = true; + } + if ( GUI.changed ) { + serializedObject.ApplyModifiedProperties(); + } + } + // ------------------------------------------------------------------------ // // Messages @@ -52,18 +123,8 @@ namespace FlashTools.Internal { } public override void OnInspectorGUI() { - DrawDefaultInspector(); - if ( GUILayout.Button("Apply settings") ) { - ApplySettings(_asset); - } - GUILayout.BeginHorizontal(); - if ( GUILayout.Button("Create animation prefab") ) { - CreateAnimationPrefab(); - } - if ( GUILayout.Button("Create animation on scene") ) { - CreateAnimationOnScene(); - } - GUILayout.EndHorizontal(); + DrawGUIProperties(); + DrawGUIControls(); } } -} +} \ No newline at end of file diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetPostprocessor.cs b/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetPostprocessor.cs index e81e228..44680e5 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetPostprocessor.cs +++ b/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetPostprocessor.cs @@ -4,7 +4,6 @@ using System; using System.IO; using System.Linq; using System.Reflection; -using System.Collections.Generic; namespace FlashTools.Internal { public class SwfAnimationAssetPostprocessor : AssetPostprocessor { @@ -39,32 +38,31 @@ namespace FlashTools.Internal { } static void ConfigureAtlas(string asset_path, SwfAnimationAsset asset) { - var meta_data = new List(); var atlas_importer = GetBitmapsAtlasImporter(asset_path); var atlas_size = GetSizeFromTextureImporter(atlas_importer); - var unique_bitmaps = asset.Data.Bitmaps; - 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 = asset.PixelsPerUnit; - AssetDatabase.ImportAsset( - GetAtlasPath(asset_path), - ImportAssetOptions.ForceUncompressedImport); - } + atlas_importer.spritesheet = asset.Data.Bitmaps + .Select(bitmap => new SpriteMetaData{ + name = bitmap.Id.ToString(), + rect = new Rect( + bitmap.SourceRect.xMin * atlas_size.x, + bitmap.SourceRect.yMin * atlas_size.y, + bitmap.SourceRect.width * atlas_size.x, + bitmap.SourceRect.height * atlas_size.y)}) + .ToArray(); + atlas_importer.textureType = TextureImporterType.Sprite; + atlas_importer.spriteImportMode = SpriteImportMode.Multiple; + atlas_importer.spritePixelsPerUnit = asset.OverriddenSettings.PixelsPerUnit; + atlas_importer.mipmapEnabled = asset.OverriddenSettings.GenerateMipMaps; + atlas_importer.filterMode = asset.OverriddenSettings.AtlasFilterMode; + atlas_importer.textureFormat = asset.OverriddenSettings.AtlasImporterFormat; + AssetDatabase.ImportAsset( + GetAtlasPath(asset_path), + ImportAssetOptions.ForceUpdate); } static TextureImporter GetBitmapsAtlasImporter(string asset_path) { var atlas_path = GetAtlasPath(asset_path); - var importer = AssetImporter.GetAtPath(atlas_path) as TextureImporter; + var importer = AssetImporter.GetAtPath(atlas_path) as TextureImporter; if ( !importer ) { throw new UnityException(string.Format( "atlas texture importer not found ({0})", @@ -85,4 +83,4 @@ namespace FlashTools.Internal { return Path.ChangeExtension(asset_path, ".png"); } } -} +} \ No newline at end of file diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfPostprocessor.cs b/Assets/FlashTools/Scripts/Internal/Editor/SwfPostprocessor.cs index 272f3a5..60bb5d4 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/SwfPostprocessor.cs +++ b/Assets/FlashTools/Scripts/Internal/Editor/SwfPostprocessor.cs @@ -181,11 +181,14 @@ namespace FlashTools.Internal { .ToArray(); var textures = bitmap_defines - .Select(p => LoadTextureFromBitmapDefine(p.Value)); + .Select (p => LoadTextureFromBitmapDefine(p.Value)) + .ToArray(); var atlas = new Texture2D(0, 0, TextureFormat.ARGB32, false); var atlas_rects = atlas.PackTextures( - textures.ToArray(), asset.AtlasPadding, asset.MaxAtlasSize); + textures, + asset.OverriddenSettings.AtlasPadding, + asset.OverriddenSettings.MaxAtlasSize); File.WriteAllBytes( GetAtlasPath(swf_asset), @@ -193,7 +196,7 @@ namespace FlashTools.Internal { GameObject.DestroyImmediate(atlas, true); AssetDatabase.ImportAsset( GetAtlasPath(swf_asset), - ImportAssetOptions.ForceUncompressedImport); + ImportAssetOptions.ForceUpdate); var bitmaps = new List(); for ( var i = 0; i < bitmap_defines.Length; ++i ) { diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfTools/SwfTags/RemoveObjectTag.cs b/Assets/FlashTools/Scripts/Internal/Editor/SwfTools/SwfTags/RemoveObjectTag.cs index 1f08044..a1139de 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/SwfTools/SwfTags/RemoveObjectTag.cs +++ b/Assets/FlashTools/Scripts/Internal/Editor/SwfTools/SwfTags/RemoveObjectTag.cs @@ -25,4 +25,4 @@ return tag; } } -} +} \ No newline at end of file diff --git a/Assets/FlashTools/Scripts/SwfAnimation.cs b/Assets/FlashTools/Scripts/SwfAnimation.cs index d532eb7..78d28dc 100644 --- a/Assets/FlashTools/Scripts/SwfAnimation.cs +++ b/Assets/FlashTools/Scripts/SwfAnimation.cs @@ -110,9 +110,9 @@ namespace FlashTools { var matrix = Matrix4x4.Scale(new Vector3( - 1.0f / Asset.PixelsPerUnit, - -1.0f / Asset.PixelsPerUnit, - 1.0f / Asset.PixelsPerUnit)) * inst.Matrix; + 1.0f / Asset.OverriddenSettings.PixelsPerUnit, + -1.0f / Asset.OverriddenSettings.PixelsPerUnit, + 1.0f / Asset.OverriddenSettings.PixelsPerUnit)) * inst.Matrix; _vertices.Add(matrix.MultiplyPoint3x4(v0)); _vertices.Add(matrix.MultiplyPoint3x4(v1)); diff --git a/Assets/FlashTools/Scripts/SwfAnimationAsset.cs b/Assets/FlashTools/Scripts/SwfAnimationAsset.cs index da068b7..718488a 100644 --- a/Assets/FlashTools/Scripts/SwfAnimationAsset.cs +++ b/Assets/FlashTools/Scripts/SwfAnimationAsset.cs @@ -7,32 +7,28 @@ namespace FlashTools { public Vector4 Mul; public Vector4 Add; - public SwfAnimationColorTransform(Vector4 Mul, Vector4 Add) { - this.Mul = Mul; - this.Add = Add; - } - public static SwfAnimationColorTransform identity { get { - return new SwfAnimationColorTransform( - new Vector4(1,1,1,1), - new Vector4(0,0,0,0)); + return new SwfAnimationColorTransform{ + Mul = Vector4.one, + Add = Vector4.zero}; } } public static SwfAnimationColorTransform operator*( SwfAnimationColorTransform a, SwfAnimationColorTransform b) { - var res = new SwfAnimationColorTransform(); - res.Mul.x = b.Mul.x * a.Mul.x; - res.Mul.y = b.Mul.y * a.Mul.y; - res.Mul.z = b.Mul.z * a.Mul.z; - res.Mul.w = b.Mul.w * a.Mul.w; - res.Add.x = b.Add.x * a.Mul.x + a.Add.x; - res.Add.y = b.Add.y * a.Mul.y + a.Add.y; - res.Add.z = b.Add.z * a.Mul.z + a.Add.z; - res.Add.w = b.Add.w * a.Mul.w + a.Add.w; - return res; + return new SwfAnimationColorTransform{ + Mul = new Vector4( + b.Mul.x * a.Mul.x, + b.Mul.y * a.Mul.y, + b.Mul.z * a.Mul.z, + b.Mul.w * a.Mul.w), + Add = new Vector4( + b.Add.x * a.Mul.x + a.Add.x, + b.Add.y * a.Mul.y + a.Add.y, + b.Add.z * a.Mul.z + a.Add.z, + b.Add.w * a.Mul.w + a.Add.w)}; } } @@ -45,39 +41,46 @@ namespace FlashTools { [System.Serializable] public class SwfAnimationInstanceData { - public SwfAnimationInstanceType Type = SwfAnimationInstanceType.Group; - public ushort ClipDepth = 0; - public ushort Bitmap = 0; - public Matrix4x4 Matrix = Matrix4x4.identity; - public SwfAnimationColorTransform ColorTransform = SwfAnimationColorTransform.identity; + public SwfAnimationInstanceType Type = SwfAnimationInstanceType.Group; + public ushort ClipDepth = 0; + public ushort Bitmap = 0; + public Matrix4x4 Matrix = Matrix4x4.identity; + public SwfAnimationColorTransform ColorTransform = SwfAnimationColorTransform.identity; } [System.Serializable] public class SwfAnimationFrameData { - public string Name = string.Empty; - public List Instances = new List(); + public string Name = string.Empty; + public List Instances = new List(); } [System.Serializable] public class SwfAnimationBitmapData { - public int Id = 0; - public Vector2 RealSize = Vector2.zero; - public Rect SourceRect = new Rect(); + public int Id = 0; + public Vector2 RealSize = Vector2.zero; + public Rect SourceRect = new Rect(); } [System.Serializable] public class SwfAnimationData { - public float FrameRate = 0.0f; - public List Frames = new List(); - public List Bitmaps = new List(); + public float FrameRate = 0.0f; + public List Frames = new List(); + public List Bitmaps = new List(); } public class SwfAnimationAsset : ScriptableObject { - //[HideInInspector] - public SwfAnimationData Data = new SwfAnimationData(); - public Texture2D Atlas = null; - public int MaxAtlasSize = 1024; - public int AtlasPadding = 1; - public int PixelsPerUnit = 100; + public SwfAnimationData Data = new SwfAnimationData(); + public Texture2D Atlas = null; + public bool OverrideSettings = false; + public SwfConverterSettings.Settings OverriddenSettings = new SwfConverterSettings.Settings(); + + #if UNITY_EDITOR + void Reset() { + Data = new SwfAnimationData(); + Atlas = null; + OverrideSettings = false; + OverriddenSettings = SwfConverterSettings.LoadOrCreate().DefaultSettings; + } + #endif } } \ No newline at end of file diff --git a/Assets/FlashTools/Scripts/SwfConverterSettings.cs b/Assets/FlashTools/Scripts/SwfConverterSettings.cs new file mode 100644 index 0000000..35c77e9 --- /dev/null +++ b/Assets/FlashTools/Scripts/SwfConverterSettings.cs @@ -0,0 +1,64 @@ +using UnityEngine; + +#if UNITY_EDITOR +using System.IO; +using UnityEditor; +#endif + +namespace FlashTools { + public class SwfConverterSettings : ScriptableObject { + [System.Serializable] + public struct Settings { + public int MaxAtlasSize; + public int AtlasPadding; + public int PixelsPerUnit; + public bool AtlasPowerOfTwo; + public bool GenerateMipMaps; + public FilterMode AtlasFilterMode; + public TextureImporterFormat AtlasImporterFormat; + + public void Reset() { + MaxAtlasSize = 1024; + AtlasPadding = 1; + PixelsPerUnit = 100; + AtlasPowerOfTwo = false; + GenerateMipMaps = true; + AtlasFilterMode = FilterMode.Bilinear; + AtlasImporterFormat = TextureImporterFormat.AutomaticTruecolor; + } + } + public Settings DefaultSettings; + + #if UNITY_EDITOR + void Reset() { + DefaultSettings.Reset(); + } + + public static SwfConverterSettings LoadOrCreate() { + var settings_path = GetSettingsPath(); + var settings = AssetDatabase.LoadAssetAtPath(settings_path); + if ( !settings ) { + settings = ScriptableObject.CreateInstance(); + CreateAssetDatabaseFolders(Path.GetDirectoryName(settings_path)); + AssetDatabase.CreateAsset(settings, settings_path); + AssetDatabase.SaveAssets(); + } + return settings; + } + + static void CreateAssetDatabaseFolders(string path) { + if ( !AssetDatabase.IsValidFolder(path) ) { + var parent = Path.GetDirectoryName(path); + if ( !string.IsNullOrEmpty(parent) ) { + CreateAssetDatabaseFolders(parent); + } + AssetDatabase.CreateFolder(parent, Path.GetFileName(path)); + } + } + + static string GetSettingsPath() { + return "Assets/FlashTools/Resources/SwfConverterSettings.asset"; + } + #endif + } +} \ No newline at end of file diff --git a/Assets/FlashTools/Scripts/SwfConverterSettings.cs.meta b/Assets/FlashTools/Scripts/SwfConverterSettings.cs.meta new file mode 100644 index 0000000..5a416a3 --- /dev/null +++ b/Assets/FlashTools/Scripts/SwfConverterSettings.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 6da591d87f967451298a275621e1fd5d +timeCreated: 1471761453 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: