zoukankan      html  css  js  c++  java
  • 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     }
  • 相关阅读:
    八枚硬币问题
    找出诡异的Bug:数据怎么存不进去
    IKAnalyzer使用停用词词典进行分词
    【Quick-COCOS2D-X 3.3 怎样绑定自己定义类至Lua之四】使用绑定C++至Lua的自己定义类
    iOS 自我检測
    蓝桥杯 BASIC 29 高精度加法(大数)
    二叉树的非递归遍历
    [算法]有趣算法合辑[11-20]
    习惯的力量之四理直气壮的借口?
    《github一天一道算法题》:分治法求数组最大连续子序列和
  • 原文地址:https://www.cnblogs.com/gabo/p/4581788.html
Copyright © 2011-2022 走看看