UNITY UGUI Event Trigger

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UNITY UGUI Event Trigger相关的知识,希望对你有一定的参考价值。

参考技术A 今天永强接着昨天的课程 再接着给玉田讲一个新东西,名字非常酷炫,叫《EventTrigger》他的名字啥意思玉田并不需要知道,可以粗暴的这样理解哈。给你举个简单的例子:就好比玉田干坏错事了,玉田的爹地赵四抬手就给玉田一个大嘴巴子,玉田被抽一巴掌后,就捂住脸。

这里的EventTrigger就是:赵四抬手抽玉田一大嘴巴子。

执行的Function就是:玉田捂脸。。。

好嘞!下面我们看看详细操作:

这是我们上一节课的栗子。现在选中Hierarchy中的Image对象
然后在Inspector中选择最下端的AddComponent按钮!按他就对啦!

然后会蹦出来一个小框框,上面有个搜索栏。像你平时搜索“苍老师”一样,在搜索栏中输入“EventTrigger”,然后就显示EventTrigger组件

选中我用红框框起来的这个吊东西。

这时你的Inspector中会多了一个东西,如下图

没错,揍四他!

摁“Add New Event Type”按钮。

选择PointerEnter

点右下角的➕号,出现下图的样子

把Hierarchy中的Image拖到对相框里

然后按我下图的设置做。玉田你蹩BB,就按我说的做就对了!

这个意思是当画布渲染时,如果有PointerEnter事件时,Image的Alpha设置为0.5
下面我们再添加一个事件。当PointerExit事件发生时。Image的Alpha设置为1

好滴。玉田真棒!现在运行一下看看效果吧。嘤嘤嘤。。。

当鼠标悬停在图片上时,Image透明度变成0.5

当鼠标离开图片时,Image的透明度变成了1
好啦玉田,哥累了。你该回家吃饭了。

unity中Event Trigger组件应用代码

using UnityEngine;
using System.Collections;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using UnityEngine .UI ;
public class DistrictBgEvent {



    //门户小界面按钮功能
    private EventTrigger m_eventTrigger;
    public  string pathDistrictBg;
    public GameObject PanelName;
    private  bool m_state = true   ;
    private bool m_state_2 = true;
    private bool m_toggleState = false ;
    // Use this for initialization

    //初始化
    public void StartInit(GameObject Obj,string path)
    {
        if (PanelName.name == "mainPanel(Clone)" || PanelName.name == "mainControllerPanel(Clone)")
        {
            Enter(Obj, path, "Gwh", "管委会");        
        }
        else
        {
            if (pathDistrictBg == "LeftBar/districtBg/mainconbg/")
            {

                Enter(Obj, path, "Gwh", "管委会");
            }
            else
            {

                Exit(Obj, path, "Gwh", "管委会");

            }
           
        }
        Exit(Obj, path, "Gjq", "高教区");
        Exit(Obj, path, "Jmq", "居民区");
        Exit(Obj, path, "Gyq", "工业区");
        Exit(Obj, path, "Yfq", "研发区");
        Exit(Obj, path, "Syq", "商业区");

        m_state = true;
        m_state_2 = true;
        m_toggleState = false ;

}

