我希望在touch屏幕时player起跳,于是在playerControl.cs的Update函数中添加如下touch代码:

  if (Input.GetMouseButtonDown (0)) {//left button down
        jump ();
   }

同时我在屏幕左上角加了一个实现暂停的pause按钮,用的是Unity的UI Button。

于是问题来了,当我点pause按钮想暂停时,人物同时也会起跳!

即Button响应touch消息后并没能把它拦截下来,touch消息刺穿Button到达了屏幕。

解决UGUI刺穿问题目前在所有平台都有效的办法是在屏幕响应touch前先用graphicRaycaster当前touch是否落在ui上。

举例说明如下:

假设我的项目UI部分Hierarchy如下:

在UI节点上的脚本UIcontrol.cs中实现如下函数:

using UnityEngine.EventSystems;
using System.Collections.Generic;

using UnityEngine.UI;

   public bool isTouchOnUI()
    {
        EventSystem eventSystem = gameObject.transform.FindChild ("EventSystem").GetComponent<EventSystem> ();
        Transform[] childrenTransformList = GetComponentsInChildren<Transform>();
        foreach (Transform transform in childrenTransformList) {
            GraphicRaycaster graphicRaycaster = transform.GetComponent<GraphicRaycaster> ();
            if (graphicRaycaster&&graphicRaycaster.enabled==true) {
                PointerEventData eventData = new PointerEventData (eventSystem);
                eventData.pressPosition = Input.mousePosition;
                eventData.position = Input.mousePosition;
                List<RaycastResult> list = new List<RaycastResult> ();
                graphicRaycaster.Raycast (eventData, list);

          ////Debug.Log("list.Count:"+list.Count);
                if (list.Count > 0)
                    return true;
            }
        }
        return false;
    }

那么前面的touch代码改为:

        bool isTouchOnUI=m_gameRef.transform.Find("UI").GetComponent<UIcontrol>().isTouchOnUI();
            if (isTouchOnUI==false&&Input.GetMouseButtonDown (0)) {//left button down
                jump ();
            }

即可解决touch穿透问题。

补充:

一,一定要加if(graphicRaycaster)的判断,因为注意GameObject.GetComponentsInChildren的文档中写道:

Returns all components of Type type in the GameObject or any of its children.

即,除了UI节点的孩子会被搜索以外,UI节点本身也会被搜索,而UI节点上不含有graphicRaycaster,所以必须先判断graphicRaycaster是否获得成功,再进行后续处理。

另外文档中还写道:

The search for components is carried out recursively on child objects, so it includes children of children, and so on.

即GameObject.GetComponentsInChildren不只是会搜索子节点,也会搜索孙节点。

二,加上graphicRaycaster.enabled==true的判断,好处是,如果我们希望某个Canvas能被刺穿,则可以将其Graphic Raycaster去掉勾选即可。

参考:http://www.cnblogs.com/fly-100/p/4570366.html

