diff --git a/Assembly-CSharp-Editor.csproj b/Assembly-CSharp-Editor.csproj index 0e9e491..7c4f8df 100644 --- a/Assembly-CSharp-Editor.csproj +++ b/Assembly-CSharp-Editor.csproj @@ -47,6 +47,7 @@ + diff --git a/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfAssetEditor.cs b/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfAssetEditor.cs index a7a19f1..94eb2ee 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfAssetEditor.cs +++ b/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfAssetEditor.cs @@ -1,7 +1,6 @@ using UnityEngine; using UnityEditor; -using System; using System.IO; using System.Linq; using System.Collections.Generic; @@ -61,7 +60,7 @@ namespace FlashTools.Internal { // // - void AllAssetsForeach(Action act) { + void AllAssetsForeach(System.Action act) { foreach ( var asset in _assets ) { act(asset); } diff --git a/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipAssetEditor.cs b/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipAssetEditor.cs index 9c2dd43..00a8223 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipAssetEditor.cs +++ b/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipAssetEditor.cs @@ -142,5 +142,9 @@ namespace FlashTools.Internal { serializedObject.ApplyModifiedProperties(); } } + + public override bool RequiresConstantRepaint() { + return true; + } } } \ No newline at end of file diff --git a/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipAssetPreview.cs b/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipAssetPreview.cs new file mode 100644 index 0000000..c6b25d0 --- /dev/null +++ b/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipAssetPreview.cs @@ -0,0 +1,164 @@ +using UnityEngine; +using UnityEditor; + +using System.Linq; + +namespace FlashTools.Internal { + [CustomPreview(typeof(SwfClipAsset))] + public class SwfClipAssetPreview : ObjectPreview { + int _sequenceIndex = 0; + MaterialPropertyBlock _matPropBlock = null; + PreviewRenderUtility _previewUtility = null; + + Texture2D targetAtlas { + get { + var clip = target as SwfClipAsset; + return clip.Atlas; + } + } + + int targetSequenceCount { + get { + var clip = target as SwfClipAsset; + return clip && clip.Sequences != null + ? clip.Sequences.Count + : 0; + } + } + + SwfClipAsset.Frame targetFrame { + get { + var clip = target as SwfClipAsset; + return GetFrameForClip(clip, _sequenceIndex); + } + } + + SwfClipAsset.Sequence targetSequence { + get { + var clip = target as SwfClipAsset; + return GetSequenceForClip(clip, _sequenceIndex); + } + } + + static SwfClipAsset.Frame GetFrameForClip(SwfClipAsset clip, int sequence_index) { + var sequence = GetSequenceForClip(clip, sequence_index); + var frames = sequence != null && sequence.Frames != null && sequence.Frames.Count > 0 + ? sequence.Frames + : null; + var frame_time = (float)(EditorApplication.timeSinceStartup * clip.FrameRate); + return frames != null + ? frames[Mathf.FloorToInt(frame_time) % frames.Count] + : null; + } + + static SwfClipAsset.Sequence GetSequenceForClip(SwfClipAsset clip, int sequence_index) { + return clip && clip.Sequences != null && clip.Sequences.Count > 0 + ? clip.Sequences[Mathf.Abs(sequence_index) % clip.Sequences.Count] + : null; + } + + static Bounds CalculateBoundsForSequence(SwfClipAsset.Sequence sequence) { + var bounds = sequence != null && sequence.Frames != null && sequence.Frames.Count > 0 + ? sequence.Frames + .Where (p => !!p.Mesh) + .Select(p => p.Mesh.bounds) + : new Bounds[0]; + var result = bounds.Any() ? bounds.First() : new Bounds(); + foreach ( var bound in bounds ) { + result.Encapsulate(bound); + } + return result; + } + + static void ConfigureCameraForSequence(Camera camera, SwfClipAsset.Sequence sequence) { + var bounds = CalculateBoundsForSequence(sequence); + camera.orthographic = true; + camera.orthographicSize = Mathf.Max( + Mathf.Abs(bounds.extents.x), + Mathf.Abs(bounds.extents.y)); + camera.transform.position = new Vector3( + bounds.center.x, + bounds.center.y, + -10.0f); + } + + // --------------------------------------------------------------------- + // + // Functions + // + // --------------------------------------------------------------------- + + public void SetCurrentSequence(string sequence_name) { + var clip = target as SwfClipAsset; + _sequenceIndex = clip && clip.Sequences != null + ? Mathf.Max(0, clip.Sequences.FindIndex(p => p.Name == sequence_name)) + : 0; + } + + // --------------------------------------------------------------------- + // + // Messages + // + // --------------------------------------------------------------------- + + public override void Initialize(Object[] targets) { + base.Initialize(targets); + _matPropBlock = new MaterialPropertyBlock(); + _previewUtility = new PreviewRenderUtility(); + } + + public override bool HasPreviewGUI() { + return true; + } + + public override void OnPreviewSettings() { + var any_multi_sequences = m_Targets + .OfType() + .Any(p => p.Sequences != null && p.Sequences.Count > 1); + if ( any_multi_sequences && GUILayout.Button("<", EditorStyles.miniButton) ) { + --_sequenceIndex; + } + var sequence_names = m_Targets + .OfType() + .Select (p => GetSequenceForClip(p, _sequenceIndex)) + .Where (p => p != null && !string.IsNullOrEmpty(p.Name)) + .Select (p => p.Name) + .ToArray(); + var label_text = string.Empty; + for ( int i = 0, e = sequence_names.Length; i < e; ++i ) { + label_text += string.Format( + i < e - 1 ? "{0}, " : "{0}", + sequence_names[i]); + } + GUILayout.Label(label_text, EditorStyles.whiteLabel); + if ( any_multi_sequences && GUILayout.Button(">", EditorStyles.miniButton) ) { + ++_sequenceIndex; + } + } + + public override void OnPreviewGUI(Rect r, GUIStyle background) { + if ( Event.current.type == EventType.Repaint ) { + var atlas = targetAtlas; + var frame = targetFrame; + var sequence = targetSequence; + if ( atlas && frame != null && sequence != null ) { + _previewUtility.BeginPreview(r, background); + { + _matPropBlock.SetTexture("_MainTex", atlas); + ConfigureCameraForSequence(_previewUtility.m_Camera, sequence); + for ( var i = 0; i < frame.Materials.Length; ++i ) { + _previewUtility.DrawMesh( + frame.Mesh, + Matrix4x4.identity, + frame.Materials[i], + i, + _matPropBlock); + } + _previewUtility.m_Camera.Render(); + } + _previewUtility.EndAndDrawPreview(r); + } + } + } + } +} \ No newline at end of file diff --git a/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipAssetPreview.cs.meta b/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipAssetPreview.cs.meta new file mode 100644 index 0000000..1e87e1c --- /dev/null +++ b/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipAssetPreview.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 61493c5b6491d4432a831d25914ed92a +timeCreated: 1473360782 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipControllerEditor.cs b/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipControllerEditor.cs index 1333c92..459d06f 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipControllerEditor.cs +++ b/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipControllerEditor.cs @@ -1,7 +1,6 @@ using UnityEngine; using UnityEditor; -using System; using System.Linq; using System.Collections.Generic; @@ -10,7 +9,7 @@ namespace FlashTools.Internal { public class SwfClipControllerEditor : Editor { List _controllers = new List(); - void AllControllersForeach(Action act) { + void AllControllersForeach(System.Action act) { foreach ( var controller in _controllers ) { act(controller); } diff --git a/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipEditor.cs b/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipEditor.cs index c740222..a76974f 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipEditor.cs +++ b/Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipEditor.cs @@ -1,19 +1,17 @@ using UnityEngine; using UnityEditor; -using System; using System.Linq; using System.Collections.Generic; namespace FlashTools.Internal { [CustomEditor(typeof(SwfClip)), CanEditMultipleObjects] public class SwfClipEditor : Editor { - List _clips = new List(); + List _clips = new List(); + Dictionary _previews = new Dictionary(); - void AllClipsForeachWithUndo(Action act) { - Undo.RecordObjects( - _clips.ToArray(), - "Inspector"); + void AllClipsForeachWithUndo(System.Action act) { + Undo.RecordObjects(_clips.ToArray(), "Inspector"); foreach ( var clip in _clips ) { act(clip); EditorUtility.SetDirty(clip); @@ -141,9 +139,12 @@ namespace FlashTools.Internal { // --------------------------------------------------------------------- void OnEnable() { - _clips = targets - .OfType() - .ToList(); + _clips = targets.OfType().ToList(); + foreach ( var clip in _clips.Where(p => !!p.clip) ) { + var preview = new SwfClipAssetPreview(); + preview.Initialize(new Object[]{clip.clip}); + _previews.Add(clip, preview); + } } public override void OnInspectorGUI() { @@ -155,5 +156,24 @@ namespace FlashTools.Internal { serializedObject.ApplyModifiedProperties(); } } + + public override bool RequiresConstantRepaint() { + return _previews.Count > 0; + } + + public override bool HasPreviewGUI() { + return _previews.Count > 0; + } + + public override void OnPreviewGUI(Rect r, GUIStyle background) { + if ( Event.current.type == EventType.Repaint ) { + SwfClipAssetPreview preview; + var clip = target as SwfClip; + if ( _previews.TryGetValue(clip, out preview) ) { + preview.SetCurrentSequence(clip.sequence); + preview.DrawPreview(r); + } + } + } } } \ No newline at end of file diff --git a/Assets/FlashTools/Scripts/SwfManager.cs b/Assets/FlashTools/Scripts/SwfManager.cs index 1e3a043..c6a1e9c 100644 --- a/Assets/FlashTools/Scripts/SwfManager.cs +++ b/Assets/FlashTools/Scripts/SwfManager.cs @@ -91,8 +91,7 @@ namespace FlashTools { _controllers.Clear(); } - void UpdateControllers() { - var dt = Time.deltaTime; + void UpdateControllers(float dt) { _controllers.AssignTo(_safeUpdates); for ( int i = 0, e = _safeUpdates.Count; i < e; ++i ) { var ctrl = _safeUpdates[i]; @@ -119,7 +118,8 @@ namespace FlashTools { } void Update() { - UpdateControllers(); + var dt = Time.deltaTime; + UpdateControllers(dt); } } } \ No newline at end of file diff --git a/unityflash.userprefs b/unityflash.userprefs index ff50da4..4ec6e6f 100644 --- a/unityflash.userprefs +++ b/unityflash.userprefs @@ -1,6 +1,10 @@  - + + + + +