ZERO_BEYOND

博客园 首页 联系 订阅 管理
 1     public static bool IsPointerOverUI()
 2     {
 3         // 优先检测触摸
 4         for (int i = 0; i < Input.touchCount; i++)
 5         {
 6             Touch touch = Input.touches[i];
 7             if (touch.phase == TouchPhase.Began)
 8             {
 9                 if (EventSystem.current.IsPointerOverGameObject(touch.fingerId))
10                     return true;
11             }
12         }
13 
14         // 检测鼠标
15         if (EventSystem.current.IsPointerOverGameObject())
16             return true;
17 
18         // 备用方案:手动射线检测
19         return RaycastUI();
20     }
21 
22     private static bool RaycastUI()
23     {
24         PointerEventData eventData = new PointerEventData(EventSystem.current);
25         eventData.position = Input.mousePosition;
26 
27         List<RaycastResult> results = new List<RaycastResult>();
28         EventSystem.current.RaycastAll(eventData, results);
29 
30         // 过滤掉透明部分(可选)
31         foreach (RaycastResult result in results)
32         {
33             if (result.gameObject.GetComponent<UnityEngine.UI.Image>()?.raycastTarget ?? false)
34                 return true;
35         }
36         return false;
37     }

 

posted on 2023-07-15 02:43  ZERO_BEYOND  阅读(56)  评论(0)    收藏  举报