Delegate01.cs 864 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Examples
  5. {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public class Delegate01 : MonoBehaviour
  10. {
  11. private delegate void TheDelegate(string st); // 定义一个委托
  12. private TheDelegate handler; // 委托变量事件
  13. public void ButtonTriggerA()
  14. {
  15. handler = FunctionA; // 指向FunctionA
  16. // handler += FunctionA; // 如果是+= // 会添加一个事件 // 添加
  17. handler("AAAAAAAAAAA");
  18. return;
  19. }
  20. public void ButtonTriggerB()
  21. {
  22. handler = FunctionB;
  23. handler("BBBBBB");
  24. return;
  25. }
  26. private void FunctionA(string value)
  27. {
  28. Debug.Log("Function A " + value);
  29. return;
  30. }
  31. private void FunctionB(string value)
  32. {
  33. Debug.Log("Function B " + value);
  34. return;
  35. }
  36. }
  37. }