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

View File

@ -194,9 +194,13 @@ namespace Spine.Unity.Editor {
SceneView.onSceneGUIDelegate += DragAndDropInstantiation.SceneViewDragAndDrop; SceneView.onSceneGUIDelegate += DragAndDropInstantiation.SceneViewDragAndDrop;
#endif #endif
#if UNITY_2021_2_OR_NEWER
DragAndDrop.RemoveDropHandler(HierarchyHandler.HandleDragAndDrop);
DragAndDrop.AddDropHandler(HierarchyHandler.HandleDragAndDrop);
#else
EditorApplication.hierarchyWindowItemOnGUI -= HierarchyHandler.HandleDragAndDrop; EditorApplication.hierarchyWindowItemOnGUI -= HierarchyHandler.HandleDragAndDrop;
EditorApplication.hierarchyWindowItemOnGUI += HierarchyHandler.HandleDragAndDrop; EditorApplication.hierarchyWindowItemOnGUI += HierarchyHandler.HandleDragAndDrop;
#endif
// Hierarchy Icons // Hierarchy Icons
#if NEWPLAYMODECALLBACKS #if NEWPLAYMODECALLBACKS
EditorApplication.playModeStateChanged -= HierarchyHandler.IconsOnPlaymodeStateChanged; 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) { internal static void HandleDragAndDrop (int instanceId, Rect selectionRect) {
// HACK: Uses EditorApplication.hierarchyWindowItemOnGUI. // HACK: Uses EditorApplication.hierarchyWindowItemOnGUI.
// Only works when there is at least one item in the scene. // 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. // when dragging into empty space in hierarchy below last node, last node would be parent.
if (IsLastNodeInHierarchy(parent)) if (IsLastNodeInHierarchy(parent))
parent = null; parent = null;
DragAndDropInstantiation.ShowInstantiateContextMenu(skeletonDataAsset, Vector3.zero, parent); DragAndDropInstantiation.ShowInstantiateContextMenu(skeletonDataAsset, Vector3.zero, parent, 0);
UnityEditor.DragAndDrop.AcceptDrag(); UnityEditor.DragAndDrop.AcceptDrag();
current.Use(); current.Use();
return; return;
@ -501,6 +530,7 @@ namespace Spine.Unity.Editor {
bool isLastNode = (rootNodes.Length > 0 && rootNodes[rootNodes.Length - 1].transform == node); bool isLastNode = (rootNodes.Length > 0 && rootNodes[rootNodes.Length - 1].transform == node);
return isLastNode; return isLastNode;
} }
#endif
} }
} }