如何检测 UI 和游戏对象上的点击/触摸事件
A:
How to detect UI object on Canvas on Touch in android?
如何在android中检测Canvas on Touch上的UI对象?
For example, I have a canvas that have 5 objects such as Image
, RawImage
, Buttons
, InputField
and so on.
例如,我有一个画布,其中有 5 个对象,例如Image
、 RawImage
、 Buttons
、 InputField
等。
When I touch on Button UI object Then do something. Each button do different process when clicked depending.
当我触摸 Button UI 对象时然后执行某些操作。单击时,每个按钮都会执行不同的过程。
The code will look like:
代码如下所示:
private void Update()
{
if (Input.touches.Length <= 0) return;
for (int i = 0; i < Input.touchCount; i++)
{
if (Button1.touch)
if (Input.GetTouch(i).phase == TouchPhase.Began)
login();
else if (Button2.touch && Input.GetTouch(i).phase == TouchPhase.Began)
LogOut();
}
}
So how to do it?
那么该怎么做呢?
Second: How to detect Gameobject get touch? Is it same with that above or not?
第二:如何检测Gameobject被触摸?是不是和上面的一样呢?
Q:
You don’t use the Input API for the new UI. You subscribe to UI events or implement interface depending on the event.
您不使用新 UI 的输入 API。您可以订阅 UI 事件或根据事件实现接口。
These are the proper ways to detect events on the new UI components:
以下是检测新 UI 组件上的事件的正确方法:
1.Image
, RawImage
and Text
Components:
1 . Image
、 RawImage
和Text
组件:
Implement the needed interface and override its function. The example below implements the most used events.
实现所需的接口并覆盖其功能。下面的示例实现了最常用的事件。
using UnityEngine.EventSystems;
public class ClickDetector : MonoBehaviour, IPointerDownHandler, IPointerClickHandler,
IPointerUpHandler, IPointerExitHandler, IPointerEnterHandler,
IBeginDragHandler, IDragHandler, IEndDragHandler
{
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("Drag Begin");
}
public void OnDrag(PointerEventData eventData)
{
Debug.Log("Dragging");
}
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("Drag Ended");
}
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Mouse Down: " + eventData.pointerCurrentRaycast.gameObject.name);
}
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("Mouse Enter");
}
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("Mouse Exit");
}
public void OnPointerUp(PointerEventData eventData)
{
Debug.Log("Mouse Up");
}
}
2.Button
Component:
2 . Button
组件:
You use events to register to Button clicks:
您使用事件来注册按钮点击:
public class ButtonClickDetector : MonoBehaviour
{
public Button button1;
public Button button2;
public Button button3;
void OnEnable()
{
//Register Button Events
button1.onClick.AddListener(() => buttonCallBack(button1));
button2.onClick.AddListener(() => buttonCallBack(button2));
button3.onClick.AddListener(() => buttonCallBack(button3));
}
private void buttonCallBack(Button buttonPressed)
{
if (buttonPressed == button1)
{
//Your code for button 1
Debug.Log("Clicked: " + button1.name);
}
if (buttonPressed == button2)
{
//Your code for button 2
Debug.Log("Clicked: " + button2.name);
}
if (buttonPressed == button3)
{
//Your code for button 3
Debug.Log("Clicked: " + button3.name);
}
}
void OnDisable()
{
//Un-Register Button Events
button1.onClick.RemoveAllListeners();
button2.onClick.RemoveAllListeners();
button3.onClick.RemoveAllListeners();
}
}
If you are detecting something other than Button Click on the Button then use method 1. For example, Button down and not Button Click, use IPointerDownHandler
and its OnPointerDown
function from method 1.
如果您检测到按钮上的按钮单击以外的其他内容,请使用方法 1。例如,按钮按下而不是按钮单击,请使用方法 1 中的IPointerDownHandler
及其OnPointerDown
函数。
3.InputField
Component:
3 . InputField
组件:
You use events to register to register for InputField submit:
您使用事件来注册来注册InputField提交:
public InputField inputField;
void OnEnable()
{
//Register InputField Events
inputField.onEndEdit.AddListener(delegate { inputEndEdit(); });
inputField.onValueChanged.AddListener(delegate { inputValueChanged(); });
}
//Called when Input is submitted
private void inputEndEdit()
{
Debug.Log("Input Submitted");
}
//Called when Input changes
private void inputValueChanged()
{
Debug.Log("Input Changed");
}
void OnDisable()
{
//Un-Register InputField Events
inputField.onEndEdit.RemoveAllListeners();
inputField.onValueChanged.RemoveAllListeners();
}
4.Slider
Component:
4 . Slider
组件:
To detect when slider value changes during drag:
要检测拖动过程中滑块值何时发生变化:
public Slider slider;
void OnEnable()
{
//Subscribe to the Slider Click event
slider.onValueChanged.AddListener(delegate { sliderCallBack(slider.value); });
}
//Will be called when Slider changes
void sliderCallBack(float value)
{
Debug.Log("Slider Changed: " + value);
}
void OnDisable()
{
//Un-Subscribe To Slider Event
slider.onValueChanged.RemoveListener(delegate { sliderCallBack(slider.value); });
}
For other events, use Method 1.
对于其他事件,请使用方法 1 。
5.Dropdown
Component
5 . Dropdown
组件
public Dropdown dropdown;
void OnEnable()
{
//Register to onValueChanged Events
//Callback with parameter
dropdown.onValueChanged.AddListener(delegate { callBack(); });
//Callback without parameter
dropdown.onValueChanged.AddListener(callBackWithParameter);
}
void OnDisable()
{
//Un-Register from onValueChanged Events
dropdown.onValueChanged.RemoveAllListeners();
}
void callBack()
{
}
void callBackWithParameter(int value)
{
}
NON-UI OBJECTS: 非 UI 对象:
6.For 3D Object (Mesh Renderer/any 3D Collider)
6.对于3D对象(网格渲染器/任何3D碰撞器)
Add PhysicsRaycaster
to the Camera then use any of the events from Method 1.
将PhysicsRaycaster
添加到相机,然后使用方法1 中的任何事件。
The code below will automatically add PhysicsRaycaster
to the main Camera
.
下面的代码将自动将PhysicsRaycaster
添加到主Camera
中。
public class MeshDetector : MonoBehaviour, IPointerDownHandler
{
void Start()
{
addPhysicsRaycaster();
}
void addPhysicsRaycaster()
{
PhysicsRaycaster physicsRaycaster = GameObject.FindObjectOfType<PhysicsRaycaster>();
if (physicsRaycaster == null)
{
Camera.main.gameObject.AddComponent<PhysicsRaycaster>();
}
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
}
//Implement Other Events from Method 1
}
7.For 2D Object (Sprite Renderer/any 2D Collider)
7.对于2D对象(Sprite渲染器/任何2D碰撞器)
Add Physics2DRaycaster
to the Camera then use any of the events from Method 1.
将Physics2DRaycaster
添加到相机,然后使用方法1 中的任何事件。
The code below will automatically add Physics2DRaycaster
to the main Camera
.
下面的代码将自动将Physics2DRaycaster
添加到主Camera
中。
public class SpriteDetector : MonoBehaviour, IPointerDownHandler
{
void Start()
{
addPhysics2DRaycaster();
}
void addPhysics2DRaycaster()
{
Physics2DRaycaster physicsRaycaster = GameObject.FindObjectOfType<Physics2DRaycaster>();
if (physicsRaycaster == null)
{
Camera.main.gameObject.AddComponent<Physics2DRaycaster>();
}
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
}
//Implement Other Events from Method 1
}
Troubleshooting the EventSystem:
事件系统故障排除:
No clicks detected on UI, 2D Objects (Sprite Renderer/any 2D Collider) and 3D Objects (Mesh Renderer/any 3D Collider):
在 UI、2D 对象(Sprite 渲染器/任何 2D 碰撞器)和 3D 对象(网格渲染器/任何 3D 碰撞器)上未检测到点击:
A.Check that you have EventSystem. Without EventSystem it can’t detect clicks at-all. If you don’t have have it, create it yourself.
A.检查您是否有EventSystem。如果没有 EventSystem,它根本无法检测点击。如果您没有,请自行创建。
Go to GameObject —> UI —> Event System. This will create an EventSystem if it doesn’t exist yet. If it already exist, Unity will just ignore it.
转到游戏对象—> UI —>事件系统。如果事件系统尚不存在,这将创建它。如果它已经存在,Unity 将忽略它。
B.The UI component or GameObject with the UI component must be under a Canvas
. It means that a Canvas
must be the parent of the UI component. Without this, EventSystem will not function and clicks will not be detected.
B.UI组件或带有UI组件的GameObject必须位于Canvas
下。这意味着Canvas
必须是 UI 组件的父组件。如果没有这个,EventSystem 将无法运行并且不会检测到点击。
This only applies to UI Objects. It doesn’t apply to 2D (Sprite Renderer/any 2D Collider) or 3D Objects (Mesh Renderer/any 3D Collider).
这仅适用于 UI 对象。它不适用于 2D(精灵渲染器/任何 2D 碰撞器)或 3D 对象(网格渲染器/任何 3D 碰撞器)。
C.If this is a 3D Object, PhysicsRaycaster
is not attached to the camera. Make sure that PhysicsRaycaster
is attached to the camera. See #6 above for more information.
C .如果这是一个 3D 对象, PhysicsRaycaster
不会连接到相机。确保PhysicsRaycaster
已连接到相机。有关更多信息,请参阅上面#6 。
D.If this is a 2D Object, Physics2DRaycaster
is not attached to the camera. Make sure that Physics2DRaycaster
is attached to the camera. See #7 above for more information.
D .如果这是一个 2D 对象, Physics2DRaycaster
不会连接到相机。确保Physics2DRaycaster
已连接到相机。有关更多信息,请参阅上面#7 。
E.If this is a UI object you want to detect clicks on with the interface functions such as OnBeginDrag
, OnPointerClick
, OnPointerEnter
and other functions mentioned in #1 then the script with the detection code must be attached to that UI Object you want to detect click on.
E .如果这是一个 UI 对象,您想要使用OnBeginDrag
、 OnPointerClick
、 OnPointerEnter
和#1中提到的其他函数等界面函数检测点击,那么带有检测代码的脚本必须附加到您想要检测的 UI 对象单击。
F.Also, if this is a UI Object you want to detect clicks on, make sure that no other UI Object is in front of it. If there is another UI in front of the one you want to detect click on, it will be blocking that click.
F 。此外,如果这是您要检测点击的 UI 对象,请确保其前面没有其他 UI 对象。如果您想要检测点击的 UI 前面有另一个 UI,它将阻止该点击。
To verify that this is not the issue, disable every object under the Canvas except the one you want to detect click on then see if clicking it works.
要验证这不是问题,请禁用画布下的每个对象(除了您想要检测点击的对象),然后查看点击是否有效。
本站发布的内容若无意中侵犯到您的权益,请联系我们,本站将在一个工作日内删除。如遇到任何问题请联系客服QQ:2385367137
912sy » 如何检测 UI 和游戏对象上的点击/触摸事件