    //鼠标进入事件
    public void MouseInit(GameObject Obj,string path) {
       
        AddTriggersListener(Obj.transform.Find(path + "LeftBtn/Gwh").gameObject, EventTriggerType.PointerEnter, GwhEnter);
        AddTriggersListener(Obj.transform.Find(path + "LeftBtn/Gjq").gameObject, EventTriggerType.PointerEnter, GjqEnter);
        AddTriggersListener(Obj.transform.Find(path + "LeftBtn/Jmq").gameObject, EventTriggerType.PointerEnter, JmqEnter);
        AddTriggersListener(Obj.transform.Find(path + "LeftBtn/Gyq").gameObject, EventTriggerType.PointerEnter, GyqEnter);
        AddTriggersListener(Obj.transform.Find(path + "LeftBtn/Yfq").gameObject, EventTriggerType.PointerEnter, YfqEnter);
        AddTriggersListener(Obj.transform.Find(path + "LeftBtn/Syq").gameObject, EventTriggerType.PointerEnter, SyqEnter);

        if (PanelName.name == "mainPanel(Clone)" || PanelName.name == "mainControllerPanel(Clone)")
        {

        }
        else
        {
            AddTriggersListener(Obj.transform.FindChild("TopBarBg/districtBg").gameObject, EventTriggerType.PointerEnter, OnPointerEnter_2);
            AddTriggersListener(Obj.transform.FindChild("TopBarBg/districtBg").gameObject, EventTriggerType.PointerExit, OnPointerExit_2);

            AddTriggersListener(Obj.transform.FindChild(path + "RightBtn/Gwh").gameObject, EventTriggerType.PointerEnter, OnPointerEnter);
            AddTriggersListener(Obj.transform.FindChild(path + "RightBtn/Gwh").gameObject, EventTriggerType.PointerExit, OnPointerExit);

            AddTriggersListener(Obj.transform.FindChild(path + "RightBtn/Gjq").gameObject, EventTriggerType.PointerEnter, OnPointerEnter);
            AddTriggersListener(Obj.transform.FindChild(path + "RightBtn/Gjq").gameObject, EventTriggerType.PointerExit, OnPointerExit);

            AddTriggersListener(Obj.transform.FindChild(path + "RightBtn/Jmq").gameObject, EventTriggerType.PointerEnter, OnPointerEnter);
            AddTriggersListener(Obj.transform.FindChild(path + "RightBtn/Jmq").gameObject, EventTriggerType.PointerExit, OnPointerExit);

            AddTriggersListener(Obj.transform.FindChild(path + "RightBtn/Gyq").gameObject, EventTriggerType.PointerEnter, OnPointerEnter);
            AddTriggersListener(Obj.transform.FindChild(path + "RightBtn/Gyq").gameObject, EventTriggerType.PointerExit, OnPointerExit);

            AddTriggersListener(Obj.transform.FindChild(path + "RightBtn/Yfq").gameObject, EventTriggerType.PointerEnter, OnPointerEnter);
            AddTriggersListener(Obj.transform.FindChild(path + "RightBtn/Yfq").gameObject, EventTriggerType.PointerExit, OnPointerExit);

            AddTriggersListener(Obj.transform.FindChild(path + "RightBtn/Syq").gameObject, EventTriggerType.PointerEnter, OnPointerEnter);
            AddTriggersListener(Obj.transform.FindChild(path + "RightBtn/Syq").gameObject, EventTriggerType.PointerExit, OnPointerExit);

            AddTriggersListener(Obj.transform.FindChild("TopBarBg/LeftPanel/LeftButtons/mainConBtn").gameObject, EventTriggerType.PointerEnter, OnPointerEnter_3);
            AddTriggersListener(Obj.transform.FindChild("TopBarBg/LeftPanel/LeftButtons/mainConBtn").gameObject, EventTriggerType.PointerExit, OnPointerExit_3);

        }


    }

    //监控系统下拉菜单点击关闭
    public void MouseClickUpdate(GameObject Obj,GameObject Obj_2)
    {
       
            if (m_toggleState && Input.GetMouseButton(0) && Obj.transform.GetComponent<Toggle>().isOn && m_state  && m_state_2 )
            {
                Obj.transform.FindChild("Text")
                    .GetComponent<Text>()
                    .color = new Color(50 / 255f, 50 / 255f, 50 / 255f, 1);
                Obj_2.SetActive(false);
                Obj.transform.GetComponent<Toggle>().isOn = false;
            }
      
       

    }
    

    //门户小界面按钮功能

