Bläddra i källkod

Implemented SkeletonBinary for spine-unity runtimes.

Fenrisul 10 år sedan
förälder
incheckning
4369f0ee6c

+ 31 - 4
spine-unity/Assets/spine-unity/Editor/SkeletonDataAssetInspector.cs

@@ -34,6 +34,7 @@
 *****************************************************************************/
 using System;
 using System.Collections.Generic;
+using System.IO;
 using UnityEditor;
 
 #if !UNITY_4_3
@@ -123,6 +124,10 @@ public class SkeletonDataAssetInspector : Editor {
 		EditorGUILayout.PropertyField(spriteCollection, true);
 #endif
 		EditorGUILayout.PropertyField(skeletonJSON);
+		GUILayout.Label("Load Time: " + m_skeletonDataAsset.LoadTime + "ms");
+		if (IsBinary) {
+			GUILayout.Label(new GUIContent("Binary Mode Engaged! Hold on to your pants!", SpineEditorUtilities.Icons.warning));
+		}
 		EditorGUILayout.PropertyField(scale);
 		if (EditorGUI.EndChangeCheck()) {
 			if (serializedObject.ApplyModifiedProperties()) {
@@ -256,6 +261,18 @@ public class SkeletonDataAssetInspector : Editor {
 			m_previewUtility = null;
 		}
 
+		//Temporary Binary atlas name-match reimport
+		if (IsBinary && m_skeletonData == null) {
+			string dirPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(m_skeletonDataAsset));
+			string maybeAtlasPath = Path.Combine(dirPath, m_skeletonDataAsset.skeletonJSON.name.Replace(".skel", "_Atlas.asset"));
+			if (File.Exists(maybeAtlasPath)) {
+				var atlas = (AtlasAsset)AssetDatabase.LoadAssetAtPath(maybeAtlasPath, typeof(AtlasAsset));
+				if (atlas != null) {
+					m_skeletonDataAsset.atlasAssets = new AtlasAsset[1] { atlas };
+				}
+			}
+		}
+
 		RepopulateWarnings();
 		OnEnable();
 
@@ -447,6 +464,14 @@ public class SkeletonDataAssetInspector : Editor {
 		EditorGUI.indentLevel--;
 	}
 
+	public bool IsBinary {
+		get {
+			if (skeletonJSON.objectReferenceValue == null)
+				return false;
+
+			return skeletonJSON.objectReferenceValue.name.EndsWith(".skel");
+		}
+	}
 
 	void RepopulateWarnings () {
 		warnings.Clear();
@@ -455,7 +480,12 @@ public class SkeletonDataAssetInspector : Editor {
 			warnings.Add("Missing Skeleton JSON");
 		else {
 
-			if (SpineEditorUtilities.IsSpineJSON((TextAsset)skeletonJSON.objectReferenceValue) == false) {
+			if (IsBinary) {
+				//can't pre process binary yet
+				warnings.Add("Cannot analyze Skeleton Binary for errors!");
+				warnings.Add("Probably can't attempt reimport yet either :)");
+			}
+			else if (SpineEditorUtilities.IsSpineJSON((TextAsset)skeletonJSON.objectReferenceValue) == false) {
 				warnings.Add("Skeleton JSON is not a Valid JSON file");
 			} else {
 				bool detectedNullAtlasEntry = false;
@@ -486,9 +516,6 @@ public class SkeletonDataAssetInspector : Editor {
 
 					foreach (var str in missingPaths)
 						warnings.Add("Missing Region: '" + str + "'");
-
-
-
 				}
 			}
 

+ 61 - 10
spine-unity/Assets/spine-unity/SkeletonDataAsset.cs

@@ -48,6 +48,15 @@ public class SkeletonDataAsset : ScriptableObject {
 	public RuntimeAnimatorController controller;
 	private SkeletonData skeletonData;
 	private AnimationStateData stateData;
+	private double loadTime = -1;
+
+	public double LoadTime {
+		get {
+			return loadTime;
+		}
+	}
+
+	
 
 	public void Reset() {
 		skeletonData = null;
@@ -55,6 +64,13 @@ public class SkeletonDataAsset : ScriptableObject {
 	}
 
 	public SkeletonData GetSkeletonData(bool quiet) {
+		if (skeletonData == null) {
+			loadTime = -1;
+		}
+			
+
+		DateTime startLoadTime = DateTime.Now;
+
 		if (atlasAssets == null) {
 			atlasAssets = new AtlasAsset[0];
 			if (!quiet)
@@ -98,11 +114,43 @@ public class SkeletonDataAsset : ScriptableObject {
 		if (skeletonData != null)
 			return skeletonData;
 
-		SkeletonJson json;
+		bool isBinary = skeletonJSON.name.EndsWith(".skel");
+
+		if (isBinary) {
+			SkeletonBinary binary;
 
 #if !SPINE_TK2D
-		json = new SkeletonJson(atlasArr);
-		json.Scale = scale;
+			binary = new SkeletonBinary(atlasArr);
+			binary.Scale = scale;
+#else
+		if (spriteCollection != null) {
+			binary = new SkeletonBinary(new SpriteCollectionAttachmentLoader(spriteCollection));
+			binary.Scale = (1.0f / (spriteCollection.invOrthoSize * spriteCollection.halfTargetHeight) * scale) * 100f;
+		} else {
+			if (atlasArr.Length == 0) {
+				Reset();
+				if (!quiet)
+					Debug.LogError("Atlas not set for SkeletonData asset: " + name, this);
+				return null;
+			}
+			binary = new SkeletonBinary(atlasArr);
+			binary.Scale = scale;
+		}
+#endif
+			try {
+				skeletonData = binary.ReadSkeletonData(new BufferedStream(new MemoryStream(skeletonJSON.bytes)));
+			} catch (Exception ex) {
+				if (!quiet)
+					Debug.LogError("Error reading skeleton binary file for SkeletonData asset: " + name + "\n" + ex.Message + "\n" + ex.StackTrace, this);
+				return null;
+			}
+
+		} else {
+			SkeletonJson json;
+
+#if !SPINE_TK2D
+			json = new SkeletonJson(atlasArr);
+			json.Scale = scale;
 #else
 		if (spriteCollection != null) {
 			json = new SkeletonJson(new SpriteCollectionAttachmentLoader(spriteCollection));
@@ -119,18 +167,21 @@ public class SkeletonDataAsset : ScriptableObject {
 		}
 #endif
 
-		
-		try {
-			skeletonData = json.ReadSkeletonData(new StringReader(skeletonJSON.text));
-		} 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;
+
+			try {
+				skeletonData = json.ReadSkeletonData(new StringReader(skeletonJSON.text));
+			} 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();
 
+		loadTime = (DateTime.Now - startLoadTime).TotalMilliseconds;
+
 		return skeletonData;
 	}