UIButton.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [AddComponentMenu("NGUI/Interaction/Button")]
  5. public class UIButton : UIButtonColor
  6. {
  7. public override bool isEnabled
  8. {
  9. get
  10. {
  11. if (!base.enabled)
  12. {
  13. return false;
  14. }
  15. Collider component = base.gameObject.GetComponent<Collider>();
  16. if (component && component.enabled)
  17. {
  18. return true;
  19. }
  20. Collider2D component2 = base.GetComponent<Collider2D>();
  21. return component2 && component2.enabled;
  22. }
  23. set
  24. {
  25. if (this.isEnabled != value)
  26. {
  27. Collider component = base.gameObject.GetComponent<Collider>();
  28. if (component != null)
  29. {
  30. component.enabled = value;
  31. this.SetState((!value) ? UIButtonColor.State.Disabled : UIButtonColor.State.Normal, false);
  32. }
  33. else
  34. {
  35. Collider2D component2 = base.GetComponent<Collider2D>();
  36. if (component2 != null)
  37. {
  38. component2.enabled = value;
  39. this.SetState((!value) ? UIButtonColor.State.Disabled : UIButtonColor.State.Normal, false);
  40. }
  41. else
  42. {
  43. base.enabled = value;
  44. }
  45. }
  46. }
  47. }
  48. }
  49. public string normalSprite
  50. {
  51. get
  52. {
  53. if (!this.mInitDone)
  54. {
  55. this.OnInit();
  56. }
  57. return this.mNormalSprite;
  58. }
  59. set
  60. {
  61. if (!this.mInitDone)
  62. {
  63. this.OnInit();
  64. }
  65. if (this.mSprite != null && !string.IsNullOrEmpty(this.mNormalSprite) && this.mNormalSprite == this.mSprite.spriteName)
  66. {
  67. this.mNormalSprite = value;
  68. this.SetSprite(value);
  69. NGUITools.SetDirty(this.mSprite);
  70. }
  71. else
  72. {
  73. this.mNormalSprite = value;
  74. if (this.mState == UIButtonColor.State.Normal)
  75. {
  76. this.SetSprite(value);
  77. }
  78. }
  79. }
  80. }
  81. public Sprite normalSprite2D
  82. {
  83. get
  84. {
  85. if (!this.mInitDone)
  86. {
  87. this.OnInit();
  88. }
  89. return this.mNormalSprite2D;
  90. }
  91. set
  92. {
  93. if (!this.mInitDone)
  94. {
  95. this.OnInit();
  96. }
  97. if (this.mSprite2D != null && this.mNormalSprite2D == this.mSprite2D.sprite2D)
  98. {
  99. this.mNormalSprite2D = value;
  100. this.SetSprite(value);
  101. NGUITools.SetDirty(this.mSprite);
  102. }
  103. else
  104. {
  105. this.mNormalSprite2D = value;
  106. if (this.mState == UIButtonColor.State.Normal)
  107. {
  108. this.SetSprite(value);
  109. }
  110. }
  111. }
  112. }
  113. protected override void OnInit()
  114. {
  115. base.OnInit();
  116. this.mSprite = (this.mWidget as UISprite);
  117. this.mSprite2D = (this.mWidget as UI2DSprite);
  118. if (this.mSprite != null)
  119. {
  120. this.mNormalSprite = this.mSprite.spriteName;
  121. }
  122. if (this.mSprite2D != null)
  123. {
  124. this.mNormalSprite2D = this.mSprite2D.sprite2D;
  125. }
  126. }
  127. protected override void OnEnable()
  128. {
  129. if (this.isEnabled)
  130. {
  131. if (this.mInitDone)
  132. {
  133. if (UICamera.currentScheme == UICamera.ControlScheme.Controller)
  134. {
  135. this.OnHover(UICamera.selectedObject == base.gameObject);
  136. }
  137. else if (UICamera.currentScheme == UICamera.ControlScheme.Mouse)
  138. {
  139. this.OnHover(UICamera.hoveredObject == base.gameObject);
  140. }
  141. else
  142. {
  143. this.SetState(UIButtonColor.State.Normal, false);
  144. }
  145. }
  146. }
  147. else
  148. {
  149. this.SetState(UIButtonColor.State.Disabled, true);
  150. }
  151. }
  152. protected override void OnDragOver()
  153. {
  154. if (this.isEnabled && (this.dragHighlight || UICamera.currentTouch.pressed == base.gameObject))
  155. {
  156. base.OnDragOver();
  157. }
  158. }
  159. protected override void OnDragOut()
  160. {
  161. if (this.isEnabled && (this.dragHighlight || UICamera.currentTouch.pressed == base.gameObject))
  162. {
  163. base.OnDragOut();
  164. }
  165. }
  166. protected virtual void OnClick()
  167. {
  168. if (UIButton.current == null && this.isEnabled)
  169. {
  170. UIButton.current = this;
  171. EventDelegate.Execute(this.onClick);
  172. UIButton.current = null;
  173. this.PlaySoundClick();
  174. this.beforeHover = false;
  175. }
  176. }
  177. public virtual void PlaySoundClick()
  178. {
  179. if (this.clickMouseSe == SoundMgr.SeType.Self)
  180. {
  181. GameMain.Instance.SoundMgr.PlaySystem(this.clickMouseSeFile);
  182. }
  183. else
  184. {
  185. GameMain.Instance.SoundMgr.PlaySystem(this.clickMouseSe);
  186. }
  187. }
  188. public virtual void PlaySoundHover()
  189. {
  190. if (this.clickMouseSe == SoundMgr.SeType.Self)
  191. {
  192. GameMain.Instance.SoundMgr.PlaySystem(this.hoverMouseSeFile);
  193. }
  194. else
  195. {
  196. GameMain.Instance.SoundMgr.PlaySystem(this.hoverMouseSe);
  197. }
  198. }
  199. public override void SetState(UIButtonColor.State state, bool immediate)
  200. {
  201. base.SetState(state, immediate);
  202. if (this.mSprite != null)
  203. {
  204. switch (state)
  205. {
  206. case UIButtonColor.State.Normal:
  207. this.SetSprite(this.mNormalSprite);
  208. break;
  209. case UIButtonColor.State.Hover:
  210. this.SetSprite((!string.IsNullOrEmpty(this.hoverSprite)) ? this.hoverSprite : this.mNormalSprite);
  211. break;
  212. case UIButtonColor.State.Pressed:
  213. this.SetSprite(this.pressedSprite);
  214. break;
  215. case UIButtonColor.State.Disabled:
  216. this.SetSprite(this.disabledSprite);
  217. break;
  218. }
  219. }
  220. else if (this.mSprite2D != null)
  221. {
  222. switch (state)
  223. {
  224. case UIButtonColor.State.Normal:
  225. this.SetSprite(this.mNormalSprite2D);
  226. break;
  227. case UIButtonColor.State.Hover:
  228. this.SetSprite((!(this.hoverSprite2D == null)) ? this.hoverSprite2D : this.mNormalSprite2D);
  229. break;
  230. case UIButtonColor.State.Pressed:
  231. this.SetSprite(this.pressedSprite2D);
  232. break;
  233. case UIButtonColor.State.Disabled:
  234. this.SetSprite(this.disabledSprite2D);
  235. break;
  236. }
  237. }
  238. if (state == UIButtonColor.State.Hover)
  239. {
  240. if (!this.beforeHover)
  241. {
  242. this.PlaySoundHover();
  243. }
  244. }
  245. else if (state == UIButtonColor.State.Pressed)
  246. {
  247. this.beforeHover = true;
  248. }
  249. else
  250. {
  251. this.beforeHover = false;
  252. }
  253. }
  254. protected void SetSprite(string sp)
  255. {
  256. if (this.mSprite != null && !string.IsNullOrEmpty(sp) && this.mSprite.spriteName != sp)
  257. {
  258. this.mSprite.spriteName = sp;
  259. if (this.pixelSnap)
  260. {
  261. this.mSprite.MakePixelPerfect();
  262. }
  263. }
  264. }
  265. protected void SetSprite(Sprite sp)
  266. {
  267. if (sp != null && this.mSprite2D != null && this.mSprite2D.sprite2D != sp)
  268. {
  269. this.mSprite2D.sprite2D = sp;
  270. if (this.pixelSnap)
  271. {
  272. this.mSprite2D.MakePixelPerfect();
  273. }
  274. }
  275. }
  276. public static UIButton current;
  277. public bool dragHighlight;
  278. public string hoverSprite;
  279. public string pressedSprite;
  280. public string disabledSprite;
  281. public SoundMgr.SeType clickMouseSe;
  282. public string clickMouseSeFile = string.Empty;
  283. public SoundMgr.SeType hoverMouseSe = SoundMgr.SeType.Hover;
  284. public string hoverMouseSeFile = string.Empty;
  285. private bool beforeHover;
  286. public Sprite hoverSprite2D;
  287. public Sprite pressedSprite2D;
  288. public Sprite disabledSprite2D;
  289. public bool pixelSnap;
  290. public List<EventDelegate> onClick = new List<EventDelegate>();
  291. [NonSerialized]
  292. private UISprite mSprite;
  293. [NonSerialized]
  294. private UI2DSprite mSprite2D;
  295. [NonSerialized]
  296. private string mNormalSprite;
  297. [NonSerialized]
  298. private Sprite mNormalSprite2D;
  299. }