using UnityEngine; using UnityEditor; using UnityEngine.UI; using UnityEngine.EventSystems; namespace TMPro.EditorUtilities { public static class TMPro_CreateObjectMenu { /// /// Create a TextMeshPro object that works with the Mesh Renderer /// /// [MenuItem("GameObject/3D Object/TextMeshPro - Text", false, 30)] static void CreateTextMeshProObjectPerform(MenuCommand command) { GameObject go = new GameObject("TextMeshPro"); TextMeshPro textMeshPro = go.AddComponent(); textMeshPro.text = "Sample text"; textMeshPro.alignment = TextAlignmentOptions.TopLeft; Undo.RegisterCreatedObjectUndo((Object)go, "Create " + go.name); GameObject contextObject = command.context as GameObject; if (contextObject != null) { GameObjectUtility.SetParentAndAlign(go, contextObject); Undo.SetTransformParent(go.transform, contextObject.transform, "Parent " + go.name); } Selection.activeGameObject = go; } /// /// Create a TextMeshPro object that works with the CanvasRenderer /// /// [MenuItem("GameObject/UI/TextMeshPro - Text", false, 2001)] static void CreateTextMeshProGuiObjectPerform(MenuCommand command) { // Check if there is a Canvas in the scene Canvas canvas = Object.FindObjectOfType(); if (canvas == null) { // Create new Canvas since none exists in the scene. GameObject canvasObject = new GameObject("Canvas"); canvas = canvasObject.AddComponent(); canvas.renderMode = RenderMode.ScreenSpaceOverlay; // Add a Graphic Raycaster Component as well canvas.gameObject.AddComponent(); Undo.RegisterCreatedObjectUndo(canvasObject, "Create " + canvasObject.name); } // Create the TextMeshProUGUI Object GameObject go = new GameObject("TextMeshPro Text"); RectTransform goRectTransform = go.AddComponent(); Undo.RegisterCreatedObjectUndo((Object)go, "Create " + go.name); // Check if object is being create with left or right click GameObject contextObject = command.context as GameObject; if (contextObject == null) { //goRectTransform.sizeDelta = new Vector2(200f, 50f); GameObjectUtility.SetParentAndAlign(go, canvas.gameObject); TextMeshProUGUI textMeshPro = go.AddComponent(); textMeshPro.text = "New Text"; textMeshPro.alignment = TextAlignmentOptions.TopLeft; } else { if (contextObject.GetComponent