TMP_DefaultControls.cs 17 KB

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