unity判断鼠标在不在UI上


关于判断鼠标在不在UI上 

unity有自带的方法,很简单,关于射线的穿透也可以用下面方法

下面是代码:

这里需要引用  using UnityEngine.EventSystems; 命名空间

EventSystem.current.IsPointerOverGameObject()==true 的时候表示鼠标在Ui上面
EventSystem.current.IsPointerOverGameObject()==false 的时候表示鼠标不在Ui上面
 if (!EventSystem.current.IsPointerOverGameObject())
                {               
                    Debug.Log("不在UI上");
                }
                else
                {
                    Debug.Log("在UI上");
                }

关于射线穿透也可以这样用在射线的地方加入这个判断,就可以了

下面贴上简单的代码(一看就懂)

 if (Input.GetMouseButtonUp(0))
        {
            ray = cameraa.ScreenPointToRay(Input.mousePosition);
            LayerMask ss = 1 << 9;//射线在第九层
            RaycastHit raycastHit;
            if (Physics.Raycast(ray, out raycastHit, ss) && !EventSystem.current.IsPointerOverGameObject())
            {
                //判断鼠标不在UI上且射线打到了物体
            }
        }

在手机端的话

EventSystem.current.IsPointerOverGameObject(Input .GetTouch (0).fingerId))=True 表示在UI上面
EventSystem.current.IsPointerOverGameObject(Input .GetTouch (0).fingerId))=false 表示不在UI上面
 

本次就这么多了,喜欢请点个赞吧,  谢谢.


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM