委托和事件
参考
起因:
简单理解一下:
什么是委托?定义方法的类型。
可以将多个方法绑定到委托上 使用+=,同理取消方法的绑定-=
什么是事件?类似属性对字段的封装,是对委托变量的封装
unity中一个小栗子
using UnityEngine;
using UnityEditor;
// ensure class initializer is called whenever scripts recompile
[InitializeOnLoadAttribute]
public static class PlayModeStateChangedExample
{
// register an event handler when the class is initialized
static PlayModeStateChangedExample()
{
EditorApplication.playModeStateChanged -= LogPlayModeState;
EditorApplication.playModeStateChanged += LogPlayModeState;
}
private static void LogPlayModeState(PlayModeStateChange state)
{
Debug.Log(state);
}
}