    /// <summary>
    /// 为obj添加Eventrigger监听事件  
    /// </summary>
    /// <param name="obj">添加监听的对象</param>
    /// <param name="eventType">添加的监听类型</param>
    /// <param name="action">触发的函数</param>
    public  void AddTriggersListener(GameObject obj, EventTriggerType eventType, UnityAction<BaseEventData> action)
    {
        m_eventTrigger = obj.GetComponent<EventTrigger>();
        if (m_eventTrigger == null)
        {
            m_eventTrigger = obj.AddComponent<EventTrigger>();
        }
        if (m_eventTrigger.triggers.Count == 0)
        {
            m_eventTrigger.triggers = new List<EventTrigger.Entry>();
        }
        //定义所要绑定的事件类型   
        EventTrigger.Entry entry = new EventTrigger.Entry();
        //设置事件类型    
        entry.eventID = eventType;
        //定义回调函数    
        UnityAction<BaseEventData> callback = new UnityAction<BaseEventData>(action);
        //设置回调函数    
        entry.callback.AddListener(callback);
        //添加事件触发记录到GameObject的事件触发组件    
        m_eventTrigger.triggers.Add(entry);

    }
    private void Enter(GameObject obj,string path, string name_1, string name_2)
    {
      
        obj.transform.FindChild(path + "LeftBtn/" + name_1 + "/Image").gameObject.SetActive(true);
        obj.transform.FindChild(path + "RightBtn/" + name_1).gameObject.SetActive(true);
        if (obj.name == "mainPanel(Clone)" || obj.name == "mainControllerPanel(Clone)")
        {
            obj.transform.FindChild(path + "LeftBtn/" + name_1 + "/Text").gameObject.GetComponent<Text>().color =
                new Color(0, 209/225f, 229/225f, 1);
            obj.transform.FindChild(path + "LeftBtn/" + name_1 + "/Text").gameObject.GetComponent<Text>().text = "   " +name_2 +"    >".ToString();
        }
        else
        {
            if (pathDistrictBg == "LeftBar/districtBg/mainconbg/")
            {

                obj.transform.FindChild(pathDistrictBg + "LeftBtn/" + name_1 + "/Text").gameObject.GetComponent<Text>().color =
                    new Color(0, 209/225f, 229/225f, 1);
                obj.transform.FindChild(pathDistrictBg + "LeftBtn/" + name_1 + "/Text").gameObject.GetComponent<Text>().text =
                    "   " + name_2 + "    >".ToString();

            }
            else
            {

            }
        }

    }

    private void Exit(GameObject obj,string path, string name_1, string name_2)
    {
        obj.transform.FindChild(path + "RightBtn/" + name_1).gameObject.SetActive(false);
        obj.transform.FindChild(path + "LeftBtn/" + name_1 + "/Image").gameObject.SetActive(false);
        if (obj.name == "mainPanel(Clone)" || obj.name == "mainControllerPanel(Clone)")
        {
            obj.transform.FindChild(path + "LeftBtn/" + name_1 + "/Text").gameObject.GetComponent<Text>().text =
                name_2.ToString();
            obj.transform.FindChild(path + "LeftBtn/" + name_1 + "/Text").gameObject.GetComponent<Text>().color =
                Color.white;
        }
        else
        {

            if (pathDistrictBg == "LeftBar/districtBg/mainconbg/")
            {


                obj.transform.FindChild(pathDistrictBg + "LeftBtn/" + name_1 + "/Text").gameObject.GetComponent<Text>().text =
                    name_2.ToString();
                obj.transform.FindChild(pathDistrictBg + "LeftBtn/" + name_1 + "/Text").gameObject.GetComponent<Text>().color =
                    Color.white;

            }
            else
            {


            }
        }
    }

    private void GwhEnter(BaseEventData arg0)
    {
       // Debug.Log("GwhEnter");
        Enter(PanelName,pathDistrictBg, "Gwh", "管委会");
        Exit(PanelName,pathDistrictBg, "Gjq", "高教区");
        Exit(PanelName,pathDistrictBg, "Jmq", "居民区");
        Exit(PanelName,pathDistrictBg, "Gyq", "工业区");
        Exit(PanelName,pathDistrictBg, "Yfq", "研发区");
        Exit(PanelName,pathDistrictBg, "Syq", "商业区");

    }

    private void GwhExit(BaseEventData arg0)
    {
       // Debug.Log("GwhExit");

    }

