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

MenuOptions.cs 18KB

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