123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- [AddComponentMenu("NGUI/Interaction/Button")]
- public class UIButton : UIButtonColor
- {
- public override bool isEnabled
- {
- get
- {
- if (!base.enabled)
- {
- return false;
- }
- Collider component = base.gameObject.GetComponent<Collider>();
- if (component && component.enabled)
- {
- return true;
- }
- Collider2D component2 = base.GetComponent<Collider2D>();
- return component2 && component2.enabled;
- }
- set
- {
- if (this.isEnabled != value)
- {
- Collider component = base.gameObject.GetComponent<Collider>();
- if (component != null)
- {
- component.enabled = value;
- this.SetState((!value) ? UIButtonColor.State.Disabled : UIButtonColor.State.Normal, false);
- }
- else
- {
- Collider2D component2 = base.GetComponent<Collider2D>();
- if (component2 != null)
- {
- component2.enabled = value;
- this.SetState((!value) ? UIButtonColor.State.Disabled : UIButtonColor.State.Normal, false);
- }
- else
- {
- base.enabled = value;
- }
- }
- }
- }
- }
- public string normalSprite
- {
- get
- {
- if (!this.mInitDone)
- {
- this.OnInit();
- }
- return this.mNormalSprite;
- }
- set
- {
- if (!this.mInitDone)
- {
- this.OnInit();
- }
- if (this.mSprite != null && !string.IsNullOrEmpty(this.mNormalSprite) && this.mNormalSprite == this.mSprite.spriteName)
- {
- this.mNormalSprite = value;
- this.SetSprite(value);
- NGUITools.SetDirty(this.mSprite);
- }
- else
- {
- this.mNormalSprite = value;
- if (this.mState == UIButtonColor.State.Normal)
- {
- this.SetSprite(value);
- }
- }
- }
- }
- public Sprite normalSprite2D
- {
- get
- {
- if (!this.mInitDone)
- {
- this.OnInit();
- }
- return this.mNormalSprite2D;
- }
- set
- {
- if (!this.mInitDone)
- {
- this.OnInit();
- }
- if (this.mSprite2D != null && this.mNormalSprite2D == this.mSprite2D.sprite2D)
- {
- this.mNormalSprite2D = value;
- this.SetSprite(value);
- NGUITools.SetDirty(this.mSprite);
- }
- else
- {
- this.mNormalSprite2D = value;
- if (this.mState == UIButtonColor.State.Normal)
- {
- this.SetSprite(value);
- }
- }
- }
- }
- protected override void OnInit()
- {
- base.OnInit();
- this.mSprite = (this.mWidget as UISprite);
- this.mSprite2D = (this.mWidget as UI2DSprite);
- if (this.mSprite != null)
- {
- this.mNormalSprite = this.mSprite.spriteName;
- }
- if (this.mSprite2D != null)
- {
- this.mNormalSprite2D = this.mSprite2D.sprite2D;
- }
- }
- protected override void OnEnable()
- {
- if (this.isEnabled)
- {
- if (this.mInitDone)
- {
- if (UICamera.currentScheme == UICamera.ControlScheme.Controller)
- {
- this.OnHover(UICamera.selectedObject == base.gameObject);
- }
- else if (UICamera.currentScheme == UICamera.ControlScheme.Mouse)
- {
- this.OnHover(UICamera.hoveredObject == base.gameObject);
- }
- else
- {
- this.SetState(UIButtonColor.State.Normal, false);
- }
- }
- }
- else
- {
- this.SetState(UIButtonColor.State.Disabled, true);
- }
- }
- protected override void OnDragOver()
- {
- if (this.isEnabled && (this.dragHighlight || UICamera.currentTouch.pressed == base.gameObject))
- {
- base.OnDragOver();
- }
- }
- protected override void OnDragOut()
- {
- if (this.isEnabled && (this.dragHighlight || UICamera.currentTouch.pressed == base.gameObject))
- {
- base.OnDragOut();
- }
- }
- protected virtual void OnClick()
- {
- if (UIButton.current == null && this.isEnabled)
- {
- UIButton.current = this;
- EventDelegate.Execute(this.onClick);
- UIButton.current = null;
- this.PlaySoundClick();
- this.beforeHover = false;
- }
- }
- public virtual void PlaySoundClick()
- {
- if (this.clickMouseSe == SoundMgr.SeType.Self)
- {
- GameMain.Instance.SoundMgr.PlaySystem(this.clickMouseSeFile);
- }
- else
- {
- GameMain.Instance.SoundMgr.PlaySystem(this.clickMouseSe);
- }
- }
- public virtual void PlaySoundHover()
- {
- if (this.clickMouseSe == SoundMgr.SeType.Self)
- {
- GameMain.Instance.SoundMgr.PlaySystem(this.hoverMouseSeFile);
- }
- else
- {
- GameMain.Instance.SoundMgr.PlaySystem(this.hoverMouseSe);
- }
- }
- public override void SetState(UIButtonColor.State state, bool immediate)
- {
- base.SetState(state, immediate);
- if (this.mSprite != null)
- {
- switch (state)
- {
- case UIButtonColor.State.Normal:
- this.SetSprite(this.mNormalSprite);
- break;
- case UIButtonColor.State.Hover:
- this.SetSprite((!string.IsNullOrEmpty(this.hoverSprite)) ? this.hoverSprite : this.mNormalSprite);
- break;
- case UIButtonColor.State.Pressed:
- this.SetSprite(this.pressedSprite);
- break;
- case UIButtonColor.State.Disabled:
- this.SetSprite(this.disabledSprite);
- break;
- }
- }
- else if (this.mSprite2D != null)
- {
- switch (state)
- {
- case UIButtonColor.State.Normal:
- this.SetSprite(this.mNormalSprite2D);
- break;
- case UIButtonColor.State.Hover:
- this.SetSprite((!(this.hoverSprite2D == null)) ? this.hoverSprite2D : this.mNormalSprite2D);
- break;
- case UIButtonColor.State.Pressed:
- this.SetSprite(this.pressedSprite2D);
- break;
- case UIButtonColor.State.Disabled:
- this.SetSprite(this.disabledSprite2D);
- break;
- }
- }
- if (state == UIButtonColor.State.Hover)
- {
- if (!this.beforeHover)
- {
- this.PlaySoundHover();
- }
- }
- else if (state == UIButtonColor.State.Pressed)
- {
- this.beforeHover = true;
- }
- else
- {
- this.beforeHover = false;
- }
- }
- protected void SetSprite(string sp)
- {
- if (this.mSprite != null && !string.IsNullOrEmpty(sp) && this.mSprite.spriteName != sp)
- {
- this.mSprite.spriteName = sp;
- if (this.pixelSnap)
- {
- this.mSprite.MakePixelPerfect();
- }
- }
- }
- protected void SetSprite(Sprite sp)
- {
- if (sp != null && this.mSprite2D != null && this.mSprite2D.sprite2D != sp)
- {
- this.mSprite2D.sprite2D = sp;
- if (this.pixelSnap)
- {
- this.mSprite2D.MakePixelPerfect();
- }
- }
- }
- public static UIButton current;
- public bool dragHighlight;
- public string hoverSprite;
- public string pressedSprite;
- public string disabledSprite;
- public SoundMgr.SeType clickMouseSe;
- public string clickMouseSeFile = string.Empty;
- public SoundMgr.SeType hoverMouseSe = SoundMgr.SeType.Hover;
- public string hoverMouseSeFile = string.Empty;
- private bool beforeHover;
- public Sprite hoverSprite2D;
- public Sprite pressedSprite2D;
- public Sprite disabledSprite2D;
- public bool pixelSnap;
- public List<EventDelegate> onClick = new List<EventDelegate>();
- [NonSerialized]
- private UISprite mSprite;
- [NonSerialized]
- private UI2DSprite mSprite2D;
- [NonSerialized]
- private string mNormalSprite;
- [NonSerialized]
- private Sprite mNormalSprite2D;
- }
|