    private void GjqEnter(BaseEventData arg0)
    {
        // Debug.Log("GjqEnter");
        Enter(PanelName,pathDistrictBg, "Gjq", "高教区");
        Exit(PanelName,pathDistrictBg, "Gwh", "管委会");
        Exit(PanelName,pathDistrictBg, "Jmq", "居民区");
        Exit(PanelName,pathDistrictBg, "Gyq", "工业区");
        Exit(PanelName,pathDistrictBg, "Yfq", "研发区");
        Exit(PanelName,pathDistrictBg, "Syq", "商业区");

    }
    private void JmqEnter(BaseEventData arg0)
    {
        // Debug.Log("JmqEnter");
        Enter(PanelName,pathDistrictBg, "Jmq", "居民区");
        Exit(PanelName,pathDistrictBg, "Gwh", "管委会");
        Exit(PanelName,pathDistrictBg, "Gjq", "高教区");
        Exit(PanelName,pathDistrictBg, "Gyq", "工业区");
        Exit(PanelName,pathDistrictBg, "Yfq", "研发区");
        Exit(PanelName,pathDistrictBg, "Syq", "商业区");

    }
    private void GyqEnter(BaseEventData arg0)
    {
        // Debug.Log("GyqEnter");
        Enter(PanelName,pathDistrictBg, "Gyq", "工业区");
        Exit(PanelName,pathDistrictBg, "Gwh", "管委会");
        Exit(PanelName,pathDistrictBg, "Jmq", "居民区");
        Exit(PanelName,pathDistrictBg, "Gjq", "高教区");
        Exit(PanelName,pathDistrictBg, "Yfq", "研发区");
        Exit(PanelName,pathDistrictBg, "Syq", "商业区");

    }
    private void YfqEnter(BaseEventData arg0)
    {
        // Debug.Log("YfqEnter");
        Enter(PanelName,pathDistrictBg, "Yfq", "研发区");
        Exit(PanelName,pathDistrictBg, "Gwh", "管委会");
        Exit(PanelName,pathDistrictBg, "Jmq", "居民区");
        Exit(PanelName,pathDistrictBg, "Gyq", "工业区");
        Exit(PanelName,pathDistrictBg, "Gjq", "高教区");
        Exit(PanelName,pathDistrictBg, "Syq", "商业区");

    }

    private  void SyqEnter(BaseEventData arg0)
    {
        // Debug.Log("SyqEnter"); 
        Enter(PanelName,pathDistrictBg, "Syq", "商业区");
        Exit(PanelName,pathDistrictBg, "Gwh", "管委会");
        Exit(PanelName,pathDistrictBg, "Jmq", "居民区");
        Exit(PanelName,pathDistrictBg, "Gyq", "工业区");
        Exit(PanelName,pathDistrictBg, "Yfq", "研发区");
        Exit(PanelName,pathDistrictBg, "Gjq", "高教区");

    }
    //监控系统下拉菜单
    private  void OnPointerEnter(BaseEventData arg0)
    {
        m_state = false  ;
        Debug.Log("m_state" + m_state);
    }

    private  void OnPointerExit(BaseEventData arg0)
    {
        m_state = true  ;
        Debug.Log("m_state" + m_state);
    }
    private void OnPointerEnter_2(BaseEventData arg0)
    {
        m_state_2 = false  ;
        Debug.Log("m_state_2" + m_state_2);
    }

    private void OnPointerExit_2(BaseEventData arg0)
    {
        m_state_2 = true  ;
        Debug.Log("m_state_2" + m_state_2);
    }
    private void OnPointerEnter_3(BaseEventData arg0)
    {
        m_toggleState  = false  ;
        Debug.Log("m_toggleState" + m_toggleState);
    }

    private void OnPointerExit_3(BaseEventData arg0)
    {
        m_toggleState = true  ;
        Debug.Log("m_toggleState" + m_toggleState);
    }
}

 

以上是关于UNITY UGUI Event Trigger的主要内容,如果未能解决你的问题,请参考以下文章

Unity3d中UGUI组件精简复盘(二十)EventSystem组件与Standalone Input Moudle组件

unity ugui可以忽略unity timescale吗

Unity_UGUI_动画系统

关于Unity中的NGUI和UGUI

unity 原生UGUI代码扩展

unity 原生UGUI代码扩展