PageRenderTime 77ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/VoteMario/Library/PackageCache/com.unity.2d.spriteshape@6.0.1/Editor/SceneDragAndDrop.cs

https://gitlab.com/Surabhi124/vote-mario
C# | 244 lines | 205 code | 36 blank | 3 comment | 45 complexity | 985f4f89f0c84356bbd12aeb2bc4fe7c MD5 | raw file
  1. using UnityEngine;
  2. using UnityEngine.U2D;
  3. using System.Collections.Generic;
  4. using UnityEditor.Experimental.SceneManagement;
  5. using UnityEditor.U2D.SpriteShape;
  6. using Object = UnityEngine.Object;
  7. namespace UnityEditor.U2D
  8. {
  9. [InitializeOnLoad]
  10. public static class SceneDragAndDrop
  11. {
  12. static SceneDragAndDrop()
  13. {
  14. #if UNITY_2019_1_OR_NEWER
  15. SceneView.duringSceneGui += OnSceneGUI;
  16. EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
  17. #else
  18. SceneView.onSceneGUIDelegate += OnSceneGUI;
  19. #endif
  20. }
  21. static class Contents
  22. {
  23. public static readonly string createString = "Create Sprite Shape";
  24. }
  25. static List<Object> s_SceneDragObjects;
  26. static DragType s_DragType;
  27. enum DragType { NotInitialized, CreateMultiple }
  28. public delegate string ShowFileDialogDelegate(string title, string defaultName, string extension, string message, string defaultPath);
  29. static void OnSceneGUI(SceneView sceneView)
  30. {
  31. HandleSceneDrag(sceneView, Event.current, DragAndDrop.objectReferences, DragAndDrop.paths);
  32. }
  33. public static GameObject Create(UnityEngine.U2D.SpriteShape shape, Vector3 position, SceneView sceneView)
  34. {
  35. string name = string.IsNullOrEmpty(shape.name) ? "New SpriteShapeController" : shape.name;
  36. name = GameObjectUtility.GetUniqueNameForSibling(null, name);
  37. GameObject go = new GameObject(name);
  38. SpriteShapeController shapeController = go.AddComponent<SpriteShapeController>();
  39. shapeController.spriteShape = shape;
  40. go.transform.position = position;
  41. go.hideFlags = HideFlags.HideAndDontSave;
  42. return go;
  43. }
  44. static void OnHierarchyGUI(int instanceID, Rect rect)
  45. {
  46. HandleSceneDrag(null, Event.current, DragAndDrop.objectReferences, null);
  47. }
  48. static List<UnityEngine.U2D.SpriteShape> GetSpriteShapeFromPathsOrObjects(Object[] objects, string[] paths, EventType currentEventType)
  49. {
  50. List<UnityEngine.U2D.SpriteShape> result = new List<UnityEngine.U2D.SpriteShape>();
  51. foreach (Object obj in objects)
  52. {
  53. if (AssetDatabase.Contains(obj))
  54. {
  55. if (obj is UnityEngine.U2D.SpriteShape)
  56. result.Add(obj as UnityEngine.U2D.SpriteShape);
  57. }
  58. }
  59. return result;
  60. }
  61. static void HandleSceneDrag(SceneView sceneView, Event evt, Object[] objectReferences, string[] paths)
  62. {
  63. if (evt.type != EventType.DragUpdated && evt.type != EventType.DragPerform && evt.type != EventType.DragExited)
  64. return;
  65. switch (evt.type)
  66. {
  67. case EventType.DragUpdated:
  68. {
  69. DragType newDragType = DragType.CreateMultiple;
  70. if (s_DragType != newDragType || s_SceneDragObjects == null)
  71. // Either this is first time we are here OR evt.alt changed during drag
  72. {
  73. if (ExistingAssets(objectReferences)) // External drag with images that are not in the project
  74. {
  75. List<UnityEngine.U2D.SpriteShape> assets = GetSpriteShapeFromPathsOrObjects(objectReferences, paths,
  76. evt.type);
  77. if (assets.Count == 0)
  78. return;
  79. if (s_DragType != DragType.NotInitialized)
  80. // evt.alt changed during drag, so we need to cleanup and start over
  81. CleanUp(true);
  82. s_DragType = newDragType;
  83. CreateSceneDragObjects(assets);
  84. }
  85. }
  86. if (s_SceneDragObjects != null)
  87. {
  88. if (sceneView != null)
  89. PositionSceneDragObjects(s_SceneDragObjects, sceneView, evt.mousePosition);
  90. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  91. evt.Use();
  92. }
  93. }
  94. break;
  95. case EventType.DragPerform:
  96. {
  97. List<UnityEngine.U2D.SpriteShape> assets = GetSpriteShapeFromPathsOrObjects(objectReferences, paths, evt.type);
  98. if (assets.Count > 0 && s_SceneDragObjects != null)
  99. {
  100. // For external drags, we have delayed all creation to DragPerform because only now we have the imported sprite assets
  101. if (s_SceneDragObjects.Count == 0)
  102. {
  103. CreateSceneDragObjects(assets);
  104. if (sceneView != null)
  105. PositionSceneDragObjects(s_SceneDragObjects, sceneView, evt.mousePosition);
  106. }
  107. var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
  108. foreach (GameObject dragGO in s_SceneDragObjects)
  109. {
  110. if (prefabStage != null)
  111. {
  112. var parTransform = Selection.activeTransform != null
  113. ? Selection.activeTransform
  114. : prefabStage.prefabContentsRoot.transform;
  115. dragGO.transform.SetParent(parTransform, true);
  116. }
  117. Undo.RegisterCreatedObjectUndo(dragGO, "Create Shape");
  118. dragGO.hideFlags = HideFlags.None;
  119. }
  120. Selection.objects = s_SceneDragObjects.ToArray();
  121. CleanUp(false);
  122. evt.Use();
  123. }
  124. }
  125. break;
  126. case EventType.DragExited:
  127. {
  128. if (s_SceneDragObjects != null)
  129. {
  130. CleanUp(true);
  131. evt.Use();
  132. }
  133. }
  134. break;
  135. }
  136. }
  137. static void PositionSceneDragObjects(List<Object> objects, SceneView sceneView, Vector2 mousePosition)
  138. {
  139. Vector3 position = Vector3.zero;
  140. position = HandleUtility.GUIPointToWorldRay(mousePosition).GetPoint(10);
  141. if (sceneView.in2DMode)
  142. {
  143. position.z = 0f;
  144. }
  145. else
  146. {
  147. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  148. object hit = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(mousePosition));
  149. if (hit != null)
  150. {
  151. RaycastHit rh = (RaycastHit)hit;
  152. position = rh.point;
  153. }
  154. }
  155. foreach (GameObject gameObject in objects)
  156. {
  157. gameObject.transform.position = position;
  158. }
  159. }
  160. static void CreateSceneDragObjects(List<UnityEngine.U2D.SpriteShape> shapes)
  161. {
  162. if (s_SceneDragObjects == null)
  163. s_SceneDragObjects = new List<Object>();
  164. if (s_DragType == DragType.CreateMultiple)
  165. {
  166. foreach (UnityEngine.U2D.SpriteShape sprite in shapes)
  167. s_SceneDragObjects.Add(CreateDragGO(sprite, Vector3.zero));
  168. }
  169. else
  170. {
  171. s_SceneDragObjects.Add(CreateDragGO(shapes[0], Vector3.zero));
  172. }
  173. }
  174. static void CleanUp(bool deleteTempSceneObject)
  175. {
  176. if (deleteTempSceneObject)
  177. {
  178. foreach (GameObject gameObject in s_SceneDragObjects)
  179. Object.DestroyImmediate(gameObject, false);
  180. }
  181. if (s_SceneDragObjects != null)
  182. {
  183. s_SceneDragObjects.Clear();
  184. s_SceneDragObjects = null;
  185. }
  186. s_DragType = DragType.NotInitialized;
  187. }
  188. static bool ExistingAssets(Object[] objects)
  189. {
  190. foreach (Object obj in objects)
  191. {
  192. if (AssetDatabase.Contains(obj))
  193. return true;
  194. }
  195. return false;
  196. }
  197. static GameObject CreateDragGO(UnityEngine.U2D.SpriteShape spriteShape, Vector3 position)
  198. {
  199. SpriteShapeController spriteShapeController = SpriteShapeEditorUtility.CreateSpriteShapeController(spriteShape);
  200. GameObject gameObject = spriteShapeController.gameObject;
  201. gameObject.transform.position = position;
  202. gameObject.hideFlags = HideFlags.HideAndDontSave;
  203. spriteShapeController.spriteShape = spriteShape;
  204. SpriteShapeEditorUtility.SetShapeFromAsset(spriteShapeController);
  205. return gameObject;
  206. }
  207. }
  208. }