SelectedButton.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Sirenix.OdinInspector;
  6. namespace KairoEngine.UI.InteractionHandler
  7. {
  8. [HideMonoScript]
  9. public class SelectedButton : MonoBehaviour
  10. {
  11. public Image image;
  12. public Button button;
  13. [OnValueChanged("Toggle"), OnInspectorInit("Toggle")] public bool selected = false;
  14. [SerializeField] private Sprite defaultSprite;
  15. [SerializeField] private Sprite selectedSprite;
  16. [SerializeField] private Sprite disabledSprite;
  17. [SerializeField] public bool isInteractable = true;
  18. private void Start()
  19. {
  20. if(defaultSprite == null) defaultSprite = image.sprite;
  21. }
  22. public void Toggle()
  23. {
  24. if(selected) Select();
  25. else Deselect();
  26. }
  27. public void Select()
  28. {
  29. //if(selected) return;
  30. selected = true;
  31. if(defaultSprite == null) defaultSprite = image.sprite;
  32. //image.sprite = button.spriteState.selectedSprite;
  33. var btnStates = button.spriteState;
  34. btnStates.disabledSprite = selectedSprite;
  35. button.spriteState = btnStates;
  36. button.interactable = false;
  37. }
  38. public void Deselect()
  39. {
  40. //if(!selected) return;
  41. selected = false;
  42. //image.sprite = selectedSprite;
  43. var btnStates = button.spriteState;
  44. btnStates.disabledSprite = disabledSprite;
  45. button.spriteState = btnStates;
  46. button.interactable = isInteractable;
  47. }
  48. }
  49. }