SGDefaultControls.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. namespace UnityEngine.UI
  5. {
  6. public static class SGDefaultControls
  7. {
  8. #region code from DefaultControls.cs
  9. public struct Resources
  10. {
  11. public Sprite standard;
  12. public Sprite background;
  13. public Sprite inputField;
  14. public Sprite knob;
  15. public Sprite checkmark;
  16. public Sprite dropdown;
  17. public Sprite mask;
  18. }
  19. private const float kWidth = 160f;
  20. private const float kThickHeight = 30f;
  21. private const float kThinHeight = 20f;
  22. //private static Vector2 s_ThickElementSize = new Vector2(kWidth, kThickHeight);
  23. //private static Vector2 s_ThinElementSize = new Vector2(kWidth, kThinHeight);
  24. //private static Vector2 s_ImageElementSize = new Vector2(100f, 100f);
  25. //private static Color s_DefaultSelectableColor = new Color(1f, 1f, 1f, 1f);
  26. //private static Color s_PanelColor = new Color(1f, 1f, 1f, 0.392f);
  27. private static Color s_TextColor = new Color(50f / 255f, 50f / 255f, 50f / 255f, 1f);
  28. // Helper methods at top
  29. private static GameObject CreateUIElementRoot(string name, Vector2 size)
  30. {
  31. GameObject child = new GameObject(name);
  32. RectTransform rectTransform = child.AddComponent<RectTransform>();
  33. rectTransform.sizeDelta = size;
  34. return child;
  35. }
  36. static GameObject CreateUIObject(string name, GameObject parent)
  37. {
  38. GameObject go = new GameObject(name);
  39. go.AddComponent<RectTransform>();
  40. SetParentAndAlign(go, parent);
  41. return go;
  42. }
  43. private static void SetDefaultTextValues(Text lbl)
  44. {
  45. // Set text values we want across UI elements in default controls.
  46. // Don't set values which are the same as the default values for the Text component,
  47. // since there's no point in that, and it's good to keep them as consistent as possible.
  48. lbl.color = s_TextColor;
  49. }
  50. private static void SetDefaultColorTransitionValues(Selectable slider)
  51. {
  52. ColorBlock colors = slider.colors;
  53. colors.highlightedColor = new Color(0.882f, 0.882f, 0.882f);
  54. colors.pressedColor = new Color(0.698f, 0.698f, 0.698f);
  55. colors.disabledColor = new Color(0.521f, 0.521f, 0.521f);
  56. }
  57. private static void SetParentAndAlign(GameObject child, GameObject parent)
  58. {
  59. if (parent == null)
  60. return;
  61. child.transform.SetParent(parent.transform, false);
  62. SetLayerRecursively(child, parent.layer);
  63. }
  64. private static void SetLayerRecursively(GameObject go, int layer)
  65. {
  66. go.layer = layer;
  67. Transform t = go.transform;
  68. for (int i = 0; i < t.childCount; i++)
  69. SetLayerRecursively(t.GetChild(i).gameObject, layer);
  70. }
  71. #endregion
  72. public static GameObject CreateLoopHorizontalScrollRect(DefaultControls.Resources resources)
  73. {
  74. GameObject root = CreateUIElementRoot("Loop Horizontal Scroll Rect", new Vector2(400, 200));
  75. GameObject cache = CreateUIObject("Cache", root);
  76. cache.SetActive(false);
  77. GameObject content = CreateUIObject("Content", root);
  78. RectTransform contentRT = content.GetComponent<RectTransform>();
  79. contentRT.anchorMin = new Vector2(0, 0.5f);
  80. contentRT.anchorMax = new Vector2(0, 0.5f);
  81. contentRT.sizeDelta = new Vector2(0, 200);
  82. contentRT.pivot = new Vector2(0, 0.5f);
  83. // Setup UI components.
  84. LoopHorizontalScrollRect scrollRect = root.AddComponent<LoopHorizontalScrollRect>();
  85. scrollRect.content = contentRT;
  86. scrollRect.viewport = null;
  87. scrollRect.horizontalScrollbar = null;
  88. scrollRect.verticalScrollbar = null;
  89. scrollRect.horizontal = true;
  90. scrollRect.vertical = false;
  91. scrollRect.horizontalScrollbarVisibility = LoopScrollRect.ScrollbarVisibility.Permanent;
  92. scrollRect.verticalScrollbarVisibility = LoopScrollRect.ScrollbarVisibility.Permanent;
  93. scrollRect.horizontalScrollbarSpacing = 0;
  94. scrollRect.verticalScrollbarSpacing = 0;
  95. scrollRect.u_CacheRect = cache.transform as RectTransform;
  96. root.AddComponent<RectMask2D>();
  97. HorizontalLayoutGroup layoutGroup = content.AddComponent<HorizontalLayoutGroup>();
  98. layoutGroup.childAlignment = TextAnchor.MiddleLeft;
  99. layoutGroup.childForceExpandWidth = false;
  100. layoutGroup.childForceExpandHeight = true;
  101. ContentSizeFitter sizeFitter = content.AddComponent<ContentSizeFitter>();
  102. sizeFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
  103. sizeFitter.verticalFit = ContentSizeFitter.FitMode.Unconstrained;
  104. return root;
  105. }
  106. public static GameObject CreateLoopVerticalScrollRect(DefaultControls.Resources resources)
  107. {
  108. GameObject root = CreateUIElementRoot("Loop Vertical Scroll Rect", new Vector2(200, 400));
  109. GameObject cache = CreateUIObject("Cache", root);
  110. cache.SetActive(false);
  111. GameObject content = CreateUIObject("Content", root);
  112. RectTransform contentRT = content.GetComponent<RectTransform>();
  113. contentRT.anchorMin = new Vector2(0.5f, 1);
  114. contentRT.anchorMax = new Vector2(0.5f, 1);
  115. contentRT.sizeDelta = new Vector2(200, 0);
  116. contentRT.pivot = new Vector2(0.5f, 1);
  117. // Setup UI components.
  118. LoopVerticalScrollRect scrollRect = root.AddComponent<LoopVerticalScrollRect>();
  119. scrollRect.content = contentRT;
  120. scrollRect.viewport = null;
  121. scrollRect.horizontalScrollbar = null;
  122. scrollRect.verticalScrollbar = null;
  123. scrollRect.horizontal = false;
  124. scrollRect.vertical = true;
  125. scrollRect.horizontalScrollbarVisibility = LoopScrollRect.ScrollbarVisibility.Permanent;
  126. scrollRect.verticalScrollbarVisibility = LoopScrollRect.ScrollbarVisibility.Permanent;
  127. scrollRect.horizontalScrollbarSpacing = 0;
  128. scrollRect.verticalScrollbarSpacing = 0;
  129. scrollRect.u_CacheRect = cache.transform as RectTransform;
  130. root.AddComponent<RectMask2D>();
  131. VerticalLayoutGroup layoutGroup = content.AddComponent<VerticalLayoutGroup>();
  132. layoutGroup.childAlignment = TextAnchor.UpperCenter;
  133. layoutGroup.childForceExpandWidth = true;
  134. layoutGroup.childForceExpandHeight = false;
  135. ContentSizeFitter sizeFitter = content.AddComponent<ContentSizeFitter>();
  136. sizeFitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
  137. sizeFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
  138. return root;
  139. }
  140. }
  141. }
  142. #endif