StyleComponent.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. namespace SRF.UI
  2. {
  3. using Internal;
  4. using UnityEngine;
  5. using UnityEngine.Serialization;
  6. using UnityEngine.UI;
  7. [ExecuteInEditMode]
  8. [AddComponentMenu(ComponentMenuPaths.StyleComponent)]
  9. public class StyleComponent : SRMonoBehaviour
  10. {
  11. private Style _activeStyle;
  12. private StyleRoot _cachedRoot;
  13. private Graphic _graphic;
  14. private bool _hasStarted;
  15. private Image _image;
  16. private Selectable _selectable;
  17. [SerializeField] [FormerlySerializedAs("StyleKey")] [HideInInspector] private string _styleKey;
  18. public bool IgnoreImage = false;
  19. public string StyleKey
  20. {
  21. get { return _styleKey; }
  22. set
  23. {
  24. _styleKey = value;
  25. Refresh(false);
  26. }
  27. }
  28. private void Start()
  29. {
  30. Refresh(true);
  31. _hasStarted = true;
  32. }
  33. private void OnEnable()
  34. {
  35. if (_hasStarted)
  36. {
  37. Refresh(false);
  38. }
  39. }
  40. #if UNITY_EDITOR
  41. /// <summary>
  42. /// This method is not included in exported builds - don't worry about it showing up in the profiler.
  43. /// </summary>
  44. private void Update()
  45. {
  46. if (!Application.isPlaying)
  47. {
  48. ApplyStyle();
  49. }
  50. }
  51. #endif
  52. public void Refresh(bool invalidateCache)
  53. {
  54. if (string.IsNullOrEmpty(StyleKey))
  55. {
  56. _activeStyle = null;
  57. return;
  58. }
  59. if (!isActiveAndEnabled)
  60. {
  61. _cachedRoot = null;
  62. return;
  63. }
  64. if (_cachedRoot == null || invalidateCache)
  65. {
  66. _cachedRoot = GetStyleRoot();
  67. }
  68. if (_cachedRoot == null)
  69. {
  70. Debug.LogWarning("[StyleComponent] No active StyleRoot object found in parents.", this);
  71. _activeStyle = null;
  72. return;
  73. }
  74. var s = _cachedRoot.GetStyle(StyleKey);
  75. if (s == null)
  76. {
  77. Debug.LogWarning("[StyleComponent] Style not found ({0})".Fmt(StyleKey), this);
  78. _activeStyle = null;
  79. return;
  80. }
  81. _activeStyle = s;
  82. ApplyStyle();
  83. }
  84. /// <summary>
  85. /// Find the nearest enable style root component in parents
  86. /// </summary>
  87. /// <returns></returns>
  88. private StyleRoot GetStyleRoot()
  89. {
  90. var t = CachedTransform;
  91. StyleRoot root;
  92. var i = 0;
  93. do
  94. {
  95. root = t.GetComponentInParent<StyleRoot>();
  96. if (root != null)
  97. {
  98. t = root.transform.parent;
  99. }
  100. ++i;
  101. if (i > 100)
  102. {
  103. Debug.LogWarning("Breaking Loop");
  104. break;
  105. }
  106. } while ((root != null && !root.enabled) && t != null);
  107. return root;
  108. }
  109. private void ApplyStyle()
  110. {
  111. if (_activeStyle == null)
  112. {
  113. return;
  114. }
  115. if (_graphic == null)
  116. {
  117. _graphic = GetComponent<Graphic>();
  118. }
  119. if (_selectable == null)
  120. {
  121. _selectable = GetComponent<Selectable>();
  122. }
  123. if (_image == null)
  124. {
  125. _image = GetComponent<Image>();
  126. }
  127. if (!IgnoreImage && _image != null)
  128. {
  129. _image.sprite = _activeStyle.Image;
  130. }
  131. if (_selectable != null)
  132. {
  133. var colours = _selectable.colors;
  134. colours.normalColor = _activeStyle.NormalColor;
  135. colours.highlightedColor = _activeStyle.HoverColor;
  136. colours.pressedColor = _activeStyle.ActiveColor;
  137. colours.disabledColor = _activeStyle.DisabledColor;
  138. colours.colorMultiplier = 1f;
  139. _selectable.colors = colours;
  140. if (_graphic != null)
  141. {
  142. _graphic.color = Color.white;
  143. }
  144. }
  145. else if (_graphic != null)
  146. {
  147. _graphic.color = _activeStyle.NormalColor;
  148. }
  149. }
  150. private void SRStyleDirty()
  151. {
  152. // If inactive, invalidate the cached root and return. Next time it is enabled
  153. // a new root will be found
  154. if (!CachedGameObject.activeInHierarchy)
  155. {
  156. _cachedRoot = null;
  157. return;
  158. }
  159. Refresh(true);
  160. }
  161. }
  162. }