添加EventTrigger事件监听方法

  /// 
    /// 添加EventTrigger事件监听方法
    /// 
    /// 
    /// 
    /// 
    public static void AddEventTriggerHandler(this Transform trans, EventTriggerType type,UnityAction action)
    {
        EventTrigger eventTrigger = null;
        if (!trans.TryGetComponent(out eventTrigger))
        {
            eventTrigger = trans.gameObject.AddComponent();
        }
        if (eventTrigger.triggers == null)
        {
            eventTrigger.triggers = new List();
        }
        var entry = eventTrigger.triggers.FirstOrDefault(p => p.eventID == type);
        if (entry == null)
        {
            entry = new EventTrigger.Entry
            {
                eventID = type,
            };
            entry.callback.AddListener(action);
            eventTrigger.triggers.Add(entry);
        }
        else
        {
            entry.callback.RemoveAllListeners();
            entry.callback.AddListener(action);
        }
    }
    /// 
    /// 添加带自定义参数的EventTrigger事件监听方法
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    public static void AddEventTriggerHandler(this Transform trans, EventTriggerType type, UnityAction action, T t)
    {
        trans.AddEventTriggerHandler(type,p =>
        {
            action(p,t);
        });
    }

你可能感兴趣的:(unity)