How to Detect Click/Touch Events on Ui and Gameobjects

How to detect click/touch events on UI and GameObjects

You don't use the Input API for the new UI. You subscribe to UI events or implement interface depending on the event.

These are the proper ways to detect events on the new UI components:

1.Image, RawImage and Text Components:

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:

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.

3.InputField Component:

You use events to register to register for InputField submit:

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:

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.

5.Dropdown Component

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:

6.For 3D Object (Mesh Renderer/any 3D Collider)

Add PhysicsRaycaster to the Camera then use any of the events from Method 1.

The code below will automatically add PhysicsRaycaster to the main 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)

Add Physics2DRaycaster to the Camera then use any of the events from Method 1.

The code below will automatically add Physics2DRaycaster to the main 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):

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.


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.


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.

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).


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.


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.


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.


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.

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.

Unity - Detect mouse click on specific element in array

I'm assuming that you have a bunch of non UI GameObject type elements on which you want to have an event available so you could listen for mouse clicks performed on them.

One solution would be to make a separate script with an event made using a C# delegate which would get triggered by OnMouseDown. You would need to attach this script to each one of your clickable elements. Then the script that contains your clickable element array would attach a method to each instance's event.

Example

using System;
using UnityEngine;

public class ClickableObject : MonoBehaviour
{
public event Action<ClickableObject> MouseDown;

// A collider component is required for OnMouseDown to work
private void OnMouseDown()
{
MouseDown?.Invoke(this);
}
}
using UnityEngine;

public class TestScript : MonoBehaviour
{
[SerializeField]
private ClickableObject[] clickableObjects = new ClickableObject[0];

private void Start()
{
foreach (ClickableObject unit in clickableObjects)
unit.MouseDown += OnClickableObjectMouseDown;
}

private void OnClickableObjectMouseDown(ClickableObject unit)
{
Debug.Log($"{unit.name} clicked!");
}

private void OnDestroy()
{
foreach (ClickableObject unit in clickableObjects)
unit.MouseDown -= OnClickableObjectMouseDown;
}
}

Get if Unity UI element is being pressed

You are missing the parameters and you have the wrong naming.
Have a look at the unity scripting reference for further information.

public static bool JUMP;
public void OnPointerDown(PointerEventData eventData){
JUMP = true;
}

public void OnPointerUp(PointerEventData eventData){
}

Unity Scripting Reference:
https://docs.unity3d.com/ScriptReference/UI.Selectable.OnPointerUp.html

Update
If you dont want to use OnPointerUp/Down Method. Another Solution would be to use

Input.GetTouch

These touch events hold "phases" telling you if the touch just began, or is released etc. So you can use the touch position of this event and check if it overlaps your UI element. If it is overlapping and is being pressed, well than you want to jump up. If it is released you want to stop jumping up.
You could check that in an update method of a script that you attach to your ui component.

https://docs.unity3d.com/ScriptReference/Input.GetTouch.html

Update2
U can use RectTransformUtility.ScreenPointToLocalPointInRectangle to find out, if your touch event hits your UI Element.



Related Topics



Leave a reply



Submit