如何检测UI和GameObjects上的点击/触摸事件

如何检测Android上的触摸屏上的UI对象?

例如,我有一个canvas,有5个对象,如

ImageRawImageButtonsInputField等等。

当我触摸Button UI对象然后做一些事情。

点击时,每个button做不同的过程。

代码将如下所示:

 void Update() { if (Input.touches.Length > 0) { for (int i = 0; i < Input.touchCount; i++) { if (Button1.touch) { if (Input.GetTouch(i).phase == TouchPhase.Began) { // Call a Function login(); } } else if (Button2.touch) { if (Input.GetTouch(i).phase == TouchPhase.Began) { // Call a Function LogOut(); } } } } } 

那么该怎么做呢?

第二:如何检测GameObject的接触? 是否与上述相同?

您不使用新的用户界面的inputAPI。 您根据事件订阅UI事件或实现接口。

这些是在新的UI组件上检测事件的正确方法:

1ImageRawImageText组件:

实现所需的接口并覆盖其function。 下面的例子实现了最常用的事件。

 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"); } } 

2Button组件:

您使用事件来注册button点击:

 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(); } } 

如果您正在检测button以外的某个button,请单击该button,然后使用方法1.例如,按下button而不是button单击,使用方法1中的IPointerDownHandler及其OnPointerDown函数。

3InputField组件:

你使用事件来注册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(); } 

4Slider组件:

要在拖动过程中检测滑块值何时更改:

 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); }); } 

对于其他事件,请使用方法1

5Dropdown组件

 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) { } 

无UI对象:

6.对于3D对象(网格渲染器/任何3D对撞机)

PhysicsRaycaster添加到相机,然后使用方法1中的任何事件

下面的代码会自动添加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.对于2D对象(Sprite渲染器/任何2D对撞机)

Physics2DRaycaster添加到相机,然后使用方法1中的任何事件

下面的代码会自动添加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 } 

你可以添加一个EventTrigger到已经有这些Events UI元素,你只需要在特定事件上传递method/Function