获取委托中注册的方法

知识点

DelegateGetInvocationList方法返回一个数组,该数组存储添加的方法

实例

using UnityEngine;
using UnityEngine.Events;
public class TestEvent : MonoBehaviour
{
    
    
    UnityAction action;//无参无返回值 委托
    event UnityAction eventAction;//事件
    void Start()
    {
    
    
        action += Print;
        action += Print;
        //打印调用列表
        foreach (var item in action.GetInvocationList())
        {
    
    
            Debug.Log(item.Method);//打印方法名
        }

        eventAction += Print;
        eventAction += Print;
		
		//打印调用列表
        foreach (var item in eventAction.GetInvocationList())
        {
    
    
            item.Method.Invoke(this, null);//执行方法
        }
    }
    void Print()
    {
    
    
        Debug.Log("123");
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43796392/article/details/125984518
今日推荐