// Copyright (c) Meta Platforms, Inc. and affiliates. using UnityEngine; using System.Collections; using System; using UnityEngine.UI; namespace Lofelt.NiceVibrations { /// /// A class to add to an Image or SpriteRenderer to have it act like a button with a different sprite for on and off states /// public class MMSpriteReplace : MonoBehaviour { [Header("Sprites")] /// the sprite to use when in the "on" state public Sprite OnSprite; /// the sprite to use when in the "off" state public Sprite OffSprite; [Header("Start settings")] /// 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 SpriteRenderer _spriteRenderer; protected MMTouchButton _mmTouchButton; /// /// On Start we initialize our button /// protected virtual void Start() { Initialization(); } /// /// On init, we grab our image component, and set our sprite in its initial state /// protected virtual void Initialization() { // grabs components _image = GetComponent(); _spriteRenderer = GetComponent(); // grabs button _mmTouchButton = GetComponent(); if (_mmTouchButton != null) { _mmTouchButton.ReturnToInitialSpriteAutomatically = false; } // handles start if ((OnSprite == null) || (OffSprite == null)) { return; } if (_image != null) { if (StartsOn) { _image.sprite = OnSprite; } else { _image.sprite = OffSprite; } } if (_spriteRenderer != null) { if (StartsOn) { _spriteRenderer.sprite = OnSprite; } else { _spriteRenderer.sprite = OffSprite; } } } /// /// A public method to change the sprite /// public virtual void Swap() { if (_image != null) { if (_image.sprite != OnSprite) { SwitchToOnSprite(); } else { SwitchToOffSprite(); } } if (_spriteRenderer != null) { if (_spriteRenderer.sprite != OnSprite) { SwitchToOnSprite(); } else { SwitchToOffSprite(); } } } /// /// a public method to switch to off sprite directly /// public virtual void SwitchToOffSprite() { if ((_image == null) && (_spriteRenderer == null)) { return; } if (OffSprite == null) { return; } SpriteOff(); } /// /// sets the image's sprite to off /// protected virtual void SpriteOff() { if (_image != null) { _image.sprite = OffSprite; } if (_spriteRenderer != null) { _spriteRenderer.sprite = OffSprite; } } /// /// a public method to switch to on sprite directly /// public virtual void SwitchToOnSprite() { if ((_image == null) && (_spriteRenderer == null)) { return; } if (OnSprite == null) { return; } SpriteOn(); } /// /// sets the image's sprite to on /// protected virtual void SpriteOn() { if (_image != null) { _image.sprite = OnSprite; } if (_spriteRenderer != null) { _spriteRenderer.sprite = OnSprite; } } } }