TMPro_CreateObjectMenu.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditor.Presets;
  4. using UnityEditor.SceneManagement;
  5. using UnityEditor.Experimental.SceneManagement;
  6. using UnityEngine.SceneManagement;
  7. using UnityEngine.UI;
  8. using UnityEngine.EventSystems;
  9. namespace TMPro.EditorUtilities
  10. {
  11. public static class TMPro_CreateObjectMenu
  12. {
  13. /// <summary>
  14. /// Create a TextMeshPro object that works with the Mesh Renderer
  15. /// </summary>
  16. /// <param name="command"></param>
  17. [MenuItem("GameObject/3D Object/Text - TextMeshPro", false, 30)]
  18. static void CreateTextMeshProObjectPerform(MenuCommand command)
  19. {
  20. GameObject go = ObjectFactory.CreateGameObject("Text (TMP)");
  21. // Add support for new prefab mode
  22. StageUtility.PlaceGameObjectInCurrentStage(go);
  23. TextMeshPro textComponent = ObjectFactory.AddComponent<TextMeshPro>(go);
  24. if (textComponent.m_isWaitingOnResourceLoad == false)
  25. {
  26. // Get reference to potential Presets for <TextMeshPro> component
  27. #if UNITY_2019_3_OR_NEWER
  28. Preset[] presets = Preset.GetDefaultPresetsForObject(textComponent);
  29. if (presets == null || presets.Length == 0)
  30. {
  31. textComponent.text = "Sample text";
  32. textComponent.alignment = TextAlignmentOptions.TopLeft;
  33. }
  34. else
  35. {
  36. textComponent.renderer.sortingLayerID = textComponent._SortingLayerID;
  37. textComponent.renderer.sortingOrder = textComponent._SortingOrder;
  38. }
  39. #else
  40. if (Preset.GetDefaultForObject(textComponent) == null)
  41. {
  42. textComponent.text = "Sample text";
  43. textComponent.alignment = TextAlignmentOptions.TopLeft;
  44. }
  45. else
  46. {
  47. textComponent.renderer.sortingLayerID = textComponent._SortingLayerID;
  48. textComponent.renderer.sortingOrder = textComponent._SortingOrder;
  49. }
  50. #endif
  51. if (TMP_Settings.autoSizeTextContainer)
  52. {
  53. Vector2 size = textComponent.GetPreferredValues(TMP_Math.FLOAT_MAX, TMP_Math.FLOAT_MAX);
  54. textComponent.rectTransform.sizeDelta = size;
  55. }
  56. else
  57. {
  58. textComponent.rectTransform.sizeDelta = TMP_Settings.defaultTextMeshProTextContainerSize;
  59. }
  60. }
  61. else
  62. {
  63. textComponent.text = "Sample text";
  64. textComponent.alignment = TextAlignmentOptions.TopLeft;
  65. }
  66. Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
  67. GameObject contextObject = command.context as GameObject;
  68. if (contextObject != null)
  69. {
  70. GameObjectUtility.SetParentAndAlign(go, contextObject);
  71. Undo.SetTransformParent(go.transform, contextObject.transform, "Parent " + go.name);
  72. }
  73. Selection.activeGameObject = go;
  74. }
  75. /// <summary>
  76. /// Create a TextMeshPro object that works with the CanvasRenderer
  77. /// </summary>
  78. /// <param name="command"></param>
  79. [MenuItem("GameObject/UI/Text - TextMeshPro", false, 2001)]
  80. static void CreateTextMeshProGuiObjectPerform(MenuCommand menuCommand)
  81. {
  82. GameObject go = TMP_DefaultControls.CreateText(GetStandardResources());
  83. // Override text color and font size
  84. TextMeshProUGUI textComponent = go.GetComponent<TextMeshProUGUI>();
  85. if (textComponent.m_isWaitingOnResourceLoad == false)
  86. {
  87. // Get reference to potential Presets for <TextMeshProUGUI> component
  88. #if UNITY_2019_3_OR_NEWER
  89. Preset[] presets = Preset.GetDefaultPresetsForObject(textComponent);
  90. if (presets == null || presets.Length == 0)
  91. {
  92. textComponent.fontSize = TMP_Settings.defaultFontSize;
  93. textComponent.color = Color.white;
  94. textComponent.text = "New Text";
  95. }
  96. #else
  97. if (Preset.GetDefaultForObject(textComponent) == null)
  98. {
  99. textComponent.fontSize = TMP_Settings.defaultFontSize;
  100. textComponent.color = Color.white;
  101. textComponent.text = "New Text";
  102. }
  103. #endif
  104. if (TMP_Settings.autoSizeTextContainer)
  105. {
  106. Vector2 size = textComponent.GetPreferredValues(TMP_Math.FLOAT_MAX, TMP_Math.FLOAT_MAX);
  107. textComponent.rectTransform.sizeDelta = size;
  108. }
  109. else
  110. {
  111. textComponent.rectTransform.sizeDelta = TMP_Settings.defaultTextMeshProUITextContainerSize;
  112. }
  113. }
  114. else
  115. {
  116. textComponent.fontSize = 36;
  117. textComponent.color = Color.white;
  118. textComponent.text = "New Text";
  119. }
  120. PlaceUIElementRoot(go, menuCommand);
  121. }
  122. [MenuItem("GameObject/UI/Button - TextMeshPro", false, 2031)]
  123. public static void AddButton(MenuCommand menuCommand)
  124. {
  125. GameObject go = TMP_DefaultControls.CreateButton(GetStandardResources());
  126. // Override font size
  127. TMP_Text textComponent = go.GetComponentInChildren<TMP_Text>();
  128. textComponent.fontSize = 24;
  129. PlaceUIElementRoot(go, menuCommand);
  130. }
  131. [MenuItem("GameObject/UI/Input Field - TextMeshPro", false, 2037)]
  132. static void AddTextMeshProInputField(MenuCommand menuCommand)
  133. {
  134. GameObject go = TMP_DefaultControls.CreateInputField(GetStandardResources());
  135. PlaceUIElementRoot(go, menuCommand);
  136. }
  137. [MenuItem("GameObject/UI/Dropdown - TextMeshPro", false, 2036)]
  138. public static void AddDropdown(MenuCommand menuCommand)
  139. {
  140. GameObject go = TMP_DefaultControls.CreateDropdown(GetStandardResources());
  141. PlaceUIElementRoot(go, menuCommand);
  142. }
  143. private const string kUILayerName = "UI";
  144. private const string kStandardSpritePath = "UI/Skin/UISprite.psd";
  145. private const string kBackgroundSpritePath = "UI/Skin/Background.psd";
  146. private const string kInputFieldBackgroundPath = "UI/Skin/InputFieldBackground.psd";
  147. private const string kKnobPath = "UI/Skin/Knob.psd";
  148. private const string kCheckmarkPath = "UI/Skin/Checkmark.psd";
  149. private const string kDropdownArrowPath = "UI/Skin/DropdownArrow.psd";
  150. private const string kMaskPath = "UI/Skin/UIMask.psd";
  151. private static TMP_DefaultControls.Resources s_StandardResources;
  152. private static TMP_DefaultControls.Resources GetStandardResources()
  153. {
  154. if (s_StandardResources.standard == null)
  155. {
  156. s_StandardResources.standard = AssetDatabase.GetBuiltinExtraResource<Sprite>(kStandardSpritePath);
  157. s_StandardResources.background = AssetDatabase.GetBuiltinExtraResource<Sprite>(kBackgroundSpritePath);
  158. s_StandardResources.inputField = AssetDatabase.GetBuiltinExtraResource<Sprite>(kInputFieldBackgroundPath);
  159. s_StandardResources.knob = AssetDatabase.GetBuiltinExtraResource<Sprite>(kKnobPath);
  160. s_StandardResources.checkmark = AssetDatabase.GetBuiltinExtraResource<Sprite>(kCheckmarkPath);
  161. s_StandardResources.dropdown = AssetDatabase.GetBuiltinExtraResource<Sprite>(kDropdownArrowPath);
  162. s_StandardResources.mask = AssetDatabase.GetBuiltinExtraResource<Sprite>(kMaskPath);
  163. }
  164. return s_StandardResources;
  165. }
  166. private static void SetPositionVisibleinSceneView(RectTransform canvasRTransform, RectTransform itemTransform)
  167. {
  168. // Find the best scene view
  169. SceneView sceneView = SceneView.lastActiveSceneView;
  170. if (sceneView == null && SceneView.sceneViews.Count > 0)
  171. sceneView = SceneView.sceneViews[0] as SceneView;
  172. // Couldn't find a SceneView. Don't set position.
  173. if (sceneView == null || sceneView.camera == null)
  174. return;
  175. // Create world space Plane from canvas position.
  176. Camera camera = sceneView.camera;
  177. Vector3 position = Vector3.zero;
  178. Vector2 localPlanePosition;
  179. if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRTransform, new Vector2(camera.pixelWidth / 2, camera.pixelHeight / 2), camera, out localPlanePosition))
  180. {
  181. // Adjust for canvas pivot
  182. localPlanePosition.x = localPlanePosition.x + canvasRTransform.sizeDelta.x * canvasRTransform.pivot.x;
  183. localPlanePosition.y = localPlanePosition.y + canvasRTransform.sizeDelta.y * canvasRTransform.pivot.y;
  184. localPlanePosition.x = Mathf.Clamp(localPlanePosition.x, 0, canvasRTransform.sizeDelta.x);
  185. localPlanePosition.y = Mathf.Clamp(localPlanePosition.y, 0, canvasRTransform.sizeDelta.y);
  186. // Adjust for anchoring
  187. position.x = localPlanePosition.x - canvasRTransform.sizeDelta.x * itemTransform.anchorMin.x;
  188. position.y = localPlanePosition.y - canvasRTransform.sizeDelta.y * itemTransform.anchorMin.y;
  189. Vector3 minLocalPosition;
  190. minLocalPosition.x = canvasRTransform.sizeDelta.x * (0 - canvasRTransform.pivot.x) + itemTransform.sizeDelta.x * itemTransform.pivot.x;
  191. minLocalPosition.y = canvasRTransform.sizeDelta.y * (0 - canvasRTransform.pivot.y) + itemTransform.sizeDelta.y * itemTransform.pivot.y;
  192. Vector3 maxLocalPosition;
  193. maxLocalPosition.x = canvasRTransform.sizeDelta.x * (1 - canvasRTransform.pivot.x) - itemTransform.sizeDelta.x * itemTransform.pivot.x;
  194. maxLocalPosition.y = canvasRTransform.sizeDelta.y * (1 - canvasRTransform.pivot.y) - itemTransform.sizeDelta.y * itemTransform.pivot.y;
  195. position.x = Mathf.Clamp(position.x, minLocalPosition.x, maxLocalPosition.x);
  196. position.y = Mathf.Clamp(position.y, minLocalPosition.y, maxLocalPosition.y);
  197. }
  198. itemTransform.anchoredPosition = position;
  199. itemTransform.localRotation = Quaternion.identity;
  200. itemTransform.localScale = Vector3.one;
  201. }
  202. private static void PlaceUIElementRoot(GameObject element, MenuCommand menuCommand)
  203. {
  204. GameObject parent = menuCommand.context as GameObject;
  205. bool explicitParentChoice = true;
  206. if (parent == null)
  207. {
  208. parent = GetOrCreateCanvasGameObject();
  209. explicitParentChoice = false;
  210. // If in Prefab Mode, Canvas has to be part of Prefab contents,
  211. // otherwise use Prefab root instead.
  212. PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
  213. if (prefabStage != null && !prefabStage.IsPartOfPrefabContents(parent))
  214. parent = prefabStage.prefabContentsRoot;
  215. }
  216. if (parent.GetComponentInParent<Canvas>() == null)
  217. {
  218. // Create canvas under context GameObject,
  219. // and make that be the parent which UI element is added under.
  220. GameObject canvas = CreateNewUI();
  221. canvas.transform.SetParent(parent.transform, false);
  222. parent = canvas;
  223. }
  224. // Setting the element to be a child of an element already in the scene should
  225. // be sufficient to also move the element to that scene.
  226. // However, it seems the element needs to be already in its destination scene when the
  227. // RegisterCreatedObjectUndo is performed; otherwise the scene it was created in is dirtied.
  228. SceneManager.MoveGameObjectToScene(element, parent.scene);
  229. if (element.transform.parent == null)
  230. {
  231. Undo.SetTransformParent(element.transform, parent.transform, "Parent " + element.name);
  232. }
  233. GameObjectUtility.EnsureUniqueNameForSibling(element);
  234. // We have to fix up the undo name since the name of the object was only known after reparenting it.
  235. Undo.SetCurrentGroupName("Create " + element.name);
  236. GameObjectUtility.SetParentAndAlign(element, parent);
  237. if (!explicitParentChoice) // not a context click, so center in sceneview
  238. SetPositionVisibleinSceneView(parent.GetComponent<RectTransform>(), element.GetComponent<RectTransform>());
  239. Undo.RegisterCreatedObjectUndo(element, "Create " + element.name);
  240. Selection.activeGameObject = element;
  241. }
  242. public static GameObject CreateNewUI()
  243. {
  244. // Root for the UI
  245. var root = new GameObject("Canvas");
  246. root.layer = LayerMask.NameToLayer(kUILayerName);
  247. Canvas canvas = root.AddComponent<Canvas>();
  248. canvas.renderMode = RenderMode.ScreenSpaceOverlay;
  249. root.AddComponent<CanvasScaler>();
  250. root.AddComponent<GraphicRaycaster>();
  251. // Works for all stages.
  252. StageUtility.PlaceGameObjectInCurrentStage(root);
  253. bool customScene = false;
  254. PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
  255. if (prefabStage != null)
  256. {
  257. root.transform.SetParent(prefabStage.prefabContentsRoot.transform, false);
  258. customScene = true;
  259. }
  260. Undo.RegisterCreatedObjectUndo(root, "Create " + root.name);
  261. // If there is no event system add one...
  262. // No need to place event system in custom scene as these are temporary anyway.
  263. // It can be argued for or against placing it in the user scenes,
  264. // but let's not modify scene user is not currently looking at.
  265. if (!customScene)
  266. CreateEventSystem(false);
  267. return root;
  268. }
  269. private static void CreateEventSystem(bool select)
  270. {
  271. CreateEventSystem(select, null);
  272. }
  273. private static void CreateEventSystem(bool select, GameObject parent)
  274. {
  275. var esys = Object.FindObjectOfType<EventSystem>();
  276. if (esys == null)
  277. {
  278. var eventSystem = new GameObject("EventSystem");
  279. GameObjectUtility.SetParentAndAlign(eventSystem, parent);
  280. esys = eventSystem.AddComponent<EventSystem>();
  281. eventSystem.AddComponent<StandaloneInputModule>();
  282. Undo.RegisterCreatedObjectUndo(eventSystem, "Create " + eventSystem.name);
  283. }
  284. if (select && esys != null)
  285. {
  286. Selection.activeGameObject = esys.gameObject;
  287. }
  288. }
  289. // Helper function that returns a Canvas GameObject; preferably a parent of the selection, or other existing Canvas.
  290. public static GameObject GetOrCreateCanvasGameObject()
  291. {
  292. GameObject selectedGo = Selection.activeGameObject;
  293. // Try to find a gameobject that is the selected GO or one if its parents.
  294. Canvas canvas = (selectedGo != null) ? selectedGo.GetComponentInParent<Canvas>() : null;
  295. if (IsValidCanvas(canvas))
  296. return canvas.gameObject;
  297. // No canvas in selection or its parents? Then use any valid canvas.
  298. // We have to find all loaded Canvases, not just the ones in main scenes.
  299. Canvas[] canvasArray = StageUtility.GetCurrentStageHandle().FindComponentsOfType<Canvas>();
  300. for (int i = 0; i < canvasArray.Length; i++)
  301. if (IsValidCanvas(canvasArray[i]))
  302. return canvasArray[i].gameObject;
  303. // No canvas in the scene at all? Then create a new one.
  304. return CreateNewUI();
  305. }
  306. static bool IsValidCanvas(Canvas canvas)
  307. {
  308. if (canvas == null || !canvas.gameObject.activeInHierarchy)
  309. return false;
  310. // It's important that the non-editable canvas from a prefab scene won't be rejected,
  311. // but canvases not visible in the Hierarchy at all do. Don't check for HideAndDontSave.
  312. if (EditorUtility.IsPersistent(canvas) || (canvas.hideFlags & HideFlags.HideInHierarchy) != 0)
  313. return false;
  314. if (StageUtility.GetStageHandle(canvas.gameObject) != StageUtility.GetCurrentStageHandle())
  315. return false;
  316. return true;
  317. }
  318. }
  319. }