UIButtonColor.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using System;
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. [AddComponentMenu("NGUI/Interaction/Button Color")]
  5. public class UIButtonColor : UIWidgetContainer
  6. {
  7. public UIButtonColor.State state
  8. {
  9. get
  10. {
  11. return this.mState;
  12. }
  13. set
  14. {
  15. this.SetState(value, false);
  16. }
  17. }
  18. public Color defaultColor
  19. {
  20. get
  21. {
  22. if (!this.mInitDone)
  23. {
  24. this.OnInit();
  25. }
  26. return this.mDefaultColor;
  27. }
  28. set
  29. {
  30. if (!this.mInitDone)
  31. {
  32. this.OnInit();
  33. }
  34. this.mDefaultColor = value;
  35. UIButtonColor.State state = this.mState;
  36. this.mState = UIButtonColor.State.Disabled;
  37. this.SetState(state, false);
  38. }
  39. }
  40. public virtual bool isEnabled
  41. {
  42. get
  43. {
  44. return base.enabled;
  45. }
  46. set
  47. {
  48. base.enabled = value;
  49. }
  50. }
  51. public void ResetDefaultColor()
  52. {
  53. this.defaultColor = this.mStartingColor;
  54. }
  55. private void Start()
  56. {
  57. if (!this.mInitDone)
  58. {
  59. this.OnInit();
  60. }
  61. if (!this.isEnabled)
  62. {
  63. this.SetState(UIButtonColor.State.Disabled, true);
  64. }
  65. }
  66. protected virtual void OnInit()
  67. {
  68. this.mInitDone = true;
  69. if (this.tweenTarget == null)
  70. {
  71. this.tweenTarget = base.gameObject;
  72. }
  73. if (this.tweenTarget != null)
  74. {
  75. this.mWidget = this.tweenTarget.GetComponent<UIWidget>();
  76. }
  77. if (this.mWidget != null)
  78. {
  79. this.mDefaultColor = this.mWidget.color;
  80. this.mStartingColor = this.mDefaultColor;
  81. }
  82. else if (this.tweenTarget != null)
  83. {
  84. Renderer component = this.tweenTarget.GetComponent<Renderer>();
  85. if (component != null)
  86. {
  87. this.mDefaultColor = ((!Application.isPlaying) ? component.sharedMaterial.color : component.material.color);
  88. this.mStartingColor = this.mDefaultColor;
  89. }
  90. else
  91. {
  92. Light component2 = this.tweenTarget.GetComponent<Light>();
  93. if (component2 != null)
  94. {
  95. this.mDefaultColor = component2.color;
  96. this.mStartingColor = this.mDefaultColor;
  97. }
  98. else
  99. {
  100. this.tweenTarget = null;
  101. this.mInitDone = false;
  102. }
  103. }
  104. }
  105. }
  106. protected virtual void OnEnable()
  107. {
  108. if (this.mInitDone)
  109. {
  110. this.OnHover(UICamera.IsHighlighted(base.gameObject));
  111. }
  112. if (UICamera.currentTouch != null)
  113. {
  114. if (UICamera.currentTouch.pressed == base.gameObject)
  115. {
  116. this.OnPress(true);
  117. }
  118. else if (UICamera.currentTouch.current == base.gameObject)
  119. {
  120. this.OnHover(true);
  121. }
  122. }
  123. }
  124. protected virtual void OnDisable()
  125. {
  126. if (this.mInitDone && this.tweenTarget != null)
  127. {
  128. this.SetState(UIButtonColor.State.Normal, true);
  129. TweenColor component = this.tweenTarget.GetComponent<TweenColor>();
  130. if (component != null)
  131. {
  132. component.value = this.mDefaultColor;
  133. component.enabled = false;
  134. }
  135. }
  136. }
  137. protected virtual void OnHover(bool isOver)
  138. {
  139. if (this.isEnabled)
  140. {
  141. if (!this.mInitDone)
  142. {
  143. this.OnInit();
  144. }
  145. if (this.tweenTarget != null)
  146. {
  147. this.SetState((!isOver) ? UIButtonColor.State.Normal : UIButtonColor.State.Hover, false);
  148. }
  149. }
  150. }
  151. protected virtual void OnPress(bool isPressed)
  152. {
  153. if (this.isEnabled && UICamera.currentTouch != null)
  154. {
  155. if (!this.mInitDone)
  156. {
  157. this.OnInit();
  158. }
  159. if (this.tweenTarget != null)
  160. {
  161. if (isPressed)
  162. {
  163. this.SetState(UIButtonColor.State.Pressed, false);
  164. }
  165. else if (UICamera.currentTouch.current == base.gameObject)
  166. {
  167. if (UICamera.currentScheme == UICamera.ControlScheme.Controller)
  168. {
  169. this.SetState(UIButtonColor.State.Hover, false);
  170. }
  171. else if (UICamera.currentScheme == UICamera.ControlScheme.Mouse && UICamera.hoveredObject == base.gameObject)
  172. {
  173. this.SetState(UIButtonColor.State.Hover, false);
  174. }
  175. else
  176. {
  177. this.SetState(UIButtonColor.State.Normal, false);
  178. }
  179. }
  180. else
  181. {
  182. this.SetState(UIButtonColor.State.Normal, false);
  183. }
  184. }
  185. }
  186. }
  187. protected virtual void OnDragOver()
  188. {
  189. if (this.isEnabled)
  190. {
  191. if (!this.mInitDone)
  192. {
  193. this.OnInit();
  194. }
  195. if (this.tweenTarget != null)
  196. {
  197. this.SetState(UIButtonColor.State.Pressed, false);
  198. }
  199. }
  200. }
  201. protected virtual void OnDragOut()
  202. {
  203. if (this.isEnabled)
  204. {
  205. if (!this.mInitDone)
  206. {
  207. this.OnInit();
  208. }
  209. if (this.tweenTarget != null)
  210. {
  211. this.SetState(UIButtonColor.State.Normal, false);
  212. }
  213. }
  214. }
  215. protected virtual void OnSelect(bool isSelected)
  216. {
  217. if (this.isEnabled && this.tweenTarget != null)
  218. {
  219. if (UICamera.currentScheme == UICamera.ControlScheme.Controller)
  220. {
  221. this.OnHover(isSelected);
  222. }
  223. else if (!isSelected && UICamera.touchCount < 2)
  224. {
  225. this.OnHover(isSelected);
  226. }
  227. }
  228. }
  229. public virtual void SetState(UIButtonColor.State state, bool instant)
  230. {
  231. if (!this.mInitDone)
  232. {
  233. this.mInitDone = true;
  234. this.OnInit();
  235. }
  236. if (this.mState != state)
  237. {
  238. this.mState = state;
  239. this.UpdateColor(instant);
  240. }
  241. }
  242. public void UpdateColor(bool instant)
  243. {
  244. if (this.tweenTarget != null)
  245. {
  246. TweenColor tweenColor;
  247. switch (this.mState)
  248. {
  249. case UIButtonColor.State.Hover:
  250. tweenColor = TweenColor.Begin(this.tweenTarget, this.duration, this.hover);
  251. break;
  252. case UIButtonColor.State.Pressed:
  253. tweenColor = TweenColor.Begin(this.tweenTarget, this.duration, this.pressed);
  254. break;
  255. case UIButtonColor.State.Disabled:
  256. tweenColor = TweenColor.Begin(this.tweenTarget, this.duration, this.disabledColor);
  257. break;
  258. default:
  259. tweenColor = TweenColor.Begin(this.tweenTarget, this.duration, this.mDefaultColor);
  260. break;
  261. }
  262. if (instant && tweenColor != null)
  263. {
  264. tweenColor.value = tweenColor.to;
  265. tweenColor.enabled = false;
  266. }
  267. }
  268. }
  269. public GameObject tweenTarget;
  270. public Color hover = new Color(0.882352948f, 0.784313738f, 0.5882353f, 1f);
  271. public Color pressed = new Color(0.7176471f, 0.6392157f, 0.482352942f, 1f);
  272. public Color disabledColor = Color.grey;
  273. public float duration = 0.2f;
  274. [NonSerialized]
  275. protected Color mStartingColor;
  276. [NonSerialized]
  277. protected Color mDefaultColor;
  278. [NonSerialized]
  279. protected bool mInitDone;
  280. [NonSerialized]
  281. protected UIWidget mWidget;
  282. [NonSerialized]
  283. protected UIButtonColor.State mState;
  284. public enum State
  285. {
  286. Normal,
  287. Hover,
  288. Pressed,
  289. Disabled
  290. }
  291. }