UGUI 点击穿透问题
unity上 用 做游戏欢迎界面上通用的ui,然后导到游戏里面直接用,但发现游戏里面是用ngui的,点击ugui 的ui 会穿透过去 ngui会响应,原本模型的点击处理也会响应
我用的 unity 版本 是 4.6.3的
本来是用 EventSystem.current.IsPointerOverGameObject() 来检测点击穿透的
在pc的unity编辑器上都可以检测到。可是打包到android 上 就无效,网上有的说以前的版本可以,不知道是不是之后的bug,还是有其他的接口
在网上搜到了这里,找到以下解决方法:
1 /// <summary> 2 /// Cast a ray to test if Input.mousePosition is over any UI object in EventSystem.current. This is a replacement 3 /// for IsPointerOverGameObject() which does not work on Android in 4.6.0f3 4 /// </summary> 5 private static bool IsPointerOverUIObject() 6 { 7 if (EventSystem.current == null) 8 return false; 9 10 // Referencing this code for GraphicRaycaster https://gist.github.com/stramit/ead7ca1f432f3c0f181f 11 // the ray cast appears to require only eventData.position. 12 PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current); 13 eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y); 14 15 List<RaycastResult> results = new List<RaycastResult>(); 16 EventSystem.current.RaycastAll(eventDataCurrentPosition, results); 17 18 return results.Count > 0; 19 } 20 21 /// <summary> 22 /// Cast a ray to test if screenPosition is over any UI object in canvas. This is a replacement 23 /// for IsPointerOverGameObject() which does not work on Android in 4.6.0f3 24 /// </summary> 25 private bool IsPointerOverUIObject(Canvas canvas, Vector2 screenPosition) 26 { 27 if (EventSystem.current == null) 28 return false; 29 30 // Referencing this code for GraphicRaycaster https://gist.github.com/stramit/ead7ca1f432f3c0f181f 31 // the ray cast appears to require only eventData.position. 32 PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current); 33 eventDataCurrentPosition.position = screenPosition; 34 35 GraphicRaycaster uiRaycaster = canvas.gameObject.GetComponent<GraphicRaycaster>(); 36 List<RaycastResult> results = new List<RaycastResult>(); 37 uiRaycaster.Raycast(eventDataCurrentPosition, results); 38 return results.Count > 0; 39 }