TMPro_CreateObjectMenu.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. Vector2 localPlanePosition;
  106. if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRTransform, new Vector2(camera.pixelWidth / 2, camera.pixelHeight / 2), camera, out localPlanePosition))
  107. {
  108. // Adjust for canvas pivot
  109. localPlanePosition.x = localPlanePosition.x + canvasRTransform.sizeDelta.x * canvasRTransform.pivot.x;
  110. localPlanePosition.y = localPlanePosition.y + canvasRTransform.sizeDelta.y * canvasRTransform.pivot.y;
  111. localPlanePosition.x = Mathf.Clamp(localPlanePosition.x, 0, canvasRTransform.sizeDelta.x);
  112. localPlanePosition.y = Mathf.Clamp(localPlanePosition.y, 0, canvasRTransform.sizeDelta.y);
  113. // Adjust for anchoring
  114. position.x = localPlanePosition.x - canvasRTransform.sizeDelta.x * itemTransform.anchorMin.x;
  115. position.y = localPlanePosition.y - canvasRTransform.sizeDelta.y * itemTransform.anchorMin.y;
  116. Vector3 minLocalPosition;
  117. minLocalPosition.x = canvasRTransform.sizeDelta.x * (0 - canvasRTransform.pivot.x) + itemTransform.sizeDelta.x * itemTransform.pivot.x;
  118. minLocalPosition.y = canvasRTransform.sizeDelta.y * (0 - canvasRTransform.pivot.y) + itemTransform.sizeDelta.y * itemTransform.pivot.y;
  119. Vector3 maxLocalPosition;
  120. maxLocalPosition.x = canvasRTransform.sizeDelta.x * (1 - canvasRTransform.pivot.x) - itemTransform.sizeDelta.x * itemTransform.pivot.x;
  121. maxLocalPosition.y = canvasRTransform.sizeDelta.y * (1 - canvasRTransform.pivot.y) - itemTransform.sizeDelta.y * itemTransform.pivot.y;
  122. position.x = Mathf.Clamp(position.x, minLocalPosition.x, maxLocalPosition.x);
  123. position.y = Mathf.Clamp(position.y, minLocalPosition.y, maxLocalPosition.y);
  124. }
  125. itemTransform.anchoredPosition = position;
  126. itemTransform.localRotation = Quaternion.identity;
  127. itemTransform.localScale = Vector3.one;
  128. }
  129. private static void PlaceUIElementRoot(GameObject element, MenuCommand menuCommand)
  130. {
  131. GameObject parent = menuCommand.context as GameObject;
  132. bool explicitParentChoice = true;
  133. if (parent == null)
  134. {
  135. parent = GetOrCreateCanvasGameObject();
  136. explicitParentChoice = false;
  137. // If in Prefab Mode, Canvas has to be part of Prefab contents,
  138. // otherwise use Prefab root instead.
  139. PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
  140. if (prefabStage != null && !prefabStage.IsPartOfPrefabContents(parent))
  141. parent = prefabStage.prefabContentsRoot;
  142. }
  143. if (parent.GetComponentInParent<Canvas>() == null)
  144. {
  145. // Create canvas under context GameObject,
  146. // and make that be the parent which UI element is added under.
  147. GameObject canvas = CreateNewUI();
  148. canvas.transform.SetParent(parent.transform, false);
  149. parent = canvas;
  150. }
  151. // Setting the element to be a child of an element already in the scene should
  152. // be sufficient to also move the element to that scene.
  153. // However, it seems the element needs to be already in its destination scene when the
  154. // RegisterCreatedObjectUndo is performed; otherwise the scene it was created in is dirtied.
  155. SceneManager.MoveGameObjectToScene(element, parent.scene);
  156. if (element.transform.parent == null)
  157. {
  158. Undo.SetTransformParent(element.transform, parent.transform, "Parent " + element.name);
  159. }
  160. GameObjectUtility.EnsureUniqueNameForSibling(element);
  161. // We have to fix up the undo name since the name of the object was only known after reparenting it.
  162. Undo.SetCurrentGroupName("Create " + element.name);
  163. GameObjectUtility.SetParentAndAlign(element, parent);
  164. if (!explicitParentChoice) // not a context click, so center in sceneview
  165. SetPositionVisibleinSceneView(parent.GetComponent<RectTransform>(), element.GetComponent<RectTransform>());
  166. Undo.RegisterCreatedObjectUndo(element, "Create " + element.name);
  167. Selection.activeGameObject = element;
  168. }
  169. static public GameObject CreateNewUI()
  170. {
  171. // Root for the UI
  172. var root = new GameObject("Canvas");
  173. root.layer = LayerMask.NameToLayer(kUILayerName);
  174. Canvas canvas = root.AddComponent<Canvas>();
  175. canvas.renderMode = RenderMode.ScreenSpaceOverlay;
  176. root.AddComponent<CanvasScaler>();
  177. root.AddComponent<GraphicRaycaster>();
  178. // Works for all stages.
  179. StageUtility.PlaceGameObjectInCurrentStage(root);
  180. bool customScene = false;
  181. PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
  182. if (prefabStage != null)
  183. {
  184. root.transform.SetParent(prefabStage.prefabContentsRoot.transform, false);
  185. customScene = true;
  186. }
  187. Undo.RegisterCreatedObjectUndo(root, "Create " + root.name);
  188. // If there is no event system add one...
  189. // No need to place event system in custom scene as these are temporary anyway.
  190. // It can be argued for or against placing it in the user scenes,
  191. // but let's not modify scene user is not currently looking at.
  192. if (!customScene)
  193. CreateEventSystem(false);
  194. return root;
  195. }
  196. private static void CreateEventSystem(bool select)
  197. {
  198. CreateEventSystem(select, null);
  199. }
  200. private static void CreateEventSystem(bool select, GameObject parent)
  201. {
  202. var esys = Object.FindObjectOfType<EventSystem>();
  203. if (esys == null)
  204. {
  205. var eventSystem = new GameObject("EventSystem");
  206. GameObjectUtility.SetParentAndAlign(eventSystem, parent);
  207. esys = eventSystem.AddComponent<EventSystem>();
  208. eventSystem.AddComponent<StandaloneInputModule>();
  209. Undo.RegisterCreatedObjectUndo(eventSystem, "Create " + eventSystem.name);
  210. }
  211. if (select && esys != null)
  212. {
  213. Selection.activeGameObject = esys.gameObject;
  214. }
  215. }
  216. // Helper function that returns a Canvas GameObject; preferably a parent of the selection, or other existing Canvas.
  217. static public GameObject GetOrCreateCanvasGameObject()
  218. {
  219. GameObject selectedGo = Selection.activeGameObject;
  220. // Try to find a gameobject that is the selected GO or one if its parents.
  221. Canvas canvas = (selectedGo != null) ? selectedGo.GetComponentInParent<Canvas>() : null;
  222. if (IsValidCanvas(canvas))
  223. return canvas.gameObject;
  224. // No canvas in selection or its parents? Then use any valid canvas.
  225. // We have to find all loaded Canvases, not just the ones in main scenes.
  226. Canvas[] canvasArray = StageUtility.GetCurrentStageHandle().FindComponentsOfType<Canvas>();
  227. for (int i = 0; i < canvasArray.Length; i++)
  228. if (IsValidCanvas(canvasArray[i]))
  229. return canvasArray[i].gameObject;
  230. // No canvas in the scene at all? Then create a new one.
  231. return CreateNewUI();
  232. }
  233. static bool IsValidCanvas(Canvas canvas)
  234. {
  235. if (canvas == null || !canvas.gameObject.activeInHierarchy)
  236. return false;
  237. // It's important that the non-editable canvas from a prefab scene won't be rejected,
  238. // but canvases not visible in the Hierarchy at all do. Don't check for HideAndDontSave.
  239. if (EditorUtility.IsPersistent(canvas) || (canvas.hideFlags & HideFlags.HideInHierarchy) != 0)
  240. return false;
  241. if (StageUtility.GetStageHandle(canvas.gameObject) != StageUtility.GetCurrentStageHandle())
  242. return false;
  243. return true;
  244. }
  245. }
  246. }