当前位置:网站首页>unity UGUI判断点击在UI上和3D物体上的解决方案
unity UGUI判断点击在UI上和3D物体上的解决方案
2022-04-23 06:27:00 【老少年】
1.通过EventSystem.current.IsPointerOverGameObject()的监听
/****************************************************
文件:ISOnclickUI.cs
作者:
邮箱: [email protected]
日期:2019/08/23 8:56
功能:判断是否点击在UI上
*****************************************************/
using UnityEngine;
using UnityEngine.EventSystems;
public class ISOnclickUI : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
{
#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.UI添加GraphicRaycaster组件,检测是否点击UI
在UI添加GraphicRaycaster组件, 在update里检测是否在ui上
1.先获取点击
PointerEventData data = new PointerEventData(EventSystem.current);
data.pressPosition = Input.mousePosition;
data.position = Input.mousePosition;
2.点击检测_raycaster是本物体的射线检测组件
List<RaycastResult> results = new List<RaycastResult>();
_raycaster.Raycast(data, results);
/****************************************************
文件:ISOnclickUIRaycast.cs
作者:
邮箱: [email protected]
日期:2019/08/23 9:29
功能:UI添加GraphicRaycaster组件,检测是否点击UI
*****************************************************/
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ISOnclickUIRaycast : MonoBehaviour
{
private GraphicRaycaster _raycaster;
// Start is called before the first frame update
void Start()
{
_raycaster = FindObjectOfType<GraphicRaycaster>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) ){
if (IsUI())
{
Debug.Log("在UI上");
}
else
{
Debug.Log("不在UI上");
}
}
}
private bool IsUI()
{
//获取点击
PointerEventData data = new PointerEventData(EventSystem.current);
data.pressPosition = Input.mousePosition;
data.position = Input.mousePosition;
//点击检测_raycaster是本物体的射线检测组件
List<RaycastResult> results = new List<RaycastResult>();
_raycaster.Raycast(data, results);
return results.Count > 0;
}
}
3.主摄像机添加PhysicsRaycaster,检测点击UI还是3D物体
需要检测的对象上添加IPointerClickHandler的实现,然后拿到PhysicsRaycaster的所有检测对象,名字是谁就检测到谁
/****************************************************
文件:IsOnclickUIor3D.cs
作者:
邮箱: [email protected]
日期:2019/08/23 10:02
功能:主摄像机添加PhysicsRaycaster,检测点击UI还是3D物体,或者
同时点击
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class IsOnclickUIor3D : MonoBehaviour, IPointerClickHandler
{
void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
{
ExecuteAll(eventData);
}
public void ExecuteAll(PointerEventData eventData)
{
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventData, results);
foreach (RaycastResult result in results)
{
if (result.gameObject != gameObject)
{
//点击传递给3d物体的点击事件监听
ExecuteEvents.Execute(result.gameObject, eventData, ExecuteEvents.pointerClickHandler);
Debug.Log("在UI上"+"传递"+ result.gameObject);
}
else
{
Debug.Log("在UI上");
}
}
}
}
版权声明
本文为[老少年]所创,转载请带上原文链接,感谢
https://blog.csdn.net/laoshaonian/article/details/100034333
边栏推荐
猜你喜欢

简易随机点名抽奖(js下编写)

SAP PI/PO登录使用及基本功能简介

SAP DEBUG调试FOR IN、REDUCE等复杂的语句

Super classic & Programming Guide (red and blue book) - Reading Notes

SAP CR传输请求顺序、依赖检查

canvas学习第一篇

'NPM' is not an internal or external command, nor is it a runnable program or batch file

对复杂字典Dictionary&lt;T1,T2&gt;排序问题

Configure NPM

Mysql 索引
随机推荐
js之作用域、作用域链、全局变量和局部变量
SAP PI/PO Soap2Proxy 消费外部ws示例
C# 多个矩形围成的多边形标注位置的问题
5. Sql99 standard: internal connection and external connection
CSDN很火的汤小洋老师全部课程总共有哪些(问号问号问号)
定位、修饰样式
Date object (JS built-in object)
MySQL8.0 安装/卸载 教程【Window10版】
利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)
SAP DEBUG调试FOR IN、REDUCE等复杂的语句
系统与软件安全研究(一)
4.多表查询
颜色转换公式大全及转换表格(31种)
keytool: command not found
游戏辅助脚本开发之旅
6. Aggregation function and grouping statistics
斐波拉去动态规划
SAP PI/PO rfc2RESTful 發布rfc接口為RESTful示例(Proxy間接法)
快速排序
js之预解析