简单的UI事件系统工具类

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using Random = UnityEngine.Random;

namespace Test.UI事件系统工具类
{
    public class TestUIEventTool : MonoBehaviour
    {
        public GameObject _img;
        public GameObject _cube;//为3D对象添加UI事件,记得为相机添加【物理射线检测】,以及场景中要有【EventSystem】。

        private void Start()
        {
            UIEventTool.AddUIEventForGameObject(_cube)
                .AddEventByEventTriggerType(EventTriggerType.PointerClick, OnCubeClick);
            /*
            UIEventSystemTool.AddUIEvent(_cube).AddEventDic(EventTriggerType.PointerEnter,OnCubePointerEnter);
            UIEventSystemTool.AddUIEvent(_cube).AddEventDic(EventTriggerType.PointerExit,OnCubePointerExit);
            */
            UIEventTool.AddUIEventForGameObject(_img)
                .AddEventByEventTriggerType(EventTriggerType.PointerClick, OnImageClick);
        }

        private void OnCubeClick()
        {
            Debug.Log("Click Cube...");
            _cube.GetComponent<MeshRenderer>().material.color = Random.ColorHSV();
        }

        private void OnImageClick()
        {
            Debug.Log("...Click Image");
            _img.GetComponent<Image>().color = Random.ColorHSV();
        }


        //下面是测试移入、移出UI事件
        /*
        private int isEnter = 0;
        private void Update()
        {
            switch (isEnter)
            {
                case 1: Debug.Log("鼠标进入");
                    isEnter = 0;
                    break;
                case 2: Debug.Log("鼠标移出");
                    isEnter = 0;
                    break;
            }
        }

        private void OnCubePointerExit()
        {
            isEnter = 2;
        }

        private void OnCubePointerEnter()
        {
            isEnter = 1;
        }
        */
    }
}
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

namespace Test.UI事件系统工具类
{
    public class UIEventTool : EventTrigger
    {
        private readonly Dictionary<EventTriggerType, Action> _eventDic = new Dictionary<EventTriggerType, Action>();

        public static UIEventTool AddUIEventForGameObject(GameObject go)
        {
            if (!go.TryGetComponent(out UIEventTool tool))
            {
                tool = go.AddComponent<UIEventTool>();
            }

            return tool;
        }

        public void AddEventByEventTriggerType(EventTriggerType ett, Action action)
        {
            _eventDic.TryAdd(ett, action);
        }

        //后续需要新的UI处理函数,就重写新的UI处理函数,自行添加

        public override void OnPointerClick(PointerEventData eventData)
        {
            if (_eventDic.TryGetValue(EventTriggerType.PointerClick, out Action action))
            {
                action?.Invoke();
            }
        }

        public override void OnPointerEnter(PointerEventData eventData)
        {
            if (_eventDic.TryGetValue(EventTriggerType.PointerEnter, out Action action))
            {
                action?.Invoke();
            }
        }

        public override void OnPointerExit(PointerEventData eventData)
        {
            if (_eventDic.TryGetValue(EventTriggerType.PointerExit, out Action action))
            {
                action?.Invoke();
            }
        }
    }
}

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值