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");
    }
}

原文地址:https://www.cnblogs.com/Peng18233754457/p/11813837.html
大佬的博客主页太炫酷了

写法二:

后来的使用过程中我做了一点点改动
像这样:

/// <summary>
///
/// </summary>
/// <param name="eventTrigger">需要添加事件的EventTrigger</param>
/// <param name="eventTriggerType">事件类型</param>
/// <param name="callback">回调函数</param>
void AddPointerEvent(EventTrigger eventTrigger, EventTriggerType eventTriggerType, UnityEngine.Events.UnityAction<BaseEventData> callback)
{
	EventTrigger.Entry entry = new EventTrigger.Entry();
	entry.eventID = eventTriggerType;
	entry.callback = new EventTrigger.TriggerEvent();
	entry.callback.AddListener(callback);
	eventTrigger.triggers.Add(entry);
	Debug.Log("AddPointerEnterEvent");
}


//这样注册
	AddPointerEvent(eventTrigger, EventTriggerType.PointerEnter, PointerEnter);
	AddPointerEvent(eventTrigger, EventTriggerType.PointerExit, PointerExit);
	AddPointerEvent(eventTrigger, EventTriggerType.PointerClick, PointerClick);
posted @ 2022-06-22 18:05  哒哒哒~~~  阅读(317)  评论(0编辑  收藏  举报