委托是一种数据类型
作用:占位,在不知道将来要执行的方法的具体代码时,可以先用一个委托变量来代替方法调用(委托的返回值,参数列表要确定)。在实际调用之前,需要为委托赋值,否则为null。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class _1DelegateBasics : MonoBehaviour
{
//一、基础委托 是数据类型
//1.定义阶段
delegate void delegateHandler();
delegateHandler method;
//2.注册阶段
void OnEnable()
{
method = delegateLogic;
}
void delegateLogic()
{
Debug.Log("01-----delegateLogic-------");
}
//3.执行阶段 可以在类外执行
private void Start()
{
method();
}
private void OnDestroy()
{
method -= delegateLogic;
}
}