using UnityEngine; using System.Collections; using System; using MoreMountains.Tools; using UnityEngine.UI; namespace MoreMountains.Tools { /// /// A class to add to an image to have it act like a button with a different sprite for on and off states /// public class MMDebugMenuSpriteReplace : MonoBehaviour { /// the sprite to use when in the "on" state public Sprite OnSprite; /// the sprite to use when in the "off" state public Sprite OffSprite; /// if this is true, the button will start if "on" state public bool StartsOn = true; /// the current state of the button public bool CurrentValue { get { return (_image.sprite == OnSprite); } } protected Image _image; protected MMTouchButton _mmTouchButton; /// /// On Start we initialize our button /// protected virtual void Awake() { //Initialization (); } /// /// On init, we grab our image component, and set our sprite in its initial state /// public virtual void Initialization() { _image = this.gameObject.GetComponent (); _mmTouchButton = this.gameObject.GetComponent (); if (_mmTouchButton != null) { _mmTouchButton.ReturnToInitialSpriteAutomatically = false; } if (_image == null) { return; } if ((OnSprite == null) || (OffSprite == null)) { return; } if (StartsOn) { _image.sprite = OnSprite; } else { _image.sprite = OffSprite; } } /// /// A public method to change the sprite /// public virtual void Swap() { if (_image.sprite != OnSprite) { SwitchToOnSprite (); } else { SwitchToOffSprite (); } } /// /// a public method to switch to off sprite directly /// public virtual void SwitchToOffSprite() { if (_image == null) { return; } if (OffSprite == null) { return; } SpriteOff (); } /// /// sets the image's sprite to off /// protected virtual void SpriteOff() { _image.sprite = OffSprite; } /// /// a public method to switch to on sprite directly /// public virtual void SwitchToOnSprite() { if (_image == null) { return; } if (OnSprite == null) { return; } SpriteOn (); } /// /// sets the image's sprite to on /// protected virtual void SpriteOn() { _image.sprite = OnSprite; } } }