Unity UI添加监听三种方式(IpointxxxHandler实现接口,添加组件,EventTrigger脚本动态添加)

我们以点击事件为例,就是做出最常用也是通用的类似按钮的点击事件(因为基本差不多)
首先我们看实现接口:(最常用)

先看代码

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

public class S2Test : MonoBehaviour,IPointerClickHandler
//添加接口。然后他会要你去实现响应接口,我们直接实现就好了
{
    private int index;
    public void OnPointerClick(PointerEventData eventData)   // 对应接口
    {
    ChangeColor();
    }
    private void ChangeColor()   // 目标方法
    {
        if(index == 0)
        {
            GetComponent<Image>().color = Color.blue;
        }
        else
        {
            GetComponent<Image>().color = Color.white;
        }
        index = index == 0 ? 1 : 0;
        //throw new NotImplementedException();
    }
}

实现接口的方式特别简单,这么简单的还有类似这样的
比如我已经写好了一个button,然后我要为他的点击添加事件,一般是写一个函数,从脚本里面添加监听,已经在unity界面的组件哪里有个onClick添加一下,就行,后面的方法我就不演示了,太简单了(注意要设置成public,不然unity界面访问不到)
首先添加组件(也可以动态添加)
在这里插入图片描述
看代码:

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

public class S2Test : MonoBehaviour,IPointerClickHandler
{
    void Start()
    {
        Button button = gameObject.GetComponent<Button>();//获得组件
        button.onClick.AddListener(() =>
        {
            ChangeColor();
        });//添加监听
    }

    private void ChangeColor()
    {
        if(index == 0)
        {
            GetComponent<Image>().color = Color.blue;
        }
        else
        {
            GetComponent<Image>().color = Color.white;
        }
        index = index == 0 ? 1 : 0;
        //throw new NotImplementedException();
    }
}

其实你完全可以通过AddCompoment();添加button,然后再进行监听。

下面这一种算是比较麻烦的了通过eventTrigger.组件来进行一系列操作
。看代码以及注释

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

public class S2Test : MonoBehaviour,IPointerClickHandler
{
    private int index;
    // Start is called before the first frame update
    void Start()
    {
       //添加组件
        EventTrigger trigger = gameObject.AddComponent<EventTrigger>();
        //初始化变量
        trigger.triggers = new List<EventTrigger.Entry>();
        //创建一个Trigger
        EventTrigger.Entry entry = new EventTrigger.Entry();
        //设置Id(你要的效果)
        entry.eventID = EventTriggerType.PointerClick;
        //绑定回调函数
        entry.callback = new EventTrigger.TriggerEvent();
        //设置回调函数
        entry.callback.AddListener((data) => ChangeColor());
        将trigger添加的组件的list列表里面
        trigger.triggers.Add(entry);
    }

    private void ChangeColor()
    {
        if(index == 0)
        {
            GetComponent<Image>().color = Color.blue;
        }
        else
        {
            GetComponent<Image>().color = Color.white;
        }
        index = index == 0 ? 1 : 0;
        //throw new NotImplementedException();
    }
}

猜你喜欢

转载自blog.csdn.net/fairen/article/details/110916397