A game about forced loneliness, made by TACStudios
at master 139 lines 5.6 kB view raw
1using UnityEngine; 2using UnityEngine.U2D.Animation; 3 4namespace UnityEditor.U2D.Animation 5{ 6 [InitializeOnLoad] 7 internal static class SpriteLibraryAssetDragAndDrop 8 { 9 const string k_UndoableCreate = "Create new Sprite Library Object"; 10 const string k_UndoableAdd = "Add Sprite Library"; 11 12 static SpriteLibraryAssetDragAndDrop() 13 { 14 DragAndDrop.AddDropHandler(HandleDropInspector); 15 DragAndDrop.AddDropHandler(HandleDropHierarchy); 16 DragAndDrop.AddDropHandler(HandleDropScene); 17 } 18 19 static DragAndDropVisualMode HandleDropInspector(Object[] targets, bool perform) 20 { 21 return HandleDropInspectorInternal(DragAndDrop.objectReferences, targets, perform); 22 } 23 24 static DragAndDropVisualMode HandleDropHierarchy(int dropTargetInstanceID, HierarchyDropFlags dropMode, Transform parentForDraggedObjects, bool perform) 25 { 26 return HandleDropHierarchyInternal(DragAndDrop.objectReferences, dropTargetInstanceID, dropMode, perform); 27 } 28 29 static DragAndDropVisualMode HandleDropScene(Object dropUpon, Vector3 worldPosition, Vector2 viewportPosition, Transform parentForDraggedObjects, bool perform) 30 { 31 return HandleDropSceneInternal(DragAndDrop.objectReferences, dropUpon, worldPosition, perform); 32 } 33 34 internal static DragAndDropVisualMode HandleDropInspectorInternal(Object[] draggedObjects, Object[] targets, bool perform) 35 { 36 var spriteLibraryAsset = GetSpriteLibraryAsset(draggedObjects); 37 if (spriteLibraryAsset == null) 38 return DragAndDropVisualMode.None; 39 40 DragAndDrop.AcceptDrag(); 41 if (perform) 42 { 43 for (var i = 0; i < targets.Length; i++) 44 { 45 if (targets[i] is GameObject targetGo) 46 AddSpriteLibraryToObject(targetGo, spriteLibraryAsset); 47 } 48 } 49 50 return DragAndDropVisualMode.Copy; 51 } 52 53 internal static DragAndDropVisualMode HandleDropHierarchyInternal(Object[] draggedObjects, int dropTargetInstanceID, HierarchyDropFlags dropMode, bool perform) 54 { 55 var spriteLibraryAsset = GetSpriteLibraryAsset(draggedObjects); 56 if (spriteLibraryAsset == null) 57 return DragAndDropVisualMode.None; 58 59 var dropUpon = EditorUtility.InstanceIDToObject(dropTargetInstanceID); 60 if (dropUpon == null || dropMode == HierarchyDropFlags.DropBetween) 61 { 62 DragAndDrop.AcceptDrag(); 63 if (perform) 64 CreateSpriteLibraryObject(spriteLibraryAsset, Vector3.zero); 65 66 return DragAndDropVisualMode.Copy; 67 } 68 69 if (dropUpon is GameObject targetGo) 70 { 71 DragAndDrop.AcceptDrag(); 72 if (perform) 73 AddSpriteLibraryToObject(targetGo, spriteLibraryAsset); 74 75 return DragAndDropVisualMode.Link; 76 } 77 78 return DragAndDropVisualMode.None; 79 } 80 81 internal static DragAndDropVisualMode HandleDropSceneInternal(Object[] draggedObjects, Object dropUpon, Vector3 worldPosition, bool perform) 82 { 83 var spriteLibraryAsset = GetSpriteLibraryAsset(draggedObjects); 84 if (spriteLibraryAsset == null) 85 return DragAndDropVisualMode.None; 86 87 DragAndDrop.AcceptDrag(); 88 89 if (dropUpon is GameObject targetGo) 90 { 91 if (perform) 92 AddSpriteLibraryToObject(targetGo, spriteLibraryAsset); 93 94 return DragAndDropVisualMode.Link; 95 } 96 97 if (perform) 98 CreateSpriteLibraryObject(spriteLibraryAsset, worldPosition); 99 100 return DragAndDropVisualMode.Copy; 101 } 102 103 internal static SpriteLibraryAsset GetSpriteLibraryAsset(Object[] objectReferences) 104 { 105 for (var i = 0; i < objectReferences.Length; i++) 106 { 107 var draggedObject = objectReferences[i]; 108 if (draggedObject is SpriteLibraryAsset spriteLibraryAsset) 109 return spriteLibraryAsset; 110 } 111 112 return null; 113 } 114 115 116 internal static void AddSpriteLibraryToObject(GameObject targetGo, SpriteLibraryAsset spriteLibraryAsset) 117 { 118 Undo.RegisterFullObjectHierarchyUndo(targetGo, k_UndoableAdd); 119 var spriteLibraryComponent = targetGo.GetComponent<SpriteLibrary>(); 120 if (spriteLibraryComponent == null) 121 spriteLibraryComponent = targetGo.AddComponent<SpriteLibrary>(); 122 spriteLibraryComponent.spriteLibraryAsset = spriteLibraryAsset; 123 124 Selection.objects = new Object[] { targetGo }; 125 } 126 127 internal static void CreateSpriteLibraryObject(SpriteLibraryAsset spriteLibraryAsset, Vector3 position) 128 { 129 var newSpriteLibraryGameObject = new GameObject(spriteLibraryAsset.name); 130 var transform = newSpriteLibraryGameObject.transform; 131 transform.position = position; 132 var spriteLibraryComponent = newSpriteLibraryGameObject.AddComponent<SpriteLibrary>(); 133 spriteLibraryComponent.spriteLibraryAsset = spriteLibraryAsset; 134 Undo.RegisterCreatedObjectUndo(newSpriteLibraryGameObject, k_UndoableCreate); 135 136 Selection.objects = new Object[] { newSpriteLibraryGameObject }; 137 } 138 } 139}