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上面
 

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

posted @ 2019-12-17 09:20  剑起苍穹  阅读(2893)  评论(0编辑  收藏  举报
/*鼠标点击特效*/