123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- using System;
- using UnityEngine;
- [AddComponentMenu("NGUI/UI/Image Button")]
- public class UIImageButton : MonoBehaviour
- {
- public bool isEnabled
- {
- get
- {
- Collider component = base.gameObject.GetComponent<Collider>();
- return component && component.enabled;
- }
- set
- {
- Collider component = base.gameObject.GetComponent<Collider>();
- if (!component)
- {
- return;
- }
- if (component.enabled != value)
- {
- component.enabled = value;
- this.UpdateImage();
- }
- }
- }
- private void OnEnable()
- {
- if (this.target == null)
- {
- this.target = base.GetComponentInChildren<UISprite>();
- }
- this.UpdateImage();
- }
- private void OnValidate()
- {
- if (this.target != null)
- {
- if (string.IsNullOrEmpty(this.normalSprite))
- {
- this.normalSprite = this.target.spriteName;
- }
- if (string.IsNullOrEmpty(this.hoverSprite))
- {
- this.hoverSprite = this.target.spriteName;
- }
- if (string.IsNullOrEmpty(this.pressedSprite))
- {
- this.pressedSprite = this.target.spriteName;
- }
- if (string.IsNullOrEmpty(this.disabledSprite))
- {
- this.disabledSprite = this.target.spriteName;
- }
- }
- }
- private void UpdateImage()
- {
- if (this.target != null)
- {
- if (this.isEnabled)
- {
- this.SetSprite((!UICamera.IsHighlighted(base.gameObject)) ? this.normalSprite : this.hoverSprite);
- }
- else
- {
- this.SetSprite(this.disabledSprite);
- }
- }
- }
- private void OnHover(bool isOver)
- {
- if (this.isEnabled && this.target != null)
- {
- this.SetSprite((!isOver) ? this.normalSprite : this.hoverSprite);
- }
- }
- private void OnPress(bool pressed)
- {
- if (pressed)
- {
- this.SetSprite(this.pressedSprite);
- }
- else
- {
- this.UpdateImage();
- }
- }
- private void SetSprite(string sprite)
- {
- if (this.target.atlas == null || this.target.atlas.GetSprite(sprite) == null)
- {
- return;
- }
- this.target.spriteName = sprite;
- if (this.pixelSnap)
- {
- this.target.MakePixelPerfect();
- }
- }
- public UISprite target;
- public string normalSprite;
- public string hoverSprite;
- public string pressedSprite;
- public string disabledSprite;
- public bool pixelSnap = true;
- }
|