DelegateScript.cs 733 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Examples
  5. {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public class DelegateScript : MonoBehaviour
  10. {
  11. private delegate void DelegateA(int num);
  12. private delegate void DelegateB(int numa, int numb);
  13. DelegateA da;
  14. DelegateB db;
  15. private void Start()
  16. {
  17. // 委托类型DelegateA实例da引用方法PrintNum
  18. da = PrintNum;
  19. da(50);
  20. // 传两个参自动重载
  21. db = PrintNum;
  22. db(50, 50);
  23. }
  24. private void PrintNum(int num)
  25. {
  26. Debug.Log("Print Num: " + num);
  27. }
  28. private void PrintNum(int numa, int numb)
  29. {
  30. Debug.Log("Double Num: " + numa * numb);
  31. }
  32. }
  33. }