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

[unity] Improved SkeletonGraphic's "Match RectTransform with Mesh Bounds" features: providing a button in the Inspector, automatically called when instantiated in Editor. Closes #1607.
Improved general drag and drop behaviour of SkeletonDataAssets, they are now parented to the drop mouse-over target.

Harald Csaszar 5 жил өмнө
parent
commit
3a7aff7a5c

+ 14 - 23
spine-unity/Assets/Spine/Editor/spine-unity/Editor/Components/SkeletonGraphicInspector.cs

@@ -115,6 +115,15 @@ namespace Spine.Unity.Editor {
 			EditorGUILayout.LabelField("UI", EditorStyles.boldLabel);
 			EditorGUILayout.PropertyField(raycastTarget);
 
+			EditorGUILayout.BeginHorizontal(GUILayout.Height(EditorGUIUtility.singleLineHeight + 5));
+			EditorGUILayout.PrefixLabel("Match RectTransform with Mesh");
+			if (GUILayout.Button("Match", EditorStyles.miniButton, GUILayout.Width(65f))) {
+				foreach (var skeletonGraphic in targets) {
+					MatchRectTransformWithBounds((SkeletonGraphic)skeletonGraphic);
+				}
+			}
+			EditorGUILayout.EndHorizontal();
+
 			bool wasChanged = EditorGUI.EndChangeCheck();
 
 			if (wasChanged)
@@ -125,29 +134,12 @@ namespace Spine.Unity.Editor {
 		[MenuItem("CONTEXT/SkeletonGraphic/Match RectTransform with Mesh Bounds")]
 		static void MatchRectTransformWithBounds (MenuCommand command) {
 			var skeletonGraphic = (SkeletonGraphic)command.context;
-			Mesh mesh = skeletonGraphic.GetLastMesh();
-			if (mesh == null) {
-				Debug.Log("Mesh was not previously generated.");
-				return;
-			}
-
-			if (mesh.vertexCount == 0) {
-				skeletonGraphic.rectTransform.sizeDelta = new Vector2(50f, 50f);
-				skeletonGraphic.rectTransform.pivot = new Vector2(0.5f, 0.5f);
-				return;
-			}
+			MatchRectTransformWithBounds(skeletonGraphic);
+		}
 
-			mesh.RecalculateBounds();
-			var bounds = mesh.bounds;
-			var size = bounds.size;
-			var center = bounds.center;
-			var p = new Vector2(
-				0.5f - (center.x / size.x),
-				0.5f - (center.y / size.y)
-			);
-
-			skeletonGraphic.rectTransform.sizeDelta = size;
-			skeletonGraphic.rectTransform.pivot = p;
+		static void MatchRectTransformWithBounds (SkeletonGraphic skeletonGraphic) {
+			if (!skeletonGraphic.MatchRectTransformWithBounds())
+				Debug.Log("Mesh was not previously generated.");
 		}
 
 		[MenuItem("GameObject/Spine/SkeletonGraphic (UnityUI)", false, 15)]
@@ -200,7 +192,6 @@ namespace Spine.Unity.Editor {
 			graphic.initialSkinName = skin.Name;
 			graphic.Skeleton.UpdateWorldTransform();
 			graphic.UpdateMesh();
-
 			return graphic;
 		}
 

+ 19 - 9
spine-unity/Assets/Spine/Editor/spine-unity/Editor/Utility/Instantiation.cs

@@ -47,6 +47,7 @@ namespace Spine.Unity.Editor {
 		public static class DragAndDropInstantiation {
 			public struct SpawnMenuData {
 				public Vector3 spawnPoint;
+				public Transform parent;
 				public SkeletonDataAsset skeletonDataAsset;
 				public EditorInstantiation.InstantiateDelegate instantiateDelegate;
 				public bool isUI;
@@ -81,7 +82,7 @@ namespace Spine.Unity.Editor {
 								RectTransform rectTransform = (Selection.activeGameObject == null) ? null : Selection.activeGameObject.GetComponent<RectTransform>();
 								Plane plane = (rectTransform == null) ? new Plane(Vector3.back, Vector3.zero) : new Plane(-rectTransform.forward, rectTransform.position);
 								Vector3 spawnPoint = MousePointToWorldPoint2D(mousePos, sceneview.camera, plane);
-								ShowInstantiateContextMenu(skeletonDataAsset, spawnPoint);
+								ShowInstantiateContextMenu(skeletonDataAsset, spawnPoint, null);
 								DragAndDrop.AcceptDrag();
 								current.Use();
 							}
@@ -90,13 +91,14 @@ namespace Spine.Unity.Editor {
 				}
 			}
 
-			public static void ShowInstantiateContextMenu (SkeletonDataAsset skeletonDataAsset, Vector3 spawnPoint) {
+			public static void ShowInstantiateContextMenu (SkeletonDataAsset skeletonDataAsset, Vector3 spawnPoint, Transform parent) {
 				var menu = new GenericMenu();
 
 				// SkeletonAnimation
 				menu.AddItem(new GUIContent("SkeletonAnimation"), false, HandleSkeletonComponentDrop, new SpawnMenuData {
 					skeletonDataAsset = skeletonDataAsset,
 					spawnPoint = spawnPoint,
+					parent = parent,
 					instantiateDelegate = (data) => EditorInstantiation.InstantiateSkeletonAnimation(data),
 					isUI = false
 				});
@@ -109,6 +111,7 @@ namespace Spine.Unity.Editor {
 						menu.AddItem(new GUIContent("SkeletonGraphic (UI)"), false, HandleSkeletonComponentDrop, new SpawnMenuData {
 							skeletonDataAsset = skeletonDataAsset,
 							spawnPoint = spawnPoint,
+							parent = parent,
 							instantiateDelegate = System.Delegate.CreateDelegate(typeof(EditorInstantiation.InstantiateDelegate), graphicInstantiateDelegate) as EditorInstantiation.InstantiateDelegate,
 							isUI = true
 						});
@@ -120,7 +123,9 @@ namespace Spine.Unity.Editor {
 				menu.AddItem(new GUIContent("SkeletonMecanim"), false, HandleSkeletonComponentDrop, new SpawnMenuData {
 					skeletonDataAsset = skeletonDataAsset,
 					spawnPoint = spawnPoint,
-					instantiateDelegate = (data) => EditorInstantiation.InstantiateSkeletonMecanim(data)
+					parent = parent,
+					instantiateDelegate = (data) => EditorInstantiation.InstantiateSkeletonMecanim(data),
+					isUI = false
 				});
 #endif
 
@@ -141,16 +146,21 @@ namespace Spine.Unity.Editor {
 				GameObject newGameObject = newSkeletonComponent.gameObject;
 				Transform newTransform = newGameObject.transform;
 
-				var activeGameObject = Selection.activeGameObject;
-				if (isUI && activeGameObject != null)
-					newTransform.SetParent(activeGameObject.transform, false);
+				var usedParent = data.parent != null ? data.parent.gameObject : isUI ? Selection.activeGameObject : null;
+				if (usedParent)
+					newTransform.SetParent(usedParent.transform, false);
 
 				newTransform.position = isUI ? data.spawnPoint : RoundVector(data.spawnPoint, 2);
 
-				if (isUI && (activeGameObject == null || activeGameObject.GetComponent<RectTransform>() == null))
-					Debug.Log("Created a UI Skeleton GameObject not under a RectTransform. It may not be visible until you parent it to a canvas.");
+				if (isUI) {
+					if (usedParent != null && usedParent.GetComponent<RectTransform>() != null) {
+						((SkeletonGraphic)newSkeletonComponent).MatchRectTransformWithBounds();
+					}
+					else
+						Debug.Log("Created a UI Skeleton GameObject not under a RectTransform. It may not be visible until you parent it to a canvas.");
+				}
 
-				if (!isUI && activeGameObject != null && activeGameObject.transform.localScale != Vector3.one)
+				if (!isUI && usedParent != null && usedParent.transform.localScale != Vector3.one)
 					Debug.Log("New Spine GameObject was parented to a scaled Transform. It may not be the intended size.");
 
 				Selection.activeGameObject = newGameObject;

+ 10 - 2
spine-unity/Assets/Spine/Editor/spine-unity/Editor/Utility/SpineEditorUtilities.cs

@@ -299,6 +299,7 @@ namespace Spine.Unity.Editor {
 				var eventType = current.type;
 				bool isDraggingEvent = eventType == EventType.DragUpdated;
 				bool isDropEvent = eventType == EventType.DragPerform;
+
 				if (isDraggingEvent || isDropEvent) {
 					var mouseOverWindow = EditorWindow.mouseOverWindow;
 					if (mouseOverWindow != null) {
@@ -312,12 +313,19 @@ namespace Spine.Unity.Editor {
 								// Allow drag-and-dropping anywhere in the Hierarchy Window.
 								// HACK: string-compare because we can't get its type via reflection.
 								const string HierarchyWindow = "UnityEditor.SceneHierarchyWindow";
+								const string GenericDataTargetID = "target";
 								if (HierarchyWindow.Equals(mouseOverWindow.GetType().ToString(), System.StringComparison.Ordinal)) {
 									if (isDraggingEvent) {
 										UnityEditor.DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
-										current.Use();
+
+										var mouseOverTarget = UnityEditor.EditorUtility.InstanceIDToObject(instanceId);
+										if (mouseOverTarget)
+											DragAndDrop.SetGenericData(GenericDataTargetID, mouseOverTarget);
+										// note: do not use the current event, otherwise we lose the nice mouse-over highlighting.
 									} else if (isDropEvent) {
-										DragAndDropInstantiation.ShowInstantiateContextMenu(skeletonDataAsset, Vector3.zero);
+										var parentGameObject = DragAndDrop.GetGenericData(GenericDataTargetID) as UnityEngine.GameObject;
+										Transform parent = parentGameObject != null ? parentGameObject.transform : null;
+										DragAndDropInstantiation.ShowInstantiateContextMenu(skeletonDataAsset, Vector3.zero, parent);
 										UnityEditor.DragAndDrop.AcceptDrag();
 										current.Use();
 										return;

+ 28 - 0
spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs

@@ -239,6 +239,34 @@ namespace Spine.Unity {
 			return meshBuffers.GetCurrent().mesh;
 		}
 
+		public bool MatchRectTransformWithBounds () {
+			UpdateMesh();
+
+			Mesh mesh = this.GetLastMesh();
+			if (mesh == null) {
+				return false;
+			}
+
+			if (mesh.vertexCount == 0) {
+				this.rectTransform.sizeDelta = new Vector2(50f, 50f);
+				this.rectTransform.pivot = new Vector2(0.5f, 0.5f);
+				return false;
+			}
+
+			mesh.RecalculateBounds();
+			var bounds = mesh.bounds;
+			var size = bounds.size;
+			var center = bounds.center;
+			var p = new Vector2(
+				0.5f - (center.x / size.x),
+				0.5f - (center.y / size.y)
+			);
+
+			this.rectTransform.sizeDelta = size;
+			this.rectTransform.pivot = p;
+			return true;
+		}
+
 		public event UpdateBonesDelegate UpdateLocal;
 		public event UpdateBonesDelegate UpdateWorld;
 		public event UpdateBonesDelegate UpdateComplete;