RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
UGUI防止穿透和判断点击的是否是UI-创新互联

年后刚上班,就有不幸的事情发生,项目界面要把NGUI推翻,用UGUI来做,真是蛋都碎了,但是没办法,在做的过程中遇UI穿透和点击的是否是UI,查资料,问朋友,弄出来,跟大家分享:

创新互联,专注为中小企业提供官网建设、营销型网站制作、成都响应式网站建设公司、展示型成都网站设计、网站建设、外贸网站建设等服务,帮助中小企业通过网站体现价值、有效益。帮助企业快速建站、解决网站建设与网站营销推广问题。

1.UGUI自带的防穿透代码:

 if (EventSystem.current.IsPointerOverGameObject())                     {                         return;//为真,则点击在UI上                     }    好像漏了点东西,没有写全,上面的只是Unity_Editor 下或者电脑上能运行,移动端是   if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))                     {                         return;//为真,则点击在UI上                     }           总结:    #if IPHONE || ANDROID if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) #else if (EventSystem.current.IsPointerOverGameObject()) #endif Debug.Log("当前触摸在UI上"); else         Debug.Log("当前没有触摸在UI上");

2.查资料,自己写了一个判断

using UnityEngine; using System.Collections; using UnityEngine.EventSystems; using System.Collections.Generic; using UnityEngine.UI; public class CheckUGUI : MonoBehaviour {     public static bool CheckGuiRaycastObjects(EventSystem eventSystem, GraphicRaycaster graphicRayCaster)     {         PointerEventData eventData = new PointerEventData(eventSystem); #if UNITY_EDITOR         eventData.pressPosition = Input.mousePosition;         eventData.position = Input.mousePosition; #endif #if UNITY_ANDROID || UNITY_IPHONE         if (Input.touchCount > 0)         {             eventData.pressPosition = Input.GetTouch(0).position;             eventData.position = Input.GetTouch(0).position;         } #endif         List list = new List();         graphicRayCaster.Raycast(eventData, list);         return list.Count > 0;     } }

 第二种,我把它写成了一个类,用到的调用就行

  public EventSystem eventSystem;   public GraphicRaycaster graphicRaycaster;       if (CheckUGUI.CheckGuiRaycastObjects(eventSystem, graphicRaycaster))     return;//为真,就点击的是UI。

 希望大家能用的上。

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享名称:UGUI防止穿透和判断点击的是否是UI-创新互联
分享路径:http://scyingshan.cn/article/dghpic.html