TMPro_CreateObjectMenu.cs 14 KB

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