MenuOptions.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. using System;
  2. using UnityEditor.SceneManagement;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. using UnityEngine.SceneManagement;
  7. using UnityEditor.Experimental.SceneManagement;
  8. namespace UnityEditor.UI
  9. {
  10. /// <summary>
  11. /// This script adds the UI menu options to the Unity Editor.
  12. /// </summary>
  13. static internal class MenuOptions
  14. {
  15. private const string kUILayerName = "UI";
  16. private const string kStandardSpritePath = "UI/Skin/UISprite.psd";
  17. private const string kBackgroundSpritePath = "UI/Skin/Background.psd";
  18. private const string kInputFieldBackgroundPath = "UI/Skin/InputFieldBackground.psd";
  19. private const string kKnobPath = "UI/Skin/Knob.psd";
  20. private const string kCheckmarkPath = "UI/Skin/Checkmark.psd";
  21. private const string kDropdownArrowPath = "UI/Skin/DropdownArrow.psd";
  22. private const string kMaskPath = "UI/Skin/UIMask.psd";
  23. static private DefaultControls.Resources s_StandardResources;
  24. static private DefaultControls.Resources GetStandardResources()
  25. {
  26. if (s_StandardResources.standard == null)
  27. {
  28. s_StandardResources.standard = AssetDatabase.GetBuiltinExtraResource<Sprite>(kStandardSpritePath);
  29. s_StandardResources.background = AssetDatabase.GetBuiltinExtraResource<Sprite>(kBackgroundSpritePath);
  30. s_StandardResources.inputField = AssetDatabase.GetBuiltinExtraResource<Sprite>(kInputFieldBackgroundPath);
  31. s_StandardResources.knob = AssetDatabase.GetBuiltinExtraResource<Sprite>(kKnobPath);
  32. s_StandardResources.checkmark = AssetDatabase.GetBuiltinExtraResource<Sprite>(kCheckmarkPath);
  33. s_StandardResources.dropdown = AssetDatabase.GetBuiltinExtraResource<Sprite>(kDropdownArrowPath);
  34. s_StandardResources.mask = AssetDatabase.GetBuiltinExtraResource<Sprite>(kMaskPath);
  35. }
  36. return s_StandardResources;
  37. }
  38. private class DefaultEditorFactory : DefaultControls.IFactoryControls
  39. {
  40. public static DefaultEditorFactory Default = new DefaultEditorFactory();
  41. public GameObject CreateGameObject(string name, params Type[] components)
  42. {
  43. return ObjectFactory.CreateGameObject(name, components);
  44. }
  45. }
  46. private class FactorySwapToEditor : IDisposable
  47. {
  48. DefaultControls.IFactoryControls factory;
  49. public FactorySwapToEditor()
  50. {
  51. factory = DefaultControls.factory;
  52. DefaultControls.factory = DefaultEditorFactory.Default;
  53. }
  54. public void Dispose()
  55. {
  56. DefaultControls.factory = factory;
  57. }
  58. }
  59. private static void SetPositionVisibleinSceneView(RectTransform canvasRTransform, RectTransform itemTransform)
  60. {
  61. SceneView sceneView = SceneView.lastActiveSceneView;
  62. // Couldn't find a SceneView. Don't set position.
  63. if (sceneView == null || sceneView.camera == null)
  64. return;
  65. // Create world space Plane from canvas position.
  66. Vector2 localPlanePosition;
  67. Camera camera = sceneView.camera;
  68. Vector3 position = Vector3.zero;
  69. if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRTransform, new Vector2(camera.pixelWidth / 2, camera.pixelHeight / 2), camera, out localPlanePosition))
  70. {
  71. // Adjust for canvas pivot
  72. localPlanePosition.x = localPlanePosition.x + canvasRTransform.sizeDelta.x * canvasRTransform.pivot.x;
  73. localPlanePosition.y = localPlanePosition.y + canvasRTransform.sizeDelta.y * canvasRTransform.pivot.y;
  74. localPlanePosition.x = Mathf.Clamp(localPlanePosition.x, 0, canvasRTransform.sizeDelta.x);
  75. localPlanePosition.y = Mathf.Clamp(localPlanePosition.y, 0, canvasRTransform.sizeDelta.y);
  76. // Adjust for anchoring
  77. position.x = localPlanePosition.x - canvasRTransform.sizeDelta.x * itemTransform.anchorMin.x;
  78. position.y = localPlanePosition.y - canvasRTransform.sizeDelta.y * itemTransform.anchorMin.y;
  79. Vector3 minLocalPosition;
  80. minLocalPosition.x = canvasRTransform.sizeDelta.x * (0 - canvasRTransform.pivot.x) + itemTransform.sizeDelta.x * itemTransform.pivot.x;
  81. minLocalPosition.y = canvasRTransform.sizeDelta.y * (0 - canvasRTransform.pivot.y) + itemTransform.sizeDelta.y * itemTransform.pivot.y;
  82. Vector3 maxLocalPosition;
  83. maxLocalPosition.x = canvasRTransform.sizeDelta.x * (1 - canvasRTransform.pivot.x) - itemTransform.sizeDelta.x * itemTransform.pivot.x;
  84. maxLocalPosition.y = canvasRTransform.sizeDelta.y * (1 - canvasRTransform.pivot.y) - itemTransform.sizeDelta.y * itemTransform.pivot.y;
  85. position.x = Mathf.Clamp(position.x, minLocalPosition.x, maxLocalPosition.x);
  86. position.y = Mathf.Clamp(position.y, minLocalPosition.y, maxLocalPosition.y);
  87. }
  88. itemTransform.anchoredPosition = position;
  89. itemTransform.localRotation = Quaternion.identity;
  90. itemTransform.localScale = Vector3.one;
  91. }
  92. private static void PlaceUIElementRoot(GameObject element, MenuCommand menuCommand)
  93. {
  94. GameObject parent = menuCommand.context as GameObject;
  95. bool explicitParentChoice = true;
  96. if (parent == null)
  97. {
  98. parent = GetOrCreateCanvasGameObject();
  99. explicitParentChoice = false;
  100. // If in Prefab Mode, Canvas has to be part of Prefab contents,
  101. // otherwise use Prefab root instead.
  102. PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
  103. if (prefabStage != null && !prefabStage.IsPartOfPrefabContents(parent))
  104. parent = prefabStage.prefabContentsRoot;
  105. }
  106. if (parent.GetComponentsInParent<Canvas>(true).Length == 0)
  107. {
  108. // Create canvas under context GameObject,
  109. // and make that be the parent which UI element is added under.
  110. GameObject canvas = MenuOptions.CreateNewUI();
  111. Undo.SetTransformParent(canvas.transform, parent.transform, "");
  112. parent = canvas;
  113. }
  114. GameObjectUtility.EnsureUniqueNameForSibling(element);
  115. SetParentAndAlign(element, parent);
  116. if (!explicitParentChoice) // not a context click, so center in sceneview
  117. SetPositionVisibleinSceneView(parent.GetComponent<RectTransform>(), element.GetComponent<RectTransform>());
  118. // This call ensure any change made to created Objects after they where registered will be part of the Undo.
  119. Undo.RegisterFullObjectHierarchyUndo(parent == null ? element : parent, "");
  120. // We have to fix up the undo name since the name of the object was only known after reparenting it.
  121. Undo.SetCurrentGroupName("Create " + element.name);
  122. Selection.activeGameObject = element;
  123. }
  124. private static void SetParentAndAlign(GameObject child, GameObject parent)
  125. {
  126. if (parent == null)
  127. return;
  128. Undo.SetTransformParent(child.transform, parent.transform, "");
  129. RectTransform rectTransform = child.transform as RectTransform;
  130. if (rectTransform)
  131. {
  132. rectTransform.anchoredPosition = Vector2.zero;
  133. Vector3 localPosition = rectTransform.localPosition;
  134. localPosition.z = 0;
  135. rectTransform.localPosition = localPosition;
  136. }
  137. else
  138. {
  139. child.transform.localPosition = Vector3.zero;
  140. }
  141. child.transform.localRotation = Quaternion.identity;
  142. child.transform.localScale = Vector3.one;
  143. SetLayerRecursively(child, parent.layer);
  144. }
  145. private static void SetLayerRecursively(GameObject go, int layer)
  146. {
  147. go.layer = layer;
  148. Transform t = go.transform;
  149. for (int i = 0; i < t.childCount; i++)
  150. SetLayerRecursively(t.GetChild(i).gameObject, layer);
  151. }
  152. // Graphic elements
  153. [MenuItem("GameObject/UI/Text", false, 2000)]
  154. static public void AddText(MenuCommand menuCommand)
  155. {
  156. GameObject go;
  157. using (new FactorySwapToEditor())
  158. go = DefaultControls.CreateText(GetStandardResources());
  159. PlaceUIElementRoot(go, menuCommand);
  160. }
  161. [MenuItem("GameObject/UI/Image", false, 2001)]
  162. static public void AddImage(MenuCommand menuCommand)
  163. {
  164. GameObject go;
  165. using (new FactorySwapToEditor())
  166. go = DefaultControls.CreateImage(GetStandardResources());
  167. PlaceUIElementRoot(go, menuCommand);
  168. }
  169. [MenuItem("GameObject/UI/Raw Image", false, 2002)]
  170. static public void AddRawImage(MenuCommand menuCommand)
  171. {
  172. GameObject go;
  173. using (new FactorySwapToEditor())
  174. go = DefaultControls.CreateRawImage(GetStandardResources());
  175. PlaceUIElementRoot(go, menuCommand);
  176. }
  177. // Controls
  178. // Button and toggle are controls you just click on.
  179. [MenuItem("GameObject/UI/Button", false, 2030)]
  180. static public void AddButton(MenuCommand menuCommand)
  181. {
  182. GameObject go;
  183. using (new FactorySwapToEditor())
  184. go = DefaultControls.CreateButton(GetStandardResources());
  185. PlaceUIElementRoot(go, menuCommand);
  186. }
  187. [MenuItem("GameObject/UI/Toggle", false, 2031)]
  188. static public void AddToggle(MenuCommand menuCommand)
  189. {
  190. GameObject go;
  191. using (new FactorySwapToEditor())
  192. go = DefaultControls.CreateToggle(GetStandardResources());
  193. PlaceUIElementRoot(go, menuCommand);
  194. }
  195. // Slider and Scrollbar modify a number
  196. [MenuItem("GameObject/UI/Slider", false, 2033)]
  197. static public void AddSlider(MenuCommand menuCommand)
  198. {
  199. GameObject go;
  200. using (new FactorySwapToEditor())
  201. go = DefaultControls.CreateSlider(GetStandardResources());
  202. PlaceUIElementRoot(go, menuCommand);
  203. }
  204. [MenuItem("GameObject/UI/Scrollbar", false, 2034)]
  205. static public void AddScrollbar(MenuCommand menuCommand)
  206. {
  207. GameObject go;
  208. using (new FactorySwapToEditor())
  209. go = DefaultControls.CreateScrollbar(GetStandardResources());
  210. PlaceUIElementRoot(go, menuCommand);
  211. }
  212. // More advanced controls below
  213. [MenuItem("GameObject/UI/Dropdown", false, 2035)]
  214. static public void AddDropdown(MenuCommand menuCommand)
  215. {
  216. GameObject go;
  217. using (new FactorySwapToEditor())
  218. go = DefaultControls.CreateDropdown(GetStandardResources());
  219. PlaceUIElementRoot(go, menuCommand);
  220. }
  221. [MenuItem("GameObject/UI/Input Field", false, 2036)]
  222. public static void AddInputField(MenuCommand menuCommand)
  223. {
  224. GameObject go;
  225. using (new FactorySwapToEditor())
  226. go = DefaultControls.CreateInputField(GetStandardResources());
  227. PlaceUIElementRoot(go, menuCommand);
  228. }
  229. // Containers
  230. [MenuItem("GameObject/UI/Canvas", false, 2060)]
  231. static public void AddCanvas(MenuCommand menuCommand)
  232. {
  233. var go = CreateNewUI();
  234. SetParentAndAlign(go, menuCommand.context as GameObject);
  235. if (go.transform.parent as RectTransform)
  236. {
  237. RectTransform rect = go.transform as RectTransform;
  238. rect.anchorMin = Vector2.zero;
  239. rect.anchorMax = Vector2.one;
  240. rect.anchoredPosition = Vector2.zero;
  241. rect.sizeDelta = Vector2.zero;
  242. }
  243. Selection.activeGameObject = go;
  244. }
  245. [MenuItem("GameObject/UI/Panel", false, 2061)]
  246. static public void AddPanel(MenuCommand menuCommand)
  247. {
  248. GameObject go;
  249. using (new FactorySwapToEditor())
  250. go = DefaultControls.CreatePanel(GetStandardResources());
  251. PlaceUIElementRoot(go, menuCommand);
  252. // Panel is special, we need to ensure there's no padding after repositioning.
  253. RectTransform rect = go.GetComponent<RectTransform>();
  254. rect.anchoredPosition = Vector2.zero;
  255. rect.sizeDelta = Vector2.zero;
  256. }
  257. [MenuItem("GameObject/UI/Scroll View", false, 2062)]
  258. static public void AddScrollView(MenuCommand menuCommand)
  259. {
  260. GameObject go;
  261. using (new FactorySwapToEditor())
  262. go = DefaultControls.CreateScrollView(GetStandardResources());
  263. PlaceUIElementRoot(go, menuCommand);
  264. }
  265. // Helper methods
  266. static public GameObject CreateNewUI()
  267. {
  268. // Root for the UI
  269. var root = ObjectFactory.CreateGameObject("Canvas", typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster));
  270. root.layer = LayerMask.NameToLayer(kUILayerName);
  271. Canvas canvas = root.GetComponent<Canvas>();
  272. canvas.renderMode = RenderMode.ScreenSpaceOverlay;
  273. // Works for all stages.
  274. StageUtility.PlaceGameObjectInCurrentStage(root);
  275. bool customScene = false;
  276. PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
  277. if (prefabStage != null)
  278. {
  279. Undo.SetTransformParent(root.transform, prefabStage.prefabContentsRoot.transform, "");
  280. customScene = true;
  281. }
  282. Undo.SetCurrentGroupName("Create " + root.name);
  283. // If there is no event system add one...
  284. // No need to place event system in custom scene as these are temporary anyway.
  285. // It can be argued for or against placing it in the user scenes,
  286. // but let's not modify scene user is not currently looking at.
  287. if (!customScene)
  288. CreateEventSystem(false);
  289. return root;
  290. }
  291. [MenuItem("GameObject/UI/Event System", false, 2100)]
  292. public static void CreateEventSystem(MenuCommand menuCommand)
  293. {
  294. GameObject parent = menuCommand.context as GameObject;
  295. CreateEventSystem(true, parent);
  296. }
  297. private static void CreateEventSystem(bool select)
  298. {
  299. CreateEventSystem(select, null);
  300. }
  301. private static void CreateEventSystem(bool select, GameObject parent)
  302. {
  303. StageHandle stage = parent == null ? StageUtility.GetCurrentStageHandle() : StageUtility.GetStageHandle(parent);
  304. var esys = stage.FindComponentOfType<EventSystem>();
  305. if (esys == null)
  306. {
  307. var eventSystem = ObjectFactory.CreateGameObject("EventSystem");
  308. if (parent == null)
  309. StageUtility.PlaceGameObjectInCurrentStage(eventSystem);
  310. else
  311. SetParentAndAlign(eventSystem, parent);
  312. esys = ObjectFactory.AddComponent<EventSystem>(eventSystem);
  313. ObjectFactory.AddComponent<StandaloneInputModule>(eventSystem);
  314. Undo.RegisterCreatedObjectUndo(eventSystem, "Create " + eventSystem.name);
  315. }
  316. if (select && esys != null)
  317. {
  318. Selection.activeGameObject = esys.gameObject;
  319. }
  320. }
  321. // Helper function that returns a Canvas GameObject; preferably a parent of the selection, or other existing Canvas.
  322. static public GameObject GetOrCreateCanvasGameObject()
  323. {
  324. GameObject selectedGo = Selection.activeGameObject;
  325. // Try to find a gameobject that is the selected GO or one if its parents.
  326. Canvas canvas = (selectedGo != null) ? selectedGo.GetComponentInParent<Canvas>() : null;
  327. if (IsValidCanvas(canvas))
  328. return canvas.gameObject;
  329. // No canvas in selection or its parents? Then use any valid canvas.
  330. // We have to find all loaded Canvases, not just the ones in main scenes.
  331. Canvas[] canvasArray = StageUtility.GetCurrentStageHandle().FindComponentsOfType<Canvas>();
  332. for (int i = 0; i < canvasArray.Length; i++)
  333. if (IsValidCanvas(canvasArray[i]))
  334. return canvasArray[i].gameObject;
  335. // No canvas in the scene at all? Then create a new one.
  336. return MenuOptions.CreateNewUI();
  337. }
  338. static bool IsValidCanvas(Canvas canvas)
  339. {
  340. if (canvas == null || !canvas.gameObject.activeInHierarchy)
  341. return false;
  342. // It's important that the non-editable canvas from a prefab scene won't be rejected,
  343. // but canvases not visible in the Hierarchy at all do. Don't check for HideAndDontSave.
  344. if (EditorUtility.IsPersistent(canvas) || (canvas.hideFlags & HideFlags.HideInHierarchy) != 0)
  345. return false;
  346. return StageUtility.GetStageHandle(canvas.gameObject) == StageUtility.GetCurrentStageHandle();
  347. }
  348. }
  349. }