其他分享
首页 > 其他分享> > Unity 用脚本给EventTrigger添加各种事件

Unity 用脚本给EventTrigger添加各种事件

作者:互联网

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Trigger : MonoBehaviour
{
    EventTrigger eventTrigger;
    // Start is called before the first frame update
    void Start()
    {
        eventTrigger = GetComponent<EventTrigger>();

        AddPointerEnterEvent();
        AddPointerExitEvent();
        AddPointerClickEvent();
    }

    void AddPointerEnterEvent()
    {
        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = EventTriggerType.PointerEnter;
        entry.callback = new EventTrigger.TriggerEvent();
        UnityEngine.Events.UnityAction<BaseEventData> callback = new UnityEngine.Events.UnityAction<BaseEventData>(PointerEnter);
        entry.callback.AddListener(callback);
        eventTrigger.triggers.Add(entry);
        Debug.Log("AddPointerEnterEvent");
    }

    void AddPointerExitEvent()
    {
        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = EventTriggerType.PointerExit;
        entry.callback = new EventTrigger.TriggerEvent();
        UnityEngine.Events.UnityAction<BaseEventData> callback = new UnityEngine.Events.UnityAction<BaseEventData>(PointerExit);
        entry.callback.AddListener(callback);
        eventTrigger.triggers.Add(entry);
        Debug.Log("AddPointerExitEvent");
    }

    void AddPointerClickEvent()
    {
        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = EventTriggerType.PointerClick;
        entry.callback = new EventTrigger.TriggerEvent();
        UnityEngine.Events.UnityAction<BaseEventData> callback = new UnityEngine.Events.UnityAction<BaseEventData>(PointerClick);
        entry.callback.AddListener(callback);
        eventTrigger.triggers.Add(entry);
        Debug.Log("AddPointerClickEvent");
    }

    void PointerEnter(BaseEventData baseEventData)
    {
        Debug.Log("enter");
    }

    void PointerExit(BaseEventData baseEventData)
    {
        Debug.Log("exit");
    }

    void PointerClick(BaseEventData baseEventData)
    {
        Debug.Log("click");
    }
}
View Code

用脚本动态给EventTrigger添加各种事件

标签:EventTrigger,UnityEngine,void,callback,Unity,添加,new,entry
来源: https://www.cnblogs.com/Peng18233754457/p/11813837.html