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