当前位置:网站首页>Unity ugui determines the solution of clicking on the UI and 3D objects
Unity ugui determines the solution of clicking on the UI and 3D objects
2022-04-23 07:47:00 【youth who behaves like an adult】
1. adopt EventSystem.current.IsPointerOverGameObject() Listening in
/****************************************************
file :ISOnclickUI.cs
author :
mailbox : [email protected]
date :2019/08/23 8:56
function : Determine whether to click on UI On
*****************************************************/
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(" Current touch in UI On ");
else
Debug.Log(" There is currently no touch in UI On ");
}
}
}
2.UI add to GraphicRaycaster Components , Check whether to click UI
stay UI add to GraphicRaycaster Components , stay update Check whether it is in ui On
1. First get the click
PointerEventData data = new PointerEventData(EventSystem.current);
data.pressPosition = Input.mousePosition;
data.position = Input.mousePosition;
2. Click detection _raycaster It is the radiographic testing component of the object
List<RaycastResult> results = new List<RaycastResult>();
_raycaster.Raycast(data, results);
/****************************************************
file :ISOnclickUIRaycast.cs
author :
mailbox : [email protected]
date :2019/08/23 9:29
function :UI add to GraphicRaycaster Components , Check whether to click 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(" stay UI On ");
}
else
{
Debug.Log(" be not in UI On ");
}
}
}
private bool IsUI()
{
// Get Click
PointerEventData data = new PointerEventData(EventSystem.current);
data.pressPosition = Input.mousePosition;
data.position = Input.mousePosition;
// Click detection _raycaster It is the radiographic testing component of the object
List<RaycastResult> results = new List<RaycastResult>();
_raycaster.Raycast(data, results);
return results.Count > 0;
}
}
3. Main camera add PhysicsRaycaster, Detect Click UI still 3D object
Add... To the object to be detected IPointerClickHandler The implementation of the , And then to get PhysicsRaycaster All tested objects , Detect who the name is
/****************************************************
file :IsOnclickUIor3D.cs
author :
mailbox : [email protected]
date :2019/08/23 10:02
function : Main camera add PhysicsRaycaster, Detect Click UI still 3D object , perhaps
Click at the same time
*****************************************************/
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)
{
// Click pass to 3d Object click event monitoring
ExecuteEvents.Execute(result.gameObject, eventData, ExecuteEvents.pointerClickHandler);
Debug.Log(" stay UI On "+" Pass on "+ result.gameObject);
}
else
{
Debug.Log(" stay UI On ");
}
}
}
}
版权声明
本文为[youth who behaves like an adult]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230626191251.html
边栏推荐
- ABAP 从CDS VIEW 发布OData Service示例
- MySQL storage engine
- Apache Hudi 如何加速传统的批处理模式?
- [Ted series] how does a habit change my life
- 事件管理之一
- unity 屏幕自适应
- FUEL: Fast UAV Exploration using Incremental Frontier Structure and Hierarchical Planning
- 踩坑日记:Unable to process Jar entry [module-info.class]
- Configure NPM
- 简单理解==和equals,String为什么可以不用new
猜你喜欢

SAP pi / PO rfc2restful publishing RFC interface is a restful example (proxy indirect method)

防抖和节流

H5 local storage data sessionstorage, localstorage

Configure NPM

命令行参数传递库argparse的使用

Install and configure Taobao image NPM (cnpm)

Online Safe Trajectory Generation For Quadrotors Using Fast Marching Method and Bernstein Basis Poly

Nacos / sentinel gateway current limiting and grouping (code)

js之排他思想及案例

Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight
随机推荐
4. Multi table query
js案例之求最大值,反转数组,冒泡排序
ABAP 7.4 SQL Window Expression
Xamarin版的C# SVG路径解析器
颜色转换公式大全及转换表格(31种)
Online Safe Trajectory Generation For Quadrotors Using Fast Marching Method and Bernstein Basis Poly
js之什么是事件?事件三要素以及操作元素
事件系统(二)多播事件
[Ted series] how does a habit change my life
基于NLP的软件安全研究(二)
C#使用拉依达准则(3σ准则)剔除异常数据(.Net剔除一组数据中的奇异值)
SAP PI/PO rfc2RESTful 發布rfc接口為RESTful示例(Proxy間接法)
Judge whether the beginning and end of the string contain target parameters: startswith() and endswith() methods
Mvcc (multi version concurrency control)
Reflect on the limitations of event bus and the design and implementation of communication mechanism in component development process
2. Restricted query
NodeJS(六) 子进程操作
Two threads print odd and even numbers interactively
SAP 03-AMDP CDS Table Function using ‘WITH‘ Clause(Join子查询内容)
5. Sql99 standard: internal connection and external connection