You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

345 lines
15 KiB

  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 CreateInputField(Resources resources)
  98. {
  99. GameObject root = CreateUIElementRoot("TextMeshPro - InputField", s_ThickElementSize);
  100. GameObject textArea = CreateUIObject("Text Area", root);
  101. GameObject childPlaceholder = CreateUIObject("Placeholder", textArea);
  102. GameObject childText = CreateUIObject("Text", textArea);
  103. Image image = root.AddComponent<Image>();
  104. image.sprite = resources.inputField;
  105. image.type = Image.Type.Sliced;
  106. image.color = s_DefaultSelectableColor;
  107. TMP_InputField inputField = root.AddComponent<TMP_InputField>();
  108. SetDefaultColorTransitionValues(inputField);
  109. // Use UI.Mask for Unity 5.0 - 5.1 and 2D RectMask for Unity 5.2 and up
  110. textArea.AddComponent<RectMask2D>();
  111. RectTransform textAreaRectTransform = textArea.GetComponent<RectTransform>();
  112. textAreaRectTransform.anchorMin = Vector2.zero;
  113. textAreaRectTransform.anchorMax = Vector2.one;
  114. textAreaRectTransform.sizeDelta = Vector2.zero;
  115. textAreaRectTransform.offsetMin = new Vector2(10, 6);
  116. textAreaRectTransform.offsetMax = new Vector2(-10, -7);
  117. TextMeshProUGUI text = childText.AddComponent<TextMeshProUGUI>();
  118. text.text = "";
  119. text.enableWordWrapping = false;
  120. text.extraPadding = true;
  121. text.richText = true;
  122. SetDefaultTextValues(text);
  123. TextMeshProUGUI placeholder = childPlaceholder.AddComponent<TextMeshProUGUI>();
  124. placeholder.text = "Enter text...";
  125. placeholder.fontSize = 14;
  126. placeholder.fontStyle = FontStyles.Italic;
  127. placeholder.enableWordWrapping = false;
  128. placeholder.extraPadding = true;
  129. // Make placeholder color half as opaque as normal text color.
  130. Color placeholderColor = text.color;
  131. placeholderColor.a *= 0.5f;
  132. placeholder.color = placeholderColor;
  133. RectTransform textRectTransform = childText.GetComponent<RectTransform>();
  134. textRectTransform.anchorMin = Vector2.zero;
  135. textRectTransform.anchorMax = Vector2.one;
  136. textRectTransform.sizeDelta = Vector2.zero;
  137. textRectTransform.offsetMin = new Vector2(0, 0);
  138. textRectTransform.offsetMax = new Vector2(0, 0);
  139. RectTransform placeholderRectTransform = childPlaceholder.GetComponent<RectTransform>();
  140. placeholderRectTransform.anchorMin = Vector2.zero;
  141. placeholderRectTransform.anchorMax = Vector2.one;
  142. placeholderRectTransform.sizeDelta = Vector2.zero;
  143. placeholderRectTransform.offsetMin = new Vector2(0, 0);
  144. placeholderRectTransform.offsetMax = new Vector2(0, 0);
  145. inputField.textViewport = textAreaRectTransform;
  146. inputField.textComponent = text;
  147. inputField.placeholder = placeholder;
  148. inputField.fontAsset = text.font;
  149. return root;
  150. }
  151. public static GameObject CreateDropdown(Resources resources)
  152. {
  153. GameObject root = CreateUIElementRoot("Dropdown", s_ThickElementSize);
  154. GameObject label = CreateUIObject("Label", root);
  155. GameObject arrow = CreateUIObject("Arrow", root);
  156. GameObject template = CreateUIObject("Template", root);
  157. GameObject viewport = CreateUIObject("Viewport", template);
  158. GameObject content = CreateUIObject("Content", viewport);
  159. GameObject item = CreateUIObject("Item", content);
  160. GameObject itemBackground = CreateUIObject("Item Background", item);
  161. GameObject itemCheckmark = CreateUIObject("Item Checkmark", item);
  162. GameObject itemLabel = CreateUIObject("Item Label", item);
  163. // Sub controls.
  164. GameObject scrollbar = CreateScrollbar(resources);
  165. scrollbar.name = "Scrollbar";
  166. SetParentAndAlign(scrollbar, template);
  167. Scrollbar scrollbarScrollbar = scrollbar.GetComponent<Scrollbar>();
  168. scrollbarScrollbar.SetDirection(Scrollbar.Direction.BottomToTop, true);
  169. RectTransform vScrollbarRT = scrollbar.GetComponent<RectTransform>();
  170. vScrollbarRT.anchorMin = Vector2.right;
  171. vScrollbarRT.anchorMax = Vector2.one;
  172. vScrollbarRT.pivot = Vector2.one;
  173. vScrollbarRT.sizeDelta = new Vector2(vScrollbarRT.sizeDelta.x, 0);
  174. // Setup item UI components.
  175. TextMeshProUGUI itemLabelText = itemLabel.AddComponent<TextMeshProUGUI>();
  176. SetDefaultTextValues(itemLabelText);
  177. itemLabelText.alignment = TextAlignmentOptions.Left;
  178. Image itemBackgroundImage = itemBackground.AddComponent<Image>();
  179. itemBackgroundImage.color = new Color32(245, 245, 245, 255);
  180. Image itemCheckmarkImage = itemCheckmark.AddComponent<Image>();
  181. itemCheckmarkImage.sprite = resources.checkmark;
  182. Toggle itemToggle = item.AddComponent<Toggle>();
  183. itemToggle.targetGraphic = itemBackgroundImage;
  184. itemToggle.graphic = itemCheckmarkImage;
  185. itemToggle.isOn = true;
  186. // Setup template UI components.
  187. Image templateImage = template.AddComponent<Image>();
  188. templateImage.sprite = resources.standard;
  189. templateImage.type = Image.Type.Sliced;
  190. ScrollRect templateScrollRect = template.AddComponent<ScrollRect>();
  191. templateScrollRect.content = (RectTransform)content.transform;
  192. templateScrollRect.viewport = (RectTransform)viewport.transform;
  193. templateScrollRect.horizontal = false;
  194. templateScrollRect.movementType = ScrollRect.MovementType.Clamped;
  195. templateScrollRect.verticalScrollbar = scrollbarScrollbar;
  196. templateScrollRect.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport;
  197. templateScrollRect.verticalScrollbarSpacing = -3;
  198. Mask scrollRectMask = viewport.AddComponent<Mask>();
  199. scrollRectMask.showMaskGraphic = false;
  200. Image viewportImage = viewport.AddComponent<Image>();
  201. viewportImage.sprite = resources.mask;
  202. viewportImage.type = Image.Type.Sliced;
  203. // Setup dropdown UI components.
  204. TextMeshProUGUI labelText = label.AddComponent<TextMeshProUGUI>();
  205. SetDefaultTextValues(labelText);
  206. labelText.alignment = TextAlignmentOptions.Left;
  207. Image arrowImage = arrow.AddComponent<Image>();
  208. arrowImage.sprite = resources.dropdown;
  209. Image backgroundImage = root.AddComponent<Image>();
  210. backgroundImage.sprite = resources.standard;
  211. backgroundImage.color = s_DefaultSelectableColor;
  212. backgroundImage.type = Image.Type.Sliced;
  213. TMP_Dropdown dropdown = root.AddComponent<TMP_Dropdown>();
  214. dropdown.targetGraphic = backgroundImage;
  215. SetDefaultColorTransitionValues(dropdown);
  216. dropdown.template = template.GetComponent<RectTransform>();
  217. dropdown.captionText = labelText;
  218. dropdown.itemText = itemLabelText;
  219. // Setting default Item list.
  220. itemLabelText.text = "Option A";
  221. dropdown.options.Add(new TMP_Dropdown.OptionData {text = "Option A" });
  222. dropdown.options.Add(new TMP_Dropdown.OptionData {text = "Option B" });
  223. dropdown.options.Add(new TMP_Dropdown.OptionData {text = "Option C" });
  224. dropdown.RefreshShownValue();
  225. // Set up RectTransforms.
  226. RectTransform labelRT = label.GetComponent<RectTransform>();
  227. labelRT.anchorMin = Vector2.zero;
  228. labelRT.anchorMax = Vector2.one;
  229. labelRT.offsetMin = new Vector2(10, 6);
  230. labelRT.offsetMax = new Vector2(-25, -7);
  231. RectTransform arrowRT = arrow.GetComponent<RectTransform>();
  232. arrowRT.anchorMin = new Vector2(1, 0.5f);
  233. arrowRT.anchorMax = new Vector2(1, 0.5f);
  234. arrowRT.sizeDelta = new Vector2(20, 20);
  235. arrowRT.anchoredPosition = new Vector2(-15, 0);
  236. RectTransform templateRT = template.GetComponent<RectTransform>();
  237. templateRT.anchorMin = new Vector2(0, 0);
  238. templateRT.anchorMax = new Vector2(1, 0);
  239. templateRT.pivot = new Vector2(0.5f, 1);
  240. templateRT.anchoredPosition = new Vector2(0, 2);
  241. templateRT.sizeDelta = new Vector2(0, 150);
  242. RectTransform viewportRT = viewport.GetComponent<RectTransform>();
  243. viewportRT.anchorMin = new Vector2(0, 0);
  244. viewportRT.anchorMax = new Vector2(1, 1);
  245. viewportRT.sizeDelta = new Vector2(-18, 0);
  246. viewportRT.pivot = new Vector2(0, 1);
  247. RectTransform contentRT = content.GetComponent<RectTransform>();
  248. contentRT.anchorMin = new Vector2(0f, 1);
  249. contentRT.anchorMax = new Vector2(1f, 1);
  250. contentRT.pivot = new Vector2(0.5f, 1);
  251. contentRT.anchoredPosition = new Vector2(0, 0);
  252. contentRT.sizeDelta = new Vector2(0, 28);
  253. RectTransform itemRT = item.GetComponent<RectTransform>();
  254. itemRT.anchorMin = new Vector2(0, 0.5f);
  255. itemRT.anchorMax = new Vector2(1, 0.5f);
  256. itemRT.sizeDelta = new Vector2(0, 20);
  257. RectTransform itemBackgroundRT = itemBackground.GetComponent<RectTransform>();
  258. itemBackgroundRT.anchorMin = Vector2.zero;
  259. itemBackgroundRT.anchorMax = Vector2.one;
  260. itemBackgroundRT.sizeDelta = Vector2.zero;
  261. RectTransform itemCheckmarkRT = itemCheckmark.GetComponent<RectTransform>();
  262. itemCheckmarkRT.anchorMin = new Vector2(0, 0.5f);
  263. itemCheckmarkRT.anchorMax = new Vector2(0, 0.5f);
  264. itemCheckmarkRT.sizeDelta = new Vector2(20, 20);
  265. itemCheckmarkRT.anchoredPosition = new Vector2(10, 0);
  266. RectTransform itemLabelRT = itemLabel.GetComponent<RectTransform>();
  267. itemLabelRT.anchorMin = Vector2.zero;
  268. itemLabelRT.anchorMax = Vector2.one;
  269. itemLabelRT.offsetMin = new Vector2(20, 1);
  270. itemLabelRT.offsetMax = new Vector2(-10, -2);
  271. template.SetActive(false);
  272. return root;
  273. }
  274. }
  275. }