From 3f5e640b48e90b61ec65f120fe5de046b798fa8b Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Tue, 30 Aug 2016 16:34:18 +0700 Subject: [PATCH] + sequences --- .../Editor/SwfAnimationAssetEditor.cs | 8 +-- .../Editor/SwfAnimationAssetPostprocessor.cs | 23 ++++-- .../Internal/Editor/SwfAnimationEditor.cs | 56 ++++++++++++++- Assets/FlashTools/Scripts/SwfAnimation.cs | 72 ++++++++++++++----- .../FlashTools/Scripts/SwfAnimationAsset.cs | 20 +++--- 5 files changed, 139 insertions(+), 40 deletions(-) diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetEditor.cs b/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetEditor.cs index 587d9f8..afddeb4 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetEditor.cs +++ b/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetEditor.cs @@ -167,11 +167,11 @@ namespace FlashTools.Internal { var atlas_prop = SwfEditorUtils.GetPropertyByName(serializedObject, "Atlas"); EditorGUILayout.PropertyField(atlas_prop, true); - var frames_prop = SwfEditorUtils.GetPropertyByName(serializedObject, "Frames"); - if ( frames_prop.isArray ) { + var sequences_prop = SwfEditorUtils.GetPropertyByName(serializedObject, "Sequences"); + if ( sequences_prop.isArray ) { SwfEditorUtils.DoWithMixedValue( - frames_prop.hasMultipleDifferentValues, () => { - EditorGUILayout.IntField("Frame count", frames_prop.arraySize); + sequences_prop.hasMultipleDifferentValues, () => { + EditorGUILayout.IntField("Sequence count", sequences_prop.arraySize); }); } }); diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetPostprocessor.cs b/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetPostprocessor.cs index 678e2d5..a6ac8dc 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetPostprocessor.cs +++ b/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetPostprocessor.cs @@ -151,15 +151,22 @@ namespace FlashTools.Internal { static void ConfigureBakedFrames(string asset_path, SwfAnimationAsset asset) { RemoveAllSubAssets(asset_path); - var baked_frames = new List(); - if ( asset && asset.Atlas && asset.Data != null && asset.Data.Frames.Count > 0 ) { + var sequences = new List(); + if ( IsValidAssetForFrame(asset) ) { for ( var i = 0; i < asset.Data.Frames.Count; ++i ) { - var frame = asset.Data.Frames[i]; + var frame = asset.Data.Frames[i]; var baked_frame = BakeFrameFromAnimationFrame(asset, frame); - baked_frames.Add(baked_frame); + if ( !string.IsNullOrEmpty(frame.Name) && + (sequences.Count < 1 || sequences.Last().Name != frame.Name) ) + { + sequences.Add(new SwfAnimationAsset.Sequence{Name = frame.Name}); + } else if ( sequences.Count < 1 ) { + sequences.Add(new SwfAnimationAsset.Sequence{Name = "Default"}); + } + sequences.Last().Frames.Add(baked_frame); } } - asset.Frames = baked_frames; + asset.Sequences = sequences; } static void RemoveAllSubAssets(string asset_path) { @@ -172,6 +179,12 @@ namespace FlashTools.Internal { } } + static bool IsValidAssetForFrame(SwfAnimationAsset asset) { + return + asset && asset.Atlas && + asset.Data != null && asset.Data.Frames != null; + } + static SwfAnimationAsset.Frame BakeFrameFromAnimationFrame( SwfAnimationAsset asset, SwfAnimationFrameData frame) { diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationEditor.cs b/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationEditor.cs index be6aedb..ffd601e 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationEditor.cs +++ b/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationEditor.cs @@ -28,7 +28,8 @@ namespace FlashTools.Internal { string GetAnimationsFrameCountStr() { return _animations.Aggregate(string.Empty, (acc, anim) => { - var frame_count_str = anim.frameCount.ToString(); + var frame_count = anim.frameCount > 0 ? anim.frameCount - 1 : 0; + var frame_count_str = frame_count.ToString(); return string.IsNullOrEmpty(acc) ? frame_count_str : (acc != frame_count_str ? "--" : acc); @@ -37,13 +38,61 @@ namespace FlashTools.Internal { string GetAnimationsCurrentFrameStr() { return _animations.Aggregate(string.Empty, (acc, anim) => { - var current_frame_str = anim.currentFrame.ToString(); + var current_frame = anim.currentFrame; + var current_frame_str = current_frame.ToString(); return string.IsNullOrEmpty(acc) ? current_frame_str : (acc != current_frame_str ? "--" : acc); }); } + bool IsAllAnimationsHasOneAsset() { + foreach ( var animation in _animations ) { + if ( !animation.asset ) { + return false; + } + if ( animation.asset != _animations.First().asset ) { + return false; + } + } + return true; + } + + List GetAllSequences(bool include_empty) { + var seq_set = new HashSet(_animations + .Where(p => p.asset) + .SelectMany(p => p.asset.Sequences) + .Select(p => p.Name)); + if ( include_empty ) { + seq_set.Add(string.Empty); + } + return seq_set.ToList(); + } + + void DrawSequence() { + if ( IsAllAnimationsHasOneAsset() ) { + var sequence_prop = SwfEditorUtils.GetPropertyByName(serializedObject, "_sequence"); + SwfEditorUtils.DoWithMixedValue( + sequence_prop.hasMultipleDifferentValues, () => { + var all_sequences = GetAllSequences(true); + var sequence_index = EditorGUILayout.Popup( + "Sequence", + sequence_prop.hasMultipleDifferentValues + ? all_sequences.FindIndex(p => string.IsNullOrEmpty(p)) + : all_sequences.FindIndex(p => p == sequence_prop.stringValue), + all_sequences.ToArray()); + var new_sequence = all_sequences[sequence_index]; + if ( !string.IsNullOrEmpty(new_sequence) ) { + if ( sequence_prop.hasMultipleDifferentValues ) { + sequence_prop.stringValue = string.Empty; + } + sequence_prop.stringValue = new_sequence; + sequence_prop.serializedObject.ApplyModifiedProperties(); + } + }); + } + } + void DrawCurrentFrame() { var min_frame_count = GetMinAnimationsFrameCount(); if ( min_frame_count > 0 ) { @@ -51,7 +100,7 @@ namespace FlashTools.Internal { SwfEditorUtils.GetPropertyByName(serializedObject, "_currentFrame"), 0, min_frame_count - 1, - "Frame"); + "Current frame"); } } @@ -95,6 +144,7 @@ namespace FlashTools.Internal { public override void OnInspectorGUI() { serializedObject.Update(); DrawDefaultInspector(); + DrawSequence(); DrawCurrentFrame(); DrawAnimationControls(); if ( GUI.changed ) { diff --git a/Assets/FlashTools/Scripts/SwfAnimation.cs b/Assets/FlashTools/Scripts/SwfAnimation.cs index f22168c..145db01 100644 --- a/Assets/FlashTools/Scripts/SwfAnimation.cs +++ b/Assets/FlashTools/Scripts/SwfAnimation.cs @@ -6,9 +6,11 @@ namespace FlashTools { [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))] public class SwfAnimation : MonoBehaviour { - MeshFilter _meshFilter = null; - MeshRenderer _meshRenderer = null; - MaterialPropertyBlock _matPropBlock = null; + MeshFilter _meshFilter = null; + MeshRenderer _meshRenderer = null; + + SwfAnimationAsset.Sequence _curSequence = null; + MaterialPropertyBlock _curPropBlock = null; // --------------------------------------------------------------------- // @@ -48,6 +50,16 @@ namespace FlashTools { } } + [SerializeField][HideInInspector] + string _sequence = "Default"; + public string sequence { + get { return _sequence; } + set { + _sequence = value; + ChangeSequence(); + } + } + [SerializeField][HideInInspector] int _currentFrame = 0; public int currentFrame { @@ -60,8 +72,8 @@ namespace FlashTools { public int frameCount { get { - return asset && asset.Data != null && asset.Data.Frames != null - ? asset.Data.Frames.Count + return _curSequence != null && _curSequence.Frames != null + ? _curSequence.Frames.Count : 0; } } @@ -112,6 +124,7 @@ namespace FlashTools { public void UpdateAllProperties() { asset = _asset; + sequence = _sequence; currentFrame = _currentFrame; sortingLayer = _sortingLayer; sortingOrder = _sortingOrder; @@ -121,8 +134,31 @@ namespace FlashTools { if ( _meshRenderer ) { _meshRenderer.enabled = !!asset; } - UpdateMaterialPropertyBlock(); - UpdateCurrentMesh(); + UpdatePropertyBlock(); + ChangeSequence(); + } + + void ChangeSequence() { + _curSequence = null; + if ( asset && asset.Sequences != null ) { + for ( int i = 0, e = asset.Sequences.Count; i < e; ++i ) { + var asset_sequence = asset.Sequences[i]; + if ( asset_sequence != null && asset_sequence.Name == sequence ) { + _curSequence = asset_sequence; + } + } + if ( _curSequence == null ) { + for ( int i = 0, e = asset.Sequences.Count; i < e; ++i ) { + var asset_sequence = asset.Sequences[i]; + if ( asset_sequence != null ) { + _sequence = asset_sequence.Name; + _curSequence = asset_sequence; + break; + } + } + } + } + ChangeCurrentFrame(); } void ChangeCurrentFrame() { @@ -139,17 +175,17 @@ namespace FlashTools { } } - void UpdateMaterialPropertyBlock() { + void UpdatePropertyBlock() { if ( _meshRenderer ) { - if ( _matPropBlock == null ) { - _matPropBlock = new MaterialPropertyBlock(); + if ( _curPropBlock == null ) { + _curPropBlock = new MaterialPropertyBlock(); } - _meshRenderer.GetPropertyBlock(_matPropBlock); + _meshRenderer.GetPropertyBlock(_curPropBlock); var atlas = asset && asset.Atlas ? asset.Atlas : null; if ( atlas ) { - _matPropBlock.SetTexture("_MainTex", atlas); + _curPropBlock.SetTexture("_MainTex", atlas); } - _meshRenderer.SetPropertyBlock(_matPropBlock); + _meshRenderer.SetPropertyBlock(_curPropBlock); } } @@ -162,9 +198,10 @@ namespace FlashTools { } SwfAnimationAsset.Frame GetCurrentBakedFrame() { - return currentFrame >= 0 && currentFrame < frameCount - ? asset.Frames[currentFrame] - : SwfAnimationAsset.Frame.identity; + var frames = _curSequence != null ? _curSequence.Frames : null; + return frames != null && currentFrame >= 0 && currentFrame < frames.Count + ? frames[currentFrame] + : new SwfAnimationAsset.Frame(); } // --------------------------------------------------------------------- @@ -176,7 +213,8 @@ namespace FlashTools { void Awake() { _meshFilter = GetComponent(); _meshRenderer = GetComponent(); - _matPropBlock = new MaterialPropertyBlock(); + _curSequence = null; + _curPropBlock = null; UpdateAllProperties(); } diff --git a/Assets/FlashTools/Scripts/SwfAnimationAsset.cs b/Assets/FlashTools/Scripts/SwfAnimationAsset.cs index 29e0265..08bd7cc 100644 --- a/Assets/FlashTools/Scripts/SwfAnimationAsset.cs +++ b/Assets/FlashTools/Scripts/SwfAnimationAsset.cs @@ -73,19 +73,17 @@ namespace FlashTools { public class SwfAnimationAsset : ScriptableObject { [System.Serializable] public class Frame { - public Mesh Mesh; - public Material[] Materials; - public static Frame identity { - get { - return new Frame{ - Mesh = new Mesh(), - Materials = new Material[0]}; - } - } + public Mesh Mesh = new Mesh(); + public Material[] Materials = new Material[0]; + } + [System.Serializable] + public class Sequence { + public string Name = string.Empty; + public List Frames = new List(); } public SwfAnimationData Data; public Texture2D Atlas; - public List Frames; + public List Sequences; public SwfSettings Settings; public SwfSettings Overridden; @@ -93,7 +91,7 @@ namespace FlashTools { void Reset() { Data = new SwfAnimationData(); Atlas = null; - Frames = new List(); + Sequences = new List(); Settings = SwfConverterSettings.GetDefaultSettings(); Overridden = SwfConverterSettings.GetDefaultSettings(); }