Эх сурвалжийг харах

[unity] Fix SkeletonUtility and BoundingBox functionality.

pharan 8 жил өмнө
parent
commit
78855cdb70

+ 2 - 0
spine-unity/Assets/spine-unity/Editor/SpineEditorUtilities.cs

@@ -28,6 +28,8 @@
  * POSSIBILITY OF SUCH DAMAGE.
  *****************************************************************************/
 
+#pragma warning disable 0219
+
 // Contributed by: Mitch Thompson
 
 #define SPINE_SKELETONANIMATOR

+ 1 - 1
spine-unity/Assets/spine-unity/SkeletonExtensions.cs

@@ -180,7 +180,7 @@ namespace Spine.Unity {
 				for (int i = 0; i < bufferTargetSize; i++) {
 					int j = i * 2;
 					float x = floats[j] - bwx, y = floats[j+1] - bwy;
-					buffer[i] = new Vector2(x * ia + y * ic, x * ib + y * id);
+					buffer[i] = new Vector2(x * ia + y * ib, x * ic + y * id);
 				}
 			}
 

+ 13 - 11
spine-unity/Assets/spine-unity/SkeletonUtility/Editor/SkeletonUtilityBoneInspector.cs

@@ -184,7 +184,7 @@ namespace Spine.Unity.Editor {
 			using (new EditorGUI.DisabledGroupScope(multiObject || boundingBoxTable.Count == 0)) {
 				EditorGUILayout.LabelField(new GUIContent("Bounding Boxes", SpineEditorUtilities.Icons.boundingBox), EditorStyles.boldLabel);
 
-				foreach(var entry in boundingBoxTable){
+				foreach (var entry in boundingBoxTable){
 					Slot slot = entry.Key;
 					var boundingBoxes = entry.Value;
 
@@ -195,19 +195,21 @@ namespace Spine.Unity.Editor {
 						foreach (var box in boundingBoxes) {
 							using (new GUILayout.HorizontalScope()) {
 								GUILayout.Space(30);
-								if (GUILayout.Button(box.Name, GUILayout.Width(200))) {
-									var child = utilityBone.transform.FindChild("[BoundingBox]" + box.Name);
-									if (child != null) {
-										var originalCollider = child.GetComponent<PolygonCollider2D>();
-										var updatedCollider = SkeletonUtility.AddBoundingBoxAsComponent(box, slot, child.gameObject, originalCollider.isTrigger);
-										originalCollider.points = updatedCollider.points;
-										if (EditorApplication.isPlaying)
-											Destroy(updatedCollider);
+								string buttonLabel = box.IsWeighted() ? box.Name + " (!)" : box.Name;
+								if (GUILayout.Button(buttonLabel, GUILayout.Width(200))) {
+									utilityBone.bone.Skeleton.UpdateWorldTransform();
+									var bbTransform = utilityBone.transform.FindChild("[BoundingBox]" + box.Name);
+									if (bbTransform != null) {
+										var originalCollider = bbTransform.GetComponent<PolygonCollider2D>();
+										if (originalCollider != null)
+											SkeletonUtility.SetColliderPointsLocal(originalCollider, slot, box);
 										else
-											DestroyImmediate(updatedCollider);
+											SkeletonUtility.AddBoundingBoxAsComponent(box, slot, bbTransform.gameObject);
 									} else {
-										utilityBone.AddBoundingBox(currentSkinName, slot.Data.Name, box.Name);
+										var newPolygonCollider = SkeletonUtility.AddBoundingBoxGameObject(null, box, slot, utilityBone.transform);
+										bbTransform = newPolygonCollider.transform;
 									}
+									EditorGUIUtility.PingObject(bbTransform);
 								}
 							}
 

+ 28 - 12
spine-unity/Assets/spine-unity/SkeletonUtility/SkeletonUtility.cs

@@ -40,39 +40,55 @@ namespace Spine.Unity {
 	public class SkeletonUtility : MonoBehaviour {
 	
 		#region BoundingBoxAttachment
-		public static PolygonCollider2D AddBoundingBox (Skeleton skeleton, string skinName, string slotName, string attachmentName, Transform parent, bool isTrigger = true) {
+		public static PolygonCollider2D AddBoundingBoxGameObject (Skeleton skeleton, string skinName, string slotName, string attachmentName, Transform parent, bool isTrigger = true) {
 			Skin skin = string.IsNullOrEmpty(skinName) ? skeleton.data.defaultSkin : skeleton.data.FindSkin(skinName);
 			if (skin == null) {
 				Debug.LogError("Skin " + skinName + " not found!");
 				return null;
 			}
 
+			int slotIndex = skeleton.FindSlotIndex(slotName);
 			var attachment = skin.GetAttachment(skeleton.FindSlotIndex(slotName), attachmentName);
+			if (attachment == null) {
+				Debug.LogFormat("Attachment in slot '{0}' named '{1}' not found in skin '{2}'.", slotName, attachmentName, skin.name);
+				return null;
+			}
+
 			var box = attachment as BoundingBoxAttachment;
 			if (box != null) {
-				var go = new GameObject("[BoundingBox]" + attachmentName);
-				var got = go.transform;
-				got.parent = parent;
-				got.localPosition = Vector3.zero;
-				got.localRotation = Quaternion.identity;
-				got.localScale = Vector3.one;
 				var slot = skeleton.FindSlot(slotName);
-				return AddBoundingBoxAsComponent(box, slot, go, isTrigger);
+				return AddBoundingBoxGameObject(box.Name, box, slot, parent, isTrigger);
+			} else {
+				Debug.LogFormat("Attachment '{0}' was not a Bounding Box.", attachmentName);
+				return null;
 			}
+		}
 
-			return null;
+		public static PolygonCollider2D AddBoundingBoxGameObject (string name, BoundingBoxAttachment box, Slot slot, Transform parent, bool isTrigger = true) {
+			var go = new GameObject("[BoundingBox]" + (string.IsNullOrEmpty(name) ? box.Name : name));
+			var got = go.transform;
+			got.parent = parent;
+			got.localPosition = Vector3.zero;
+			got.localRotation = Quaternion.identity;
+			got.localScale = Vector3.one;
+			return AddBoundingBoxAsComponent(box, slot, go, isTrigger);
 		}
 
 		public static PolygonCollider2D AddBoundingBoxAsComponent (BoundingBoxAttachment box, Slot slot, GameObject gameObject, bool isTrigger = true) {
 			if (box == null) return null;
-			if (box.IsWeighted()) Debug.LogWarning("UnityEngine.PolygonCollider2D does not support weighted or animated points. Collider will not be animated. If you want to use it as a collider, please remove weights and animations from the bounding box in Spine editor.");
-			var verts = box.GetLocalVertices(slot, null);
 			var collider = gameObject.AddComponent<PolygonCollider2D>();
 			collider.isTrigger = isTrigger;
-			collider.SetPath(0, verts);
+			SetColliderPointsLocal(collider, slot, box);
 			return collider;
 		}
 
+		public static void SetColliderPointsLocal (PolygonCollider2D collider, Slot slot, BoundingBoxAttachment box) {
+			if (box == null) return;
+			if (box.IsWeighted()) Debug.LogWarning("UnityEngine.PolygonCollider2D does not support weighted or animated points. Collider points will not be animated and may have incorrect orientation. If you want to use it as a collider, please remove weights and animations from the bounding box in Spine editor.");
+			var verts = box.GetLocalVertices(slot, null);
+			collider.SetPath(0, verts);
+		}
+
 		public static Bounds GetBoundingBoxBounds (BoundingBoxAttachment boundingBox, float depth = 0) {
 			float[] floats = boundingBox.Vertices;
 			int floatCount = floats.Length;

+ 3 - 5
spine-unity/Assets/spine-unity/SkeletonUtility/SkeletonUtilityBone.cs

@@ -63,9 +63,7 @@ namespace Spine.Unity {
 		Transform cachedTransform;
 		Transform skeletonTransform;
 		bool incompatibleTransformMode;
-		public bool IncompatibleTransformMode {
-			get { return incompatibleTransformMode; }
-		}
+		public bool IncompatibleTransformMode { get { return incompatibleTransformMode; } }
 
 		public void Reset () {
 			bone = null;
@@ -132,7 +130,7 @@ namespace Spine.Unity {
 				}
 
 				if (scale) {
-					cachedTransform.localScale = new Vector3(bone.scaleX, bone.scaleY, 1f);//, bone.WorldSignX);
+					cachedTransform.localScale = new Vector3(bone.scaleX, bone.scaleY, 1f);
 					incompatibleTransformMode = BoneTransformModeIncompatible(bone);
 				}
 			} else if (mode == Mode.Override) {
@@ -193,7 +191,7 @@ namespace Spine.Unity {
 		}
 
 		public void AddBoundingBox (string skinName, string slotName, string attachmentName) {
-			SkeletonUtility.AddBoundingBox(bone.skeleton, skinName, slotName, attachmentName, transform);
+			SkeletonUtility.AddBoundingBoxGameObject(bone.skeleton, skinName, slotName, attachmentName, transform);
 		}
 
 		#if UNITY_EDITOR