| | 1 | | using UnityEngine; |
| | 2 | | using UnityEngine.UI; |
| | 3 | |
|
| | 4 | | [RequireComponent(typeof(Toggle))] |
| | 5 | | public class ToggleSpriteSwap : MonoBehaviour |
| | 6 | | { |
| | 7 | | public Image targetImage; |
| | 8 | | public Sprite spriteOn; |
| | 9 | | public Sprite spriteOff; |
| | 10 | | private Toggle targetToggle; |
| | 11 | |
|
| 0 | 12 | | private void Awake() { targetToggle = GetComponent<Toggle>(); } |
| | 13 | |
|
| 0 | 14 | | protected void Start() { SetSprite(targetToggle.isOn); } |
| | 15 | |
|
| 0 | 16 | | private void OnEnable() { targetToggle.onValueChanged.AddListener(SetSprite); } |
| | 17 | |
|
| 0 | 18 | | private void OnDisable() { targetToggle.onValueChanged.RemoveListener(SetSprite); } |
| | 19 | |
|
| | 20 | | private void SetSprite(bool newEnabled) |
| | 21 | | { |
| 0 | 22 | | if (targetImage != null) |
| 0 | 23 | | targetImage.sprite = newEnabled ? spriteOn : spriteOff; |
| 0 | 24 | | } |
| | 25 | | } |