ToggleButton.cs 768 B

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace XRTool.UI
  7. {
  8. /// <summary>
  9. /// 简化的Toggle
  10. /// </summary>
  11. public class ToggleButton : Button
  12. {
  13. public Sprite closeImg;
  14. public Sprite openImg;
  15. public bool isOn = false;
  16. public event Action<bool> OnValueChanged;
  17. protected override void Start()
  18. {
  19. base.Start();
  20. image.sprite = isOn ? openImg : closeImg;
  21. onClick.AddListener(OnClickAction);
  22. }
  23. private void OnClickAction()
  24. {
  25. isOn = !isOn;
  26. image.sprite = isOn ? openImg : closeImg;
  27. OnValueChanged?.Invoke(isOn);
  28. }
  29. }
  30. }