事件是对象(对委托变量的封装)
    作用:
    1.与委托变量一样,只是功能上比委托变量有更多的限制。(比如:只能通过+=或-=来绑定方法(事件处理 程序)
    2.只能在类内部调用(触发)事件。(即定义阶段和执行阶段在同一个类中执行)

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class _2EventBasics : MonoBehaviour
    5. {
    6. //二、基础事件 事件是对象(对委托变量的封装)
    7. //1 3阶段在同一类内
    8. //1定义阶段
    9. delegate void eventHandler();
    10. event eventHandler eventMethod;
    11. //2.注册阶段
    12. private void OnEnable()
    13. {
    14. eventMethod += eventLogic;
    15. }
    16. void eventLogic()
    17. {
    18. Debug.Log("02--------eventLogic--------");
    19. }
    20. //3.执行阶段
    21. void Start()
    22. {
    23. eventMethod();
    24. }
    25. private void OnDestroy()
    26. {
    27. eventMethod -= eventLogic;
    28. }
    29. }