unity5, UGUI刺穿问题解法的更多相关文章

  1. 解决IE6下浮层遮盖select刺穿的问题

    图一未解决刺穿问题: 图二已解决                       解决方法使用iframe间接挡住层,具体方法见源码 源码一(未解决刺穿):   <html xmlns=" ...

  2. leetcode 学习心得 (2) (301~516)

    源代码地址:https://github.com/hopebo/hopelee 语言:C++ 301. Remove Invalid Parentheses Remove the minimum nu ...

  3. 2016.05.03,英语,《Vocabulary Builder》Unit 21

    sub, means 'under', as in subway, submarine, substandard. A subject is a person who is under the aut ...

  4. 极客DIY:打造属于自己的无线移动渗透测试箱

    本文中介绍的工具.技术带有一定的攻击性,请合理合法使用. 你想不想拥有一款属于自己的移动无线渗透测试箱,如果你感兴趣,下面介绍的设备将会对你很有帮助.这个箱子被称为“MiTM(中间人攻击)WiFi箱” ...

  5. [学习笔记]设计模式之Bridge

    写在前面 为方便读者,本文已添加至索引: 设计模式 学习笔记索引 “魔镜啊魔镜,谁是这个世界上最美丽的人?”月光中,一个低沉的声音回荡在女王的卧室.“是美丽的白雪公主,她正和小霍比特人们幸福快乐地生活 ...

  6. Unity 白猫操作小实例

    最近师兄找我说白猫的操作如何做,  0.0 结果白猫没有android的客户端玩不了,看了下视频介绍就简单做了下 效果图:   核心代码: using UnityEngine; using Syste ...

  7. 【转】傅盛:一家公司最大瓶颈就是CEO

    创业之路,一经踏上,即永无止境.当然,对于初创期的CEO而言,还是有一些方法论可循. 来源 | 盛盛GO(ID:fstalk) 文  | 傅盛 经常有创业者问我,如何当好创业公司CEO,或如何创业,我 ...

  8. 用户手册是Yasm汇编

    本文档的用户手册是Yasm汇编. 它是介绍和通用所有Yasm用户参考. 英文的参考:http://www.cnblogs.com/coryxie/p/3959888.html 1 .介绍 Yasm b ...

  9. 游戏引擎架构Note2

    [游戏引擎架构Note2] 1.视觉属性(visual property)决定光线如何与物体表面产生交互作用. 2.一个Mesh所使用三角形的多少可以用细致程度(level-of-detail,LOD ...

随机推荐

  1. WEB.NET error:请添加一个名为 jquery (区分大小写)的 ScriptResourceMapping 解决方案

    参考 http://blog.csdn.net/kisscatforever/article/details/50579935 今天用了一个组件 一个验证型的组件. 然后出现了这个问题. 我看了网上一 ...

  2. iOS UIPrintInteractionController打印

    - (void)printData{ //为打印做准备,创建一个指向sharedPrintController的引用 UIPrintInteractionController *printer = [ ...

  3. NTP安全漏洞公告

        NTP服务今天公告了几个高危漏洞,大概信息如下: 描述:包含缓冲区溢出等多个高危或低危漏洞. 危害:可以利用获取服务器权限完全控制服务器,至少可以造成服务器崩溃. 影响范围:只有升级到4.2. ...

  4. error C2248: 'MyString::pCharArray' : cannot access private member declared in class 'MyString'

    std::ostream & operator<<(std::ostream os,const MyString & mystr){os<<mystr.pCha ...

  5. svn: None of the environment variables SVN_EDITOR...问题解决

    转:http://blog.163.com/lgh_2002/blog/static/44017526201046111856208/ 问题1: svn: Could not use external ...

  6. java多线程加锁是对谁加锁?

    1.java多线程加锁是对谁加锁? 答:当然是对共享资源加锁啊,对谁进行访问修改,就对象进行加锁.以便使多线程按序访问此共享对象 比如: 在具体的Java代码中需要完成一下两个操作:把竞争访问的资源类 ...

  7. express结合jade模板渲染HTML

    在线html转jade工具>> 注意:以下是在Windwo环境下 运行: npm install jade 然后你的目录node_modules下将增加jade文件夹 app.js var ...

  8. Openshift初步学习问题集

    1.设置资源限额 详细参考 https://docs.openshift.com/enterprise/3.2/admin_guide/quota.html#sample-resource-quota ...

  9. JS函数节流和函数防抖问题分析

    问题1:如果实现了dom拖拽功能,但是在绑定拖拽事件的时候发现每当元素稍微移动一点便触发了大量的回调函数,导致浏览器直接卡死,这个时候怎么办? 问题2:如果给一个按钮绑定了表单提交的post事件,但是 ...

  10. 关于Java的File类、字节流和字符流

    一.File类: 在Windows下的路径分隔符(\)和在Linux下的路径分隔符(/)是不一样的,当直接使用绝对路径时,跨平台会报No Such file or diretory异常. File中还 ...