UGUI全局监控鼠标单击状态

4 篇文章 0 订阅

本文地址:https://blog.csdn.net/t163361/article/details/108598588

项目想做一个类似菜单的功能,单击非菜单部分关闭菜单列表。查了一圈没有开放的全局点击接口可用,然后去翻UGUI的源码,在PointerInputModule中发现了事件字典m_PointerData。这个成员保存所有触发的鼠标事件。然后使用反射实现了此功能。
代码如下:
反射代码

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

namespace Utils.UnityEngineUtils.Proxy
{
    public static class ProxyStandaloneInputModule
    {
        private static StandaloneInputModule inputModule;
        private static FieldInfo             m_PointerData;

        public static Dictionary<int, PointerEventData> GetPointerData()
        {
            if(m_PointerData == null)
                m_PointerData = typeof(StandaloneInputModule).GetField("m_PointerData", BindingFlags.Instance | BindingFlags.NonPublic);
            if (inputModule == null)
                inputModule = GameObject.FindObjectOfType<StandaloneInputModule>();
            if (inputModule   != null
             && m_PointerData != null)
            {
                return m_PointerData.GetValue(inputModule) as Dictionary<int, PointerEventData>;
            }

            return null;
        }
        
    }
}

部分实现代码

		private RectTransform _rectTransform;//菜单父节点
        Dictionary<int, bool> clickStates = new Dictionary<int, bool>();
        private void CheckMenuHide()
        {
            var pointers = ProxyStandaloneInputModule.GetPointerData();
            if (pointers != null)
            {
                bool click;
                foreach (var pointerData in pointers)
                {
                    if (clickStates.TryGetValue(pointerData.Key, out click))
                    {
                        if (click != pointerData.Value.eligibleForClick && click)
                        {
                            var go = pointerData.Value.pointerEnter;
                            if (!ClickMenuGameObject(go))
                            {
                                HideMenu();
                            }
                        }
                        clickStates[pointerData.Key] = pointerData.Value.eligibleForClick;
                    }
                    else
                    {
                        clickStates[pointerData.Key] = pointerData.Value.eligibleForClick;
                    }
                }
            }
        }

        private bool ClickMenuGameObject(GameObject go)
        {
            if (!go)
                return false;
            return go.transform.IsChildOf(_rectTransform);
        }
        
        private void HideMenu()
        {
            _guicadViewModel.Unselect();
        }

思路就是鼠标放开后,去遍历pointerEnter所指向的对象是否隶属于菜单父节点,不是的话,就触发隐藏菜单的操作

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

听星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值