您的位置:首页 > 移动开发 > Unity3D

unity射线穿透UI的问题解决方案

2017-07-21 10:06 681 查看
首先参考原文章:http://www.cnblogs.com/fly-100/p/4570366.html

致敬原作者,代码稍作修改才可使用。

首先先引入头文件

using UnityEngine.EventSystems;

using UnityEngine.UI;

然后创建变量

public GameObject changePanel;(这是要阻挡射线的UI)

public EventSystem es;

Graphics a;

主要代码段

bool CheckGuiRaycastObjects()
{
PointerEventData eventData = new PointerEventData(es);
eventData.pressPosition = Input.mousePosition;
eventData.position = Input.mousePosition;

List<RaycastResult> list = new List<RaycastResult>();
changeMatCanvas.GetComponent<GraphicRaycaster> ().Raycast (eventData, list);
return list.Count > 0;
}

在Update中加入

if (CheckGuiRaycastObjects()) return;

这样UI就可以挡住射线,不会点击到后面的碰撞体了。

有个新的解决方法

if
(Input.GetMouseButtonDown(0)||(Input.touchCount
>0
&&
Input.GetTouch(0).phase
==
TouchPhase.Began))

{

#if

UNITY_ANDROID || UNITY_IPHONE

if
(EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))

#else

if
(EventSystem.current.IsPointerOverGameObject())

#endif

Debug.Log("当前触摸在UI上");

else

Debug.Log("当前没有触摸在UI上");

}
方法来自雨凇MOMO,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity 解决方案 ui