├── README.md ├── TMP_DefaultControls.cs └── TMPro_CreateObjectMenu.cs /README.md: -------------------------------------------------------------------------------- 1 | # TextMeshProButtonExtension 2 | Add new context menu "UI/TextMeshPro - Button". To create button with TextMeshPro instead of Unity UI Text. 3 | 4 | 5 | How to use: 6 | 7 | 1. Copy and Replace "TMP_DefaultControls.cs" in "TextMesh Pro/Scripts/". 8 | 9 | 2. Copy and Replace "TMPro_CreateObjectMenu.cs" in "TextMesh Pro/Editor". 10 | -------------------------------------------------------------------------------- /TMP_DefaultControls.cs: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2014 - 2016 Stephan Bouchard - All Rights Reserved 2 | // This code can only be used under the standard Unity Asset Store End User License Agreement 3 | // A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms 4 | 5 | 6 | using UnityEngine; 7 | using System.Collections; 8 | using UnityEngine.UI; 9 | 10 | 11 | namespace TMPro 12 | { 13 | 14 | public static class TMP_DefaultControls 15 | { 16 | public struct Resources 17 | { 18 | public Sprite standard; 19 | public Sprite background; 20 | public Sprite inputField; 21 | public Sprite knob; 22 | public Sprite checkmark; 23 | public Sprite dropdown; 24 | public Sprite mask; 25 | } 26 | 27 | private const float kWidth = 160f; 28 | private const float kThickHeight = 30f; 29 | private const float kThinHeight = 20f; 30 | private static Vector2 s_ThickElementSize = new Vector2(kWidth, kThickHeight); 31 | private static Vector2 s_ThinElementSize = new Vector2(kWidth, kThinHeight); 32 | //private static Vector2 s_ImageElementSize = new Vector2(100f, 100f); 33 | private static Color s_DefaultSelectableColor = new Color(1f, 1f, 1f, 1f); 34 | //private static Color s_PanelColor = new Color(1f, 1f, 1f, 0.392f); 35 | private static Color s_TextColor = new Color(50f / 255f, 50f / 255f, 50f / 255f, 1f); 36 | 37 | 38 | private static GameObject CreateUIElementRoot(string name, Vector2 size) 39 | { 40 | GameObject child = new GameObject(name); 41 | RectTransform rectTransform = child.AddComponent(); 42 | rectTransform.sizeDelta = size; 43 | return child; 44 | } 45 | 46 | static GameObject CreateUIObject(string name, GameObject parent) 47 | { 48 | GameObject go = new GameObject(name); 49 | go.AddComponent(); 50 | SetParentAndAlign(go, parent); 51 | return go; 52 | } 53 | 54 | private static void SetDefaultTextValues(TMP_Text lbl) 55 | { 56 | // Set text values we want across UI elements in default controls. 57 | // Don't set values which are the same as the default values for the Text component, 58 | // since there's no point in that, and it's good to keep them as consistent as possible. 59 | lbl.color = s_TextColor; 60 | lbl.fontSize = 14; 61 | } 62 | 63 | private static void SetDefaultColorTransitionValues(Selectable slider) 64 | { 65 | ColorBlock colors = slider.colors; 66 | colors.highlightedColor = new Color(0.882f, 0.882f, 0.882f); 67 | colors.pressedColor = new Color(0.698f, 0.698f, 0.698f); 68 | colors.disabledColor = new Color(0.521f, 0.521f, 0.521f); 69 | } 70 | 71 | private static void SetParentAndAlign(GameObject child, GameObject parent) 72 | { 73 | if (parent == null) 74 | return; 75 | 76 | child.transform.SetParent(parent.transform, false); 77 | SetLayerRecursively(child, parent.layer); 78 | } 79 | 80 | private static void SetLayerRecursively(GameObject go, int layer) 81 | { 82 | go.layer = layer; 83 | Transform t = go.transform; 84 | for (int i = 0; i < t.childCount; i++) 85 | SetLayerRecursively(t.GetChild(i).gameObject, layer); 86 | } 87 | 88 | // Actual controls 89 | 90 | public static GameObject CreateScrollbar(Resources resources) 91 | { 92 | // Create GOs Hierarchy 93 | GameObject scrollbarRoot = CreateUIElementRoot("Scrollbar", s_ThinElementSize); 94 | 95 | GameObject sliderArea = CreateUIObject("Sliding Area", scrollbarRoot); 96 | GameObject handle = CreateUIObject("Handle", sliderArea); 97 | 98 | Image bgImage = scrollbarRoot.AddComponent(); 99 | bgImage.sprite = resources.background; 100 | bgImage.type = Image.Type.Sliced; 101 | bgImage.color = s_DefaultSelectableColor; 102 | 103 | Image handleImage = handle.AddComponent(); 104 | handleImage.sprite = resources.standard; 105 | handleImage.type = Image.Type.Sliced; 106 | handleImage.color = s_DefaultSelectableColor; 107 | 108 | RectTransform sliderAreaRect = sliderArea.GetComponent(); 109 | sliderAreaRect.sizeDelta = new Vector2(-20, -20); 110 | sliderAreaRect.anchorMin = Vector2.zero; 111 | sliderAreaRect.anchorMax = Vector2.one; 112 | 113 | RectTransform handleRect = handle.GetComponent(); 114 | handleRect.sizeDelta = new Vector2(20, 20); 115 | 116 | Scrollbar scrollbar = scrollbarRoot.AddComponent(); 117 | scrollbar.handleRect = handleRect; 118 | scrollbar.targetGraphic = handleImage; 119 | SetDefaultColorTransitionValues(scrollbar); 120 | 121 | return scrollbarRoot; 122 | } 123 | 124 | 125 | public static GameObject CreateInputField(Resources resources) 126 | { 127 | GameObject root = CreateUIElementRoot("TextMeshPro - InputField", s_ThickElementSize); 128 | 129 | GameObject textArea = CreateUIObject("Text Area", root); 130 | GameObject childPlaceholder = CreateUIObject("Placeholder", textArea); 131 | GameObject childText = CreateUIObject("Text", textArea); 132 | 133 | Image image = root.AddComponent(); 134 | image.sprite = resources.inputField; 135 | image.type = Image.Type.Sliced; 136 | image.color = s_DefaultSelectableColor; 137 | 138 | TMP_InputField inputField = root.AddComponent(); 139 | SetDefaultColorTransitionValues(inputField); 140 | 141 | // Use UI.Mask for Unity 5.0 - 5.1 and 2D RectMask for Unity 5.2 and up 142 | textArea.AddComponent(); 143 | 144 | RectTransform textAreaRectTransform = textArea.GetComponent(); 145 | textAreaRectTransform.anchorMin = Vector2.zero; 146 | textAreaRectTransform.anchorMax = Vector2.one; 147 | textAreaRectTransform.sizeDelta = Vector2.zero; 148 | textAreaRectTransform.offsetMin = new Vector2(10, 6); 149 | textAreaRectTransform.offsetMax = new Vector2(-10, -7); 150 | 151 | 152 | TextMeshProUGUI text = childText.AddComponent(); 153 | text.text = ""; 154 | text.enableWordWrapping = false; 155 | text.extraPadding = true; 156 | text.richText = true; 157 | SetDefaultTextValues(text); 158 | 159 | TextMeshProUGUI placeholder = childPlaceholder.AddComponent(); 160 | placeholder.text = "Enter text..."; 161 | placeholder.fontSize = 14; 162 | placeholder.fontStyle = FontStyles.Italic; 163 | placeholder.enableWordWrapping = false; 164 | placeholder.extraPadding = true; 165 | 166 | // Make placeholder color half as opaque as normal text color. 167 | Color placeholderColor = text.color; 168 | placeholderColor.a *= 0.5f; 169 | placeholder.color = placeholderColor; 170 | 171 | RectTransform textRectTransform = childText.GetComponent(); 172 | textRectTransform.anchorMin = Vector2.zero; 173 | textRectTransform.anchorMax = Vector2.one; 174 | textRectTransform.sizeDelta = Vector2.zero; 175 | textRectTransform.offsetMin = new Vector2(0, 0); 176 | textRectTransform.offsetMax = new Vector2(0, 0); 177 | 178 | RectTransform placeholderRectTransform = childPlaceholder.GetComponent(); 179 | placeholderRectTransform.anchorMin = Vector2.zero; 180 | placeholderRectTransform.anchorMax = Vector2.one; 181 | placeholderRectTransform.sizeDelta = Vector2.zero; 182 | placeholderRectTransform.offsetMin = new Vector2(0, 0); 183 | placeholderRectTransform.offsetMax = new Vector2(0, 0); 184 | 185 | inputField.textViewport = textAreaRectTransform; 186 | inputField.textComponent = text; 187 | inputField.placeholder = placeholder; 188 | inputField.fontAsset = text.font; 189 | 190 | return root; 191 | } 192 | 193 | public static GameObject CreateButton(Resources resources) 194 | { 195 | GameObject root = CreateUIElementRoot("Button", s_ThickElementSize); 196 | Image image = root.AddComponent(); 197 | image.sprite = resources.standard; 198 | image.type=Image.Type.Sliced; 199 | root.AddComponent