[unity] Fixed drag and drop instantiation for Unity 2021.2 and newer (which provides a new callback method). Closes #2077.

This commit is contained in:
Harald Csaszar 2022-05-06 15:12:14 +02:00
parent 362a9fcd20
commit 29e0e9172b
2 changed files with 41 additions and 4 deletions

View File

@ -48,6 +48,7 @@ namespace Spine.Unity.Editor {
public struct SpawnMenuData {
public Vector3 spawnPoint;
public Transform parent;
public int siblingIndex;
public SkeletonDataAsset skeletonDataAsset;
public EditorInstantiation.InstantiateDelegate instantiateDelegate;
public bool isUI;
@ -82,7 +83,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, null);
ShowInstantiateContextMenu(skeletonDataAsset, spawnPoint, null, 0);
DragAndDrop.AcceptDrag();
current.Use();
}
@ -91,7 +92,8 @@ namespace Spine.Unity.Editor {
}
}
public static void ShowInstantiateContextMenu (SkeletonDataAsset skeletonDataAsset, Vector3 spawnPoint, Transform parent) {
public static void ShowInstantiateContextMenu (SkeletonDataAsset skeletonDataAsset, Vector3 spawnPoint,
Transform parent, int siblingIndex = 0) {
var menu = new GenericMenu();
// SkeletonAnimation
@ -99,6 +101,7 @@ namespace Spine.Unity.Editor {
skeletonDataAsset = skeletonDataAsset,
spawnPoint = spawnPoint,
parent = parent,
siblingIndex = siblingIndex,
instantiateDelegate = (data) => EditorInstantiation.InstantiateSkeletonAnimation(data),
isUI = false
});
@ -112,6 +115,7 @@ namespace Spine.Unity.Editor {
skeletonDataAsset = skeletonDataAsset,
spawnPoint = spawnPoint,
parent = parent,
siblingIndex = siblingIndex,
instantiateDelegate = System.Delegate.CreateDelegate(typeof(EditorInstantiation.InstantiateDelegate), graphicInstantiateDelegate) as EditorInstantiation.InstantiateDelegate,
isUI = true
});
@ -124,6 +128,7 @@ namespace Spine.Unity.Editor {
skeletonDataAsset = skeletonDataAsset,
spawnPoint = spawnPoint,
parent = parent,
siblingIndex = siblingIndex,
instantiateDelegate = (data) => EditorInstantiation.InstantiateSkeletonMecanim(data),
isUI = false
});
@ -149,6 +154,8 @@ namespace Spine.Unity.Editor {
var usedParent = data.parent != null ? data.parent.gameObject : isUI ? Selection.activeGameObject : null;
if (usedParent)
newTransform.SetParent(usedParent.transform, false);
if (data.siblingIndex != 0)
newTransform.SetSiblingIndex(data.siblingIndex);
newTransform.position = isUI ? data.spawnPoint : RoundVector(data.spawnPoint, 2);

View File

@ -194,9 +194,13 @@ namespace Spine.Unity.Editor {
SceneView.onSceneGUIDelegate += DragAndDropInstantiation.SceneViewDragAndDrop;
#endif
#if UNITY_2021_2_OR_NEWER
DragAndDrop.RemoveDropHandler(HierarchyHandler.HandleDragAndDrop);
DragAndDrop.AddDropHandler(HierarchyHandler.HandleDragAndDrop);
#else
EditorApplication.hierarchyWindowItemOnGUI -= HierarchyHandler.HandleDragAndDrop;
EditorApplication.hierarchyWindowItemOnGUI += HierarchyHandler.HandleDragAndDrop;
#endif
// Hierarchy Icons
#if NEWPLAYMODECALLBACKS
EditorApplication.playModeStateChanged -= HierarchyHandler.IconsOnPlaymodeStateChanged;
@ -440,6 +444,31 @@ namespace Spine.Unity.Editor {
}
}
#if UNITY_2021_2_OR_NEWER
internal static DragAndDropVisualMode HandleDragAndDrop (int dropTargetInstanceID, HierarchyDropFlags dropMode, Transform parentForDraggedObjects, bool perform) {
if (!perform || DragAndDrop.objectReferences.Length == 0)
return DragAndDropVisualMode.Copy;
SkeletonDataAsset skeletonDataAsset = DragAndDrop.objectReferences[0] as SkeletonDataAsset;
if (skeletonDataAsset == null)
return DragAndDropVisualMode.Copy;
GameObject dropTargetObject = UnityEditor.EditorUtility.InstanceIDToObject(dropTargetInstanceID) as GameObject;
Transform dropTarget = dropTargetObject != null ? dropTargetObject.transform : null;
Transform parent = dropTarget;
int siblingIndex = 0;
if (parent != null) {
if (dropMode == HierarchyDropFlags.DropBetween) {
parent = dropTarget.parent;
siblingIndex = dropTarget ? dropTarget.GetSiblingIndex() + 1 : 0;
} else if (dropMode == HierarchyDropFlags.DropAbove) {
parent = dropTarget.parent;
siblingIndex = dropTarget ? dropTarget.GetSiblingIndex() : 0;
}
}
DragAndDropInstantiation.ShowInstantiateContextMenu(skeletonDataAsset, Vector3.zero, parent, siblingIndex);
return DragAndDropVisualMode.Copy;
}
#else
internal static void HandleDragAndDrop (int instanceId, Rect selectionRect) {
// HACK: Uses EditorApplication.hierarchyWindowItemOnGUI.
// Only works when there is at least one item in the scene.
@ -475,7 +504,7 @@ namespace Spine.Unity.Editor {
// when dragging into empty space in hierarchy below last node, last node would be parent.
if (IsLastNodeInHierarchy(parent))
parent = null;
DragAndDropInstantiation.ShowInstantiateContextMenu(skeletonDataAsset, Vector3.zero, parent);
DragAndDropInstantiation.ShowInstantiateContextMenu(skeletonDataAsset, Vector3.zero, parent, 0);
UnityEditor.DragAndDrop.AcceptDrag();
current.Use();
return;
@ -501,6 +530,7 @@ namespace Spine.Unity.Editor {
bool isLastNode = (rootNodes.Length > 0 && rootNodes[rootNodes.Length - 1].transform == node);
return isLastNode;
}
#endif
}
}