无需知道注册的事件有哪些,一次性全部删除注册的事件。
using System;
using UnityEngine;
public class EventTest : MonoBehaviour
{
//定义事件
public Action OnStepChangeAction;
//删除事件
void DestoryEvent()
{
if (OnStepChangeAction != null)
{
//删除注册的事件
Delegate[] dels = OnStepChangeAction.GetInvocationList();
foreach (Delegate d in dels)
{
//得到方法名
object delObj = d.GetType().GetProperty("Method").GetValue(d, null);
string funcName = (string)delObj.GetType().GetProperty("Name").GetValue(delObj, null);
OnStepChangeAction -= d as Action;
}
}
}
}