1
0

TEST.cs 978 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. /// <summary>
  6. ///
  7. /// </summary>
  8. public class TEST : MonoBehaviour
  9. {
  10. private EventListener<bool> listenerTest;
  11. private void Start()
  12. {
  13. listenerTest = new EventListener<bool>();
  14. listenerTest.OnVariableChange += Test; // 发报纸给Test
  15. }
  16. private void OnDestroy()
  17. {
  18. listenerTest.OnVariableChange -= Test; // 取消订阅
  19. }
  20. private void Update()
  21. {
  22. listenerTest.Value = Input.GetKey(KeyCode.W);
  23. }
  24. private void Test(bool value)
  25. {
  26. Debug.Log(value);
  27. }
  28. }
  29. public class EventListener<T>
  30. {
  31. public delegate void OnValueChangeDelegate(T newVal);
  32. public event OnValueChangeDelegate OnVariableChange;
  33. private T m_value;
  34. public T Value
  35. {
  36. get
  37. {
  38. return m_value;
  39. }
  40. set
  41. {
  42. if (m_value.Equals(value))
  43. {
  44. return;
  45. }
  46. OnVariableChange?.Invoke(value);
  47. m_value = value;
  48. }
  49. }
  50. }