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 }
浙公网安备 33010602011771号