TMP_DefaultControls.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. namespace TMPro
  5. {
  6. public static class TMP_DefaultControls
  7. {
  8. public struct Resources
  9. {
  10. public Sprite standard;
  11. public Sprite background;
  12. public Sprite inputField;
  13. public Sprite knob;
  14. public Sprite checkmark;
  15. public Sprite dropdown;
  16. public Sprite mask;
  17. }
  18. private const float kWidth = 160f;
  19. private const float kThickHeight = 30f;
  20. private const float kThinHeight = 20f;
  21. private static Vector2 s_ThickElementSize = new Vector2(kWidth, kThickHeight);
  22. private static Vector2 s_ThinElementSize = new Vector2(kWidth, kThinHeight);
  23. //private static Vector2 s_ImageElementSize = new Vector2(100f, 100f);
  24. private static Color s_DefaultSelectableColor = new Color(1f, 1f, 1f, 1f);
  25. //private static Color s_PanelColor = new Color(1f, 1f, 1f, 0.392f);
  26. private static Color s_TextColor = new Color(50f / 255f, 50f / 255f, 50f / 255f, 1f);
  27. private static GameObject CreateUIElementRoot(string name, Vector2 size)
  28. {
  29. GameObject child = new GameObject(name);
  30. RectTransform rectTransform = child.AddComponent<RectTransform>();
  31. rectTransform.sizeDelta = size;
  32. return child;
  33. }
  34. static GameObject CreateUIObject(string name, GameObject parent)
  35. {
  36. GameObject go = new GameObject(name);
  37. go.AddComponent<RectTransform>();
  38. SetParentAndAlign(go, parent);
  39. return go;
  40. }
  41. private static void SetDefaultTextValues(TMP_Text lbl)
  42. {
  43. // Set text values we want across UI elements in default controls.
  44. // Don't set values which are the same as the default values for the Text component,
  45. // since there's no point in that, and it's good to keep them as consistent as possible.
  46. lbl.color = s_TextColor;
  47. lbl.fontSize = 14;
  48. }
  49. private static void SetDefaultColorTransitionValues(Selectable slider)
  50. {
  51. ColorBlock colors = slider.colors;
  52. colors.highlightedColor = new Color(0.882f, 0.882f, 0.882f);
  53. colors.pressedColor = new Color(0.698f, 0.698f, 0.698f);
  54. colors.disabledColor = new Color(0.521f, 0.521f, 0.521f);
  55. }
  56. private static void SetParentAndAlign(GameObject child, GameObject parent)
  57. {
  58. if (parent == null)
  59. return;
  60. child.transform.SetParent(parent.transform, false);
  61. SetLayerRecursively(child, parent.layer);
  62. }
  63. private static void SetLayerRecursively(GameObject go, int layer)
  64. {
  65. go.layer = layer;
  66. Transform t = go.transform;
  67. for (int i = 0; i < t.childCount; i++)
  68. SetLayerRecursively(t.GetChild(i).gameObject, layer);
  69. }
  70. // Actual controls
  71. public static GameObject CreateScrollbar(Resources resources)
  72. {
  73. // Create GOs Hierarchy
  74. GameObject scrollbarRoot = CreateUIElementRoot("Scrollbar", s_ThinElementSize);
  75. GameObject sliderArea = CreateUIObject("Sliding Area", scrollbarRoot);
  76. GameObject handle = CreateUIObject("Handle", sliderArea);
  77. Image bgImage = scrollbarRoot.AddComponent<Image>();
  78. bgImage.sprite = resources.background;
  79. bgImage.type = Image.Type.Sliced;
  80. bgImage.color = s_DefaultSelectableColor;
  81. Image handleImage = handle.AddComponent<Image>();
  82. handleImage.sprite = resources.standard;
  83. handleImage.type = Image.Type.Sliced;
  84. handleImage.color = s_DefaultSelectableColor;
  85. RectTransform sliderAreaRect = sliderArea.GetComponent<RectTransform>();
  86. sliderAreaRect.sizeDelta = new Vector2(-20, -20);
  87. sliderAreaRect.anchorMin = Vector2.zero;
  88. sliderAreaRect.anchorMax = Vector2.one;
  89. RectTransform handleRect = handle.GetComponent<RectTransform>();
  90. handleRect.sizeDelta = new Vector2(20, 20);
  91. Scrollbar scrollbar = scrollbarRoot.AddComponent<Scrollbar>();
  92. scrollbar.handleRect = handleRect;
  93. scrollbar.targetGraphic = handleImage;
  94. SetDefaultColorTransitionValues(scrollbar);
  95. return scrollbarRoot;
  96. }
  97. public static GameObject CreateButton(Resources resources)
  98. {
  99. GameObject buttonRoot = CreateUIElementRoot("Button", s_ThickElementSize);
  100. GameObject childText = new GameObject("Text (TMP)");
  101. childText.AddComponent<RectTransform>();
  102. SetParentAndAlign(childText, buttonRoot);
  103. Image image = buttonRoot.AddComponent<Image>();
  104. image.sprite = resources.standard;
  105. image.type = Image.Type.Sliced;
  106. image.color = s_DefaultSelectableColor;
  107. Button bt = buttonRoot.AddComponent<Button>();
  108. SetDefaultColorTransitionValues(bt);
  109. TextMeshProUGUI text = childText.AddComponent<TextMeshProUGUI>();
  110. text.text = "Button";
  111. text.alignment = TextAlignmentOptions.Center;
  112. SetDefaultTextValues(text);
  113. RectTransform textRectTransform = childText.GetComponent<RectTransform>();
  114. textRectTransform.anchorMin = Vector2.zero;
  115. textRectTransform.anchorMax = Vector2.one;
  116. textRectTransform.sizeDelta = Vector2.zero;
  117. return buttonRoot;
  118. }
  119. public static GameObject CreateText(Resources resources)
  120. {
  121. GameObject go = CreateUIElementRoot("Text (TMP)", s_ThickElementSize);
  122. TextMeshProUGUI lbl = go.AddComponent<TextMeshProUGUI>();
  123. lbl.text = "New Text";
  124. SetDefaultTextValues(lbl);
  125. return go;
  126. }
  127. public static GameObject CreateInputField(Resources resources)
  128. {
  129. GameObject root = CreateUIElementRoot("InputField (TMP)", s_ThickElementSize);
  130. GameObject textArea = CreateUIObject("Text Area", root);
  131. GameObject childPlaceholder = CreateUIObject("Placeholder", textArea);
  132. GameObject childText = CreateUIObject("Text", textArea);
  133. Image image = root.AddComponent<Image>();
  134. image.sprite = resources.inputField;
  135. image.type = Image.Type.Sliced;
  136. image.color = s_DefaultSelectableColor;
  137. TMP_InputField inputField = root.AddComponent<TMP_InputField>();
  138. SetDefaultColorTransitionValues(inputField);
  139. // Use UI.Mask for Unity 5.0 - 5.1 and 2D RectMask for Unity 5.2 and up
  140. textArea.AddComponent<RectMask2D>();
  141. RectTransform textAreaRectTransform = textArea.GetComponent<RectTransform>();
  142. textAreaRectTransform.anchorMin = Vector2.zero;
  143. textAreaRectTransform.anchorMax = Vector2.one;
  144. textAreaRectTransform.sizeDelta = Vector2.zero;
  145. textAreaRectTransform.offsetMin = new Vector2(10, 6);
  146. textAreaRectTransform.offsetMax = new Vector2(-10, -7);
  147. TextMeshProUGUI text = childText.AddComponent<TextMeshProUGUI>();
  148. text.text = "";
  149. text.enableWordWrapping = false;
  150. text.extraPadding = true;
  151. text.richText = true;
  152. SetDefaultTextValues(text);
  153. TextMeshProUGUI placeholder = childPlaceholder.AddComponent<TextMeshProUGUI>();
  154. placeholder.text = "Enter text...";
  155. placeholder.fontSize = 14;
  156. placeholder.fontStyle = FontStyles.Italic;
  157. placeholder.enableWordWrapping = false;
  158. placeholder.extraPadding = true;
  159. // Make placeholder color half as opaque as normal text color.
  160. Color placeholderColor = text.color;
  161. placeholderColor.a *= 0.5f;
  162. placeholder.color = placeholderColor;
  163. RectTransform textRectTransform = childText.GetComponent<RectTransform>();
  164. textRectTransform.anchorMin = Vector2.zero;
  165. textRectTransform.anchorMax = Vector2.one;
  166. textRectTransform.sizeDelta = Vector2.zero;
  167. textRectTransform.offsetMin = new Vector2(0, 0);
  168. textRectTransform.offsetMax = new Vector2(0, 0);
  169. RectTransform placeholderRectTransform = childPlaceholder.GetComponent<RectTransform>();
  170. placeholderRectTransform.anchorMin = Vector2.zero;
  171. placeholderRectTransform.anchorMax = Vector2.one;
  172. placeholderRectTransform.sizeDelta = Vector2.zero;
  173. placeholderRectTransform.offsetMin = new Vector2(0, 0);
  174. placeholderRectTransform.offsetMax = new Vector2(0, 0);
  175. inputField.textViewport = textAreaRectTransform;
  176. inputField.textComponent = text;
  177. inputField.placeholder = placeholder;
  178. inputField.fontAsset = text.font;
  179. return root;
  180. }
  181. public static GameObject CreateDropdown(Resources resources)
  182. {
  183. GameObject root = CreateUIElementRoot("Dropdown", s_ThickElementSize);
  184. GameObject label = CreateUIObject("Label", root);
  185. GameObject arrow = CreateUIObject("Arrow", root);
  186. GameObject template = CreateUIObject("Template", root);
  187. GameObject viewport = CreateUIObject("Viewport", template);
  188. GameObject content = CreateUIObject("Content", viewport);
  189. GameObject item = CreateUIObject("Item", content);
  190. GameObject itemBackground = CreateUIObject("Item Background", item);
  191. GameObject itemCheckmark = CreateUIObject("Item Checkmark", item);
  192. GameObject itemLabel = CreateUIObject("Item Label", item);
  193. // Sub controls.
  194. GameObject scrollbar = CreateScrollbar(resources);
  195. scrollbar.name = "Scrollbar";
  196. SetParentAndAlign(scrollbar, template);
  197. Scrollbar scrollbarScrollbar = scrollbar.GetComponent<Scrollbar>();
  198. scrollbarScrollbar.SetDirection(Scrollbar.Direction.BottomToTop, true);
  199. RectTransform vScrollbarRT = scrollbar.GetComponent<RectTransform>();
  200. vScrollbarRT.anchorMin = Vector2.right;
  201. vScrollbarRT.anchorMax = Vector2.one;
  202. vScrollbarRT.pivot = Vector2.one;
  203. vScrollbarRT.sizeDelta = new Vector2(vScrollbarRT.sizeDelta.x, 0);
  204. // Setup item UI components.
  205. TextMeshProUGUI itemLabelText = itemLabel.AddComponent<TextMeshProUGUI>();
  206. SetDefaultTextValues(itemLabelText);
  207. itemLabelText.alignment = TextAlignmentOptions.Left;
  208. Image itemBackgroundImage = itemBackground.AddComponent<Image>();
  209. itemBackgroundImage.color = new Color32(245, 245, 245, 255);
  210. Image itemCheckmarkImage = itemCheckmark.AddComponent<Image>();
  211. itemCheckmarkImage.sprite = resources.checkmark;
  212. Toggle itemToggle = item.AddComponent<Toggle>();
  213. itemToggle.targetGraphic = itemBackgroundImage;
  214. itemToggle.graphic = itemCheckmarkImage;
  215. itemToggle.isOn = true;
  216. // Setup template UI components.
  217. Image templateImage = template.AddComponent<Image>();
  218. templateImage.sprite = resources.standard;
  219. templateImage.type = Image.Type.Sliced;
  220. ScrollRect templateScrollRect = template.AddComponent<ScrollRect>();
  221. templateScrollRect.content = (RectTransform)content.transform;
  222. templateScrollRect.viewport = (RectTransform)viewport.transform;
  223. templateScrollRect.horizontal = false;
  224. templateScrollRect.movementType = ScrollRect.MovementType.Clamped;
  225. templateScrollRect.verticalScrollbar = scrollbarScrollbar;
  226. templateScrollRect.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport;
  227. templateScrollRect.verticalScrollbarSpacing = -3;
  228. Mask scrollRectMask = viewport.AddComponent<Mask>();
  229. scrollRectMask.showMaskGraphic = false;
  230. Image viewportImage = viewport.AddComponent<Image>();
  231. viewportImage.sprite = resources.mask;
  232. viewportImage.type = Image.Type.Sliced;
  233. // Setup dropdown UI components.
  234. TextMeshProUGUI labelText = label.AddComponent<TextMeshProUGUI>();
  235. SetDefaultTextValues(labelText);
  236. labelText.alignment = TextAlignmentOptions.Left;
  237. Image arrowImage = arrow.AddComponent<Image>();
  238. arrowImage.sprite = resources.dropdown;
  239. Image backgroundImage = root.AddComponent<Image>();
  240. backgroundImage.sprite = resources.standard;
  241. backgroundImage.color = s_DefaultSelectableColor;
  242. backgroundImage.type = Image.Type.Sliced;
  243. TMP_Dropdown dropdown = root.AddComponent<TMP_Dropdown>();
  244. dropdown.targetGraphic = backgroundImage;
  245. SetDefaultColorTransitionValues(dropdown);
  246. dropdown.template = template.GetComponent<RectTransform>();
  247. dropdown.captionText = labelText;
  248. dropdown.itemText = itemLabelText;
  249. // Setting default Item list.
  250. itemLabelText.text = "Option A";
  251. dropdown.options.Add(new TMP_Dropdown.OptionData {text = "Option A" });
  252. dropdown.options.Add(new TMP_Dropdown.OptionData {text = "Option B" });
  253. dropdown.options.Add(new TMP_Dropdown.OptionData {text = "Option C" });
  254. dropdown.RefreshShownValue();
  255. // Set up RectTransforms.
  256. RectTransform labelRT = label.GetComponent<RectTransform>();
  257. labelRT.anchorMin = Vector2.zero;
  258. labelRT.anchorMax = Vector2.one;
  259. labelRT.offsetMin = new Vector2(10, 6);
  260. labelRT.offsetMax = new Vector2(-25, -7);
  261. RectTransform arrowRT = arrow.GetComponent<RectTransform>();
  262. arrowRT.anchorMin = new Vector2(1, 0.5f);
  263. arrowRT.anchorMax = new Vector2(1, 0.5f);
  264. arrowRT.sizeDelta = new Vector2(20, 20);
  265. arrowRT.anchoredPosition = new Vector2(-15, 0);
  266. RectTransform templateRT = template.GetComponent<RectTransform>();
  267. templateRT.anchorMin = new Vector2(0, 0);
  268. templateRT.anchorMax = new Vector2(1, 0);
  269. templateRT.pivot = new Vector2(0.5f, 1);
  270. templateRT.anchoredPosition = new Vector2(0, 2);
  271. templateRT.sizeDelta = new Vector2(0, 150);
  272. RectTransform viewportRT = viewport.GetComponent<RectTransform>();
  273. viewportRT.anchorMin = new Vector2(0, 0);
  274. viewportRT.anchorMax = new Vector2(1, 1);
  275. viewportRT.sizeDelta = new Vector2(-18, 0);
  276. viewportRT.pivot = new Vector2(0, 1);
  277. RectTransform contentRT = content.GetComponent<RectTransform>();
  278. contentRT.anchorMin = new Vector2(0f, 1);
  279. contentRT.anchorMax = new Vector2(1f, 1);
  280. contentRT.pivot = new Vector2(0.5f, 1);
  281. contentRT.anchoredPosition = new Vector2(0, 0);
  282. contentRT.sizeDelta = new Vector2(0, 28);
  283. RectTransform itemRT = item.GetComponent<RectTransform>();
  284. itemRT.anchorMin = new Vector2(0, 0.5f);
  285. itemRT.anchorMax = new Vector2(1, 0.5f);
  286. itemRT.sizeDelta = new Vector2(0, 20);
  287. RectTransform itemBackgroundRT = itemBackground.GetComponent<RectTransform>();
  288. itemBackgroundRT.anchorMin = Vector2.zero;
  289. itemBackgroundRT.anchorMax = Vector2.one;
  290. itemBackgroundRT.sizeDelta = Vector2.zero;
  291. RectTransform itemCheckmarkRT = itemCheckmark.GetComponent<RectTransform>();
  292. itemCheckmarkRT.anchorMin = new Vector2(0, 0.5f);
  293. itemCheckmarkRT.anchorMax = new Vector2(0, 0.5f);
  294. itemCheckmarkRT.sizeDelta = new Vector2(20, 20);
  295. itemCheckmarkRT.anchoredPosition = new Vector2(10, 0);
  296. RectTransform itemLabelRT = itemLabel.GetComponent<RectTransform>();
  297. itemLabelRT.anchorMin = Vector2.zero;
  298. itemLabelRT.anchorMax = Vector2.one;
  299. itemLabelRT.offsetMin = new Vector2(20, 1);
  300. itemLabelRT.offsetMax = new Vector2(-10, -2);
  301. template.SetActive(false);
  302. return root;
  303. }
  304. }
  305. }