UnityEngine.EventSystems

2020-09-05  本文已影响0人  知交

1.使用代码添加 EventTrigger.Entry:

代码:

using UnityEngine;
using UnityEngine.EventSystems;

public class SkillCell
{
    public Transform transform = null;
    private EventTrigger eventTrigger;
    protected SkillCell (Transform transform)
    {
        this.transform = transform;
        InitView();
    }
    
    private void InitView() {
        //给物体添加EventTrigger组件
         eventTrigger = transform.gameObject.AddComponent<EventTrigger>();
        //按下
        EventTrigger.Entry downEntry = new EventTrigger.Entry();
        downEntry.eventID = EventTriggerType.PointerDown;
       //添加TriggerEvent
        downEntry.callback = new EventTrigger.TriggerEvent();
        downEntry.callback.AddListener(OnPointerDown);
        eventTrigger.triggers.Add(downEntry);
        //抬起
        EventTrigger.Entry upEntry = new EventTrigger.Entry();
        upEntry.eventID = EventTriggerType.PointerUp;
        upEntry.callback = new EventTrigger.TriggerEvent();
        upEntry.callback.AddListener(OnPointerUp);
        eventTrigger.triggers.Add(upEntry);
    }
    public void OnPointerDown(BaseEventData eventData)
    {   
    }

    public void OnPointerUp(BaseEventData eventData)
    {
    }
    //封装方法
    public static void RegisterEventClick(EventTrigger eventTrigger, EventTriggerType eventTriggerType, UnityAction<BaseEventData> callback)
    {
        EventTrigger.Entry entry = new EventTrigger.Entry {
            eventID = eventTriggerType
        };
        entry.callback.AddListener(callback);
        eventTrigger.triggers.Add(entry);
    }
}

上一篇下一篇

猜你喜欢

热点阅读