事件是对象(对委托变量的封装)
作用:
1.与委托变量一样,只是功能上比委托变量有更多的限制。(比如:只能通过+=或-=来绑定方法(事件处理 程序)
2.只能在类内部调用(触发)事件。(即定义阶段和执行阶段在同一个类中执行)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class _2EventBasics : MonoBehaviour
{
//二、基础事件 事件是对象(对委托变量的封装)
//1 3阶段在同一类内
//1定义阶段
delegate void eventHandler();
event eventHandler eventMethod;
//2.注册阶段
private void OnEnable()
{
eventMethod += eventLogic;
}
void eventLogic()
{
Debug.Log("02--------eventLogic--------");
}
//3.执行阶段
void Start()
{
eventMethod();
}
private void OnDestroy()
{
eventMethod -= eventLogic;
}
}