From c54cb820fed346740a5e4b00aa86c51a2c477c05 Mon Sep 17 00:00:00 2001 From: pharan Date: Sat, 17 Dec 2016 14:42:27 +0800 Subject: [PATCH] [unity] Updates to asset types for custom loading. --- .../spine-unity/Asset Types/AtlasAsset.cs | 10 +-- .../Editor/SkeletonDataAssetInspector.cs | 38 +++++--- .../Asset Types/SkeletonDataAsset.cs | 87 +++++++++++-------- 3 files changed, 81 insertions(+), 54 deletions(-) diff --git a/spine-unity/Assets/spine-unity/Asset Types/AtlasAsset.cs b/spine-unity/Assets/spine-unity/Asset Types/AtlasAsset.cs index a7506f198..d39014021 100644 --- a/spine-unity/Assets/spine-unity/Asset Types/AtlasAsset.cs +++ b/spine-unity/Assets/spine-unity/Asset Types/AtlasAsset.cs @@ -41,6 +41,8 @@ namespace Spine.Unity { public Material[] materials; protected Atlas atlas; + public bool IsLoaded { get { return this.atlas != null; } } + #region Runtime Instantiation /// /// Creates a runtime AtlasAsset @@ -122,8 +124,7 @@ namespace Spine.Unity { return null; } - if (atlas != null) - return atlas; + if (atlas != null) return atlas; try { atlas = new Atlas(new StringReader(atlasFile.text), "", new MaterialsTextureLoader(this)); @@ -201,7 +202,7 @@ namespace Spine.Unity { this.atlasAsset = atlasAsset; } - public void Load (AtlasPage page, String path) { + public void Load (AtlasPage page, string path) { String name = Path.GetFileNameWithoutExtension(path); Material material = null; foreach (Material other in atlasAsset.materials) { @@ -227,7 +228,6 @@ namespace Spine.Unity { } } - public void Unload (object texture) { - } + public void Unload (object texture) { } } } diff --git a/spine-unity/Assets/spine-unity/Asset Types/Editor/SkeletonDataAssetInspector.cs b/spine-unity/Assets/spine-unity/Asset Types/Editor/SkeletonDataAssetInspector.cs index f4b385d58..48f698ff5 100644 --- a/spine-unity/Assets/spine-unity/Asset Types/Editor/SkeletonDataAssetInspector.cs +++ b/spine-unity/Assets/spine-unity/Asset Types/Editor/SkeletonDataAssetInspector.cs @@ -79,9 +79,27 @@ namespace Spine.Unity.Editor { GUIStyle activePlayButtonStyle, idlePlayButtonStyle; readonly GUIContent DefaultMixLabel = new GUIContent("Default Mix Duration", "Sets 'SkeletonDataAsset.defaultMix' in the asset and 'AnimationState.data.defaultMix' at runtime load time."); - void OnEnable () { SpineEditorUtilities.ConfirmInitialization(); + m_skeletonDataAsset = (SkeletonDataAsset)target; + + // Clear empty atlas array items. + { + bool hasNulls = false; + foreach (var a in m_skeletonDataAsset.atlasAssets) { + if (a == null) { + hasNulls = true; + break; + } + } + if (hasNulls) { + var trimmedAtlasAssets = new List(); + foreach (var a in m_skeletonDataAsset.atlasAssets) { + if (a != null) trimmedAtlasAssets.Add(a); + } + m_skeletonDataAsset.atlasAssets = trimmedAtlasAssets.ToArray(); + } + } atlasAssets = serializedObject.FindProperty("atlasAssets"); skeletonJSON = serializedObject.FindProperty("skeletonJSON"); @@ -106,7 +124,6 @@ namespace Spine.Unity.Editor { isBakingExpanded = EditorPrefs.GetBool(ShowBakingPrefsKey, false); #endif - m_skeletonDataAsset = (SkeletonDataAsset)target; m_skeletonDataAssetGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(m_skeletonDataAsset)); EditorApplication.update += EditorUpdate; m_skeletonData = m_skeletonDataAsset.GetSkeletonData(false); @@ -125,7 +142,6 @@ namespace Spine.Unity.Editor { override public void OnInspectorGUI () { { - // Lazy initialization because accessing EditorStyles values in OnEnable during a recompile causes UnityEditor to throw null exceptions. (Unity 5.3.5) idlePlayButtonStyle = idlePlayButtonStyle ?? new GUIStyle(EditorStyles.miniButton); if (activePlayButtonStyle == null) { @@ -207,7 +223,6 @@ namespace Spine.Unity.Editor { EditorGUILayout.Space(); DrawSlotList(); EditorGUILayout.Space(); - DrawUnityTools(); } else { #if !SPINE_TK2D @@ -483,10 +498,6 @@ namespace Spine.Unity.Editor { icon = Icons.warning; //JOHN: left todo: Icon for paths. Generic icon for unidentified attachments. - // MITCH: left todo: Waterboard Nate - //if (name != attachment.Name) - //icon = SpineEditorUtilities.Icons.skinPlaceholder; - bool initialState = slot.Attachment == attachment; bool toggled = EditorGUILayout.ToggleLeft(new GUIContent(attachmentName, icon), slot.Attachment == attachment); @@ -606,11 +617,12 @@ namespace Spine.Unity.Editor { if (this.m_previewUtility == null) { this.m_lastTime = Time.realtimeSinceStartup; this.m_previewUtility = new PreviewRenderUtility(true); - this.m_previewUtility.m_Camera.orthographic = true; - this.m_previewUtility.m_Camera.orthographicSize = 1; - this.m_previewUtility.m_Camera.cullingMask = -2147483648; - this.m_previewUtility.m_Camera.nearClipPlane = 0.01f; - this.m_previewUtility.m_Camera.farClipPlane = 1000f; + var c = this.m_previewUtility.m_Camera; + c.orthographic = true; + c.orthographicSize = 1; + c.cullingMask = -2147483648; + c.nearClipPlane = 0.01f; + c.farClipPlane = 1000f; this.CreatePreviewInstances(); } } diff --git a/spine-unity/Assets/spine-unity/Asset Types/SkeletonDataAsset.cs b/spine-unity/Assets/spine-unity/Asset Types/SkeletonDataAsset.cs index cb5d5e8a2..8fb9ee307 100644 --- a/spine-unity/Assets/spine-unity/Asset Types/SkeletonDataAsset.cs +++ b/spine-unity/Assets/spine-unity/Asset Types/SkeletonDataAsset.cs @@ -50,6 +50,8 @@ namespace Spine.Unity { public float defaultMix; public RuntimeAnimatorController controller; + public bool IsLoaded { get { return this.skeletonData != null; } } + void Reset () { Clear(); } @@ -109,77 +111,90 @@ namespace Spine.Unity { } #else if (atlasAssets.Length == 0 && spriteCollection == null) { - Reset(); + Clear(); return null; } #endif - - Atlas[] atlasArr = new Atlas[atlasAssets.Length]; - for (int i = 0; i < atlasAssets.Length; i++) { - if (atlasAssets[i] == null) { - Clear(); - return null; - } - atlasArr[i] = atlasAssets[i].GetAtlas(); - if (atlasArr[i] == null) { - Clear(); - return null; - } - } - if (skeletonData != null) return skeletonData; AttachmentLoader attachmentLoader; float skeletonDataScale; + Atlas[] atlasArray = this.GetAtlasArray(); #if !SPINE_TK2D - attachmentLoader = new AtlasAttachmentLoader(atlasArr); + attachmentLoader = new AtlasAttachmentLoader(atlasArray); skeletonDataScale = scale; #else if (spriteCollection != null) { attachmentLoader = new Spine.Unity.TK2D.SpriteCollectionAttachmentLoader(spriteCollection); skeletonDataScale = (1.0f / (spriteCollection.invOrthoSize * spriteCollection.halfTargetHeight) * scale); } else { - if (atlasArr.Length == 0) { + if (atlasArray.Length == 0) { Reset(); if (!quiet) Debug.LogError("Atlas not set for SkeletonData asset: " + name, this); return null; } - attachmentLoader = new AtlasAttachmentLoader(atlasArr); + attachmentLoader = new AtlasAttachmentLoader(atlasArray); skeletonDataScale = scale; } #endif + bool isBinary = skeletonJSON.name.ToLower().Contains(".skel"); + SkeletonData loadedSkeletonData; + try { - //var stopwatch = new System.Diagnostics.Stopwatch(); - if (skeletonJSON.name.ToLower().Contains(".skel")) { - var input = new MemoryStream(skeletonJSON.bytes); - var binary = new SkeletonBinary(attachmentLoader); - binary.Scale = skeletonDataScale; - //stopwatch.Start(); - skeletonData = binary.ReadSkeletonData(input); - } else { - var input = new StringReader(skeletonJSON.text); - var json = new SkeletonJson(attachmentLoader); - json.Scale = skeletonDataScale; - //stopwatch.Start(); - skeletonData = json.ReadSkeletonData(input); - } - //stopwatch.Stop(); - //Debug.Log(stopwatch.Elapsed); + if (isBinary) + loadedSkeletonData = SkeletonDataAsset.ReadSkeletonData(skeletonJSON.bytes, attachmentLoader, skeletonDataScale); + else + loadedSkeletonData = SkeletonDataAsset.ReadSkeletonData(skeletonJSON.text, attachmentLoader, skeletonDataScale); + } catch (Exception ex) { if (!quiet) Debug.LogError("Error reading skeleton JSON file for SkeletonData asset: " + name + "\n" + ex.Message + "\n" + ex.StackTrace, this); return null; + } - stateData = new AnimationStateData(skeletonData); - FillStateData(); + this.InitializeWithData(loadedSkeletonData); return skeletonData; } + internal void InitializeWithData (SkeletonData sd) { + this.skeletonData = sd; + this.stateData = new AnimationStateData(skeletonData); + FillStateData(); + } + + internal Atlas[] GetAtlasArray () { + var returnList = new System.Collections.Generic.List(atlasAssets.Length); + for (int i = 0; i < atlasAssets.Length; i++) { + var aa = atlasAssets[i]; + if (aa == null) continue; + var a = aa.GetAtlas(); + if (a == null) continue; + returnList.Add(a); + } + return returnList.ToArray(); + } + + internal static SkeletonData ReadSkeletonData (byte[] bytes, AttachmentLoader attachmentLoader, float scale) { + var input = new MemoryStream(bytes); + var binary = new SkeletonBinary(attachmentLoader) { + Scale = scale + }; + return binary.ReadSkeletonData(input); + } + + internal static SkeletonData ReadSkeletonData (string text, AttachmentLoader attachmentLoader, float scale) { + var input = new StringReader(text); + var json = new SkeletonJson(attachmentLoader) { + Scale = scale + }; + return json.ReadSkeletonData(input); + } + public void FillStateData () { if (stateData != null) { stateData.defaultMix = defaultMix;