当前位置:网站首页>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
边栏推荐
- 【TED系列】如何与内心深处的批评家相处?
- 刨根问底---cocos2d源码的理解与分析
- ABAP 实现发布RESTful服务供外部调用示例
- SAP PI / Po rfc2restful Publishing RFC interface as restful examples (proxy indirect)
- SAP TRANSLATE使用数据对象掩码示例
- FSM finite state machine
- 游戏辅助脚本开发之旅
- SAP PI/PO Soap2Proxy 消费外部ws示例
- SAP SALV14 后台输出SALV数据可直接保存文件,发送Email(带排序、超链接、筛选格式)
- 如何判断点是否在多边形内(包含复杂多边形或者多边形数量很多的情况)
猜你喜欢

ABAP CDS VIEW WITH ASSOCIATION示例

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

防抖和节流

SAP PI/PO rfc2RESTful 发布rfc接口为RESTful示例(Proxy间接法)

将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)

反思 | Android 音视频缓存机制的系统性设计

Django uses MySQL database to solve error reporting

Apache Hudi 如何加速传统的批处理模式?

BTREE, B + tree and hash index

redis连接出错 ERR AUTH <password> called without any password configured for the default user.
随机推荐
js之预解析
Learn to use search engines
canvas学习第一篇
12. Constraints
BTREE, B + tree and hash index
Ogldev reading notes
将单行文字自动适应到目标矩形框内
7. sub query
判断字符串首尾是否包含目标参数:startsWith()、endsWith()方法
SAP TRANSLATE使用数据对象掩码示例
利用网页表格导出EXCEL表格加线框及表格内部间距的问题
ogldev-读书笔记
移动端布局(3D转换、动画)
4.多表查询
What are the total number of all courses of Mr. Tang Xiaoyang, who is very popular in CSDN (question mark)
SAP SALV14 后台输出SALV数据可直接保存文件,发送Email(带排序、超链接、筛选格式)
Preliminary configuration of OpenGL super Dictionary (freeglut, glew, gltools, GLUT)
SAP PI/PO功能运行状态监控检查
Common DOS commands
驼峰命名对像