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(); if (component && component.enabled) { return true; } Collider2D component2 = base.GetComponent(); return component2 && component2.enabled; } set { if (this.isEnabled != value) { Collider component = base.gameObject.GetComponent(); if (component != null) { component.enabled = value; this.SetState((!value) ? UIButtonColor.State.Disabled : UIButtonColor.State.Normal, false); } else { Collider2D component2 = base.GetComponent(); 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 onClick = new List(); [NonSerialized] private UISprite mSprite; [NonSerialized] private UI2DSprite mSprite2D; [NonSerialized] private string mNormalSprite; [NonSerialized] private Sprite mNormalSprite2D; }