SelectedButton.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. 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 Select()
  23. {
  24. //if(selected) return;
  25. selected = true;
  26. if(defaultSprite == null) defaultSprite = image.sprite;
  27. //image.sprite = button.spriteState.selectedSprite;
  28. var btnStates = button.spriteState;
  29. btnStates.disabledSprite = selectedSprite;
  30. button.spriteState = btnStates;
  31. button.interactable = false;
  32. }
  33. public void Deselect()
  34. {
  35. //if(!selected) return;
  36. selected = false;
  37. //image.sprite = selectedSprite;
  38. var btnStates = button.spriteState;
  39. btnStates.disabledSprite = disabledSprite;
  40. button.spriteState = btnStates;
  41. button.interactable = isInteractable;
  42. }
  43. }
  44. }