暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SpriteLibraryAssetDragAndDrop.cs 5.5KB

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