当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
面经的总结
对js中argumens的简单理解
TimelineWindow
7. sub query
The difference and application of VR, AR and MR, as well as some implementation principles of AR technology
url转成对象
异步的学习
.NET 5 的新功能 What‘s new in .NET 5
SampleCameraFilter
系统与软件安全研究(五)
BTREE, B + tree and hash index
MySQL index
C# SmoothProgressBar自定义进度条控件
给定区段范围内字符串自生成代码
SAP PI / Po rfc2restful Publishing RFC interface as restful examples (proxy indirect)
unity UGUI判断点击在UI上和3D物体上的解决方案
How to judge whether a point is within a polygon (including complex polygons or a large number of polygons)
颜色转换公式大全及转换表格(31种)
移动布局(flex布局、视口标签)
Super classic & Programming Guide (red and blue book) - Reading Notes









