【UGUI】Slider EventTrigger 拖拽监听

一些button 监听到是很容易,一些OnClick可以满足,但是像Slider 有点繁琐 大体是这样的

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System.Text;
public static class UnityUtility
{
    public static void AddTriggersListener(GameObject obj, EventTriggerType eventID, UnityAction action)
    {
        EventTrigger trigger = obj.GetComponent();
        if (trigger == null)
        {
            trigger = obj.AddComponent();
        }

        if (trigger.triggers.Count == 0)
        {
            trigger.triggers = new List();
        }

        UnityAction callback = new UnityAction(action);
        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = eventID;
        entry.callback.AddListener(callback);
        trigger.triggers.Add(entry);
    }
}

如何使用呢?

 UnityUtility.AddTriggersListener(需要绑定的物体, EventTriggerType 类型, 回调方法);

 UnityUtility.AddTriggersListener(strengthSlider.gameObject, EventTriggerType.Drag, OnStrengSliderSelcet);

【UGUI】Slider EventTrigger 拖拽监听_第1张图片

那么如何删除呢?

  if (strengthSlider.GetComponent())
        {
            EventTrigger trigger = strengthSlider.GetComponent();


            trigger.triggers.RemoveAll(p => true);

        }

这里是全部删除了

你可能感兴趣的:(Unity3D,UGUI)