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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值