您的位置:首页 > 产品设计 > UI/UE

UGUI防止穿透和判断点击的是否是UI

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

1.UGUI自带的防穿透代码:
if (EventSystem.current.IsPointerOverGameObject())
{
return;//为真,则点击在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<RaycastResult> list = new List<RaycastResult>();
graphicRayCaster.Raycast(eventData, list);
return list.Count > 0;
}

}
第二种,我把它写成了一个类,用到的调用就行
public EventSystem eventSystem;
public GraphicRaycaster graphicRaycaster;

if (CheckUGUI.CheckGuiRaycastObjects(eventSystem, graphicRaycaster))
return;//为真,就点击的是UI。
希望大家能用的上。
本文出自 “Unity_3D技术探讨” 博客,请务必保留此出处http://myselfdream.blog.51cto.com/9944020/1743099
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: