TogglEvent.cs 736 B

123456789101112131415161718192021222324252627
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.UI;
  6. [RequireComponent(typeof(Toggle))]
  7. public class TogglEvent : MonoBehaviour
  8. {
  9. private Toggle m_Toggle = null;
  10. [SerializeField] private UnityEvent m_OnToggleTrue = new UnityEvent();
  11. [SerializeField] private UnityEvent m_OnToggleFalse = new UnityEvent();
  12. // Start is called before the first frame update
  13. void Start() {
  14. m_Toggle = GetComponent<Toggle>();
  15. m_Toggle.onValueChanged.AddListener((v) => {
  16. if (v) {
  17. m_OnToggleTrue.Invoke();
  18. }
  19. else {
  20. m_OnToggleFalse.Invoke();
  21. }
  22. });
  23. }
  24. }