使用EventSystem判断是否点击ui
#
private PointerEventData _uiPointerEventData; private List<RaycastResult> _uiRaycastResultCache = new List<RaycastResult>(); public bool IsTouchAnyUI(Vector2 screenPos) { if (null == EventSystem.current) return false; if (_uiPointerEventData == null) _uiPointerEventData = new PointerEventData(EventSystem.current); _uiPointerEventData.position = new Vector2(screenPos.x, screenPos.y); EventSystem.current.RaycastAll(_uiPointerEventData, _uiRaycastResultCache); return _uiRaycastResultCache.Count > 0; } public bool IsTouchUI(Vector2 screenPos, GameObject uiGo) { if (null == EventSystem.current) return false; if (_uiPointerEventData == null) _uiPointerEventData = new PointerEventData(EventSystem.current); _uiPointerEventData.position = new Vector2(screenPos.x, screenPos.y); EventSystem.current.RaycastAll(_uiPointerEventData, _uiRaycastResultCache); for (int i = 0; i < _uiRaycastResultCache.Count; i++) { if (_uiRaycastResultCache[i].gameObject == uiGo) return true; } return false; }

浙公网安备 33010602011771号