111

How to detect UI object on Canvas on Touch in android?

For example, I have a canvas that have 5 objects such as Image, RawImage, Buttons, InputField and so on.

When I touch on Button UI object Then do something. Each button do different process when clicked depending.

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?

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Dennis Liu
  • 2,268
  • 3
  • 21
  • 43

4 Answers4

227

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.

Noobles
  • 77
  • 8
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Hi @Programmer . If i don't use Input API like touch how is it going in mobile since mobile use touch ? Thanks – Dennis Liu Dec 30 '16 at 07:11
  • 1
    The UI API is meant to work on both mobile and desktop. That's the good side of it and this is not an issue at-all. I will update this to include other 3D(Mesh Renderer/Collider) and 2D(Sprite/2D Collider) in a moment. – Programmer Dec 30 '16 at 07:13
  • public void OnPointerClick(PointerEventData eventData) So this is work is mobile touch ? I don't realize it since i am using it too. Haha.. – Dennis Liu Dec 30 '16 at 07:20
  • Please Update for 3D(Mesh Renderer/Collider) and 2D(Sprite/2D Collider) in my question too.. Thanks – Dennis Liu Dec 30 '16 at 07:29
  • 2
    Added Slider and 3D, 2D example. This is what you should be using now insted of the Input system or the old Raycast ways. – Programmer Dec 30 '16 at 07:54
  • If I used OnClick() Feature that is on Interface Unity is that work for mobile too ? Like i create button than i make a function then in interface OnClick feature runtime i search for gameobject that attach script. Then the button refer to the function. Is that work for mobile touch ? So there is not need to attach using AddListerner if this can be manual pointing to it directly. – Dennis Liu Dec 30 '16 at 11:09
  • 4
    OnClick() is not a function from interface. You probably meant `OnPointerClick` which uses the `IPointerClickHandler` interface. Please read my answer again. *Everything* in my answer should work on desktop and mobile. I have seen some answer that tells people to use `OnPointerClick` for a Button component. This is so wrong. It will work but there is a bug they don't know about when you do that. To detect when a Button is clicked, use the event in my answer that uses `AddListerner`. Don't detect button click with `OnPointerClick`. `OnPointerClick` is for other components that is not a Button. – Programmer Dec 30 '16 at 11:28
  • If I may say, section 2 is fairly "ugly". I guess this is just to demonstrate but a method for each button instead of a switch is so much clearer and scalable. – Everts Feb 18 '18 at 11:32
  • @Everts Actually, it was done on purpose. Notice how it is different from others. It is done to show that you can **also** use one function when you have many buttons or UI components with **similar** actions.It's almost like grouping your UI and reducing the amount of unnecessary functions in your code – Programmer Feb 18 '18 at 14:22
  • @FahryMohammed It does. It works for dynamic spawned or cloned objects. – Programmer Aug 13 '18 at 12:53
  • What's the difference / Why not use onMouseUp() or onMouseDown() ? – shoopi Jul 11 '19 at 21:59
  • 4
    It would be useful to know what Game Objects to attach (or not attach) these scripts to. Some are obvious (button), but am I supposed to put #1 on the object I want to click on? On the camera? Does #7 go on my Game Object or the camera? Or is it somewhere else? – meed96 Oct 15 '19 at 17:25
  • If you have both GraphicRaycaster and PhysicsRaycaster and on GameObjects it does not work look at your canvas objects and disable Raycast Target on all background images. They are catching the events. – Unicorn Apr 03 '20 at 13:56
  • Not working for me at all. I can't figure out why. I followed your steps EXACTLY and exhausted all your troubleshooting steps. :( My game objects have Sprite Renderer and Box Collider 2D. – Ben Mora Dec 12 '20 at 01:36
  • I'm not getting the `OnPointerClick` callback with a `CompositeCollider2D`. Back when I was using a `PolygonCollider2D` I was getting the `OnPointerClick` callback fine :/ – Felix Jan 30 '21 at 01:08
  • Number 6 will work only if the cursor is moving. I am faking move delta with a small value 0.01f then -0.01f periodically... Any other way around it? – Radivarig Sep 12 '21 at 23:47
  • I was not getting any events triggered on my Image components on my UI/RectTransform objects, and I had to click the "Raycast Target" checkbox on the Image component to get anything to work. Just in case anybody else can't seem to figure it out, that's something else to check. – DJ_R Mar 18 '23 at 22:20
8

You can add an EventTrigger Componenet to Your UI elements that already have these Events you just have to pass method/Function on specific event.

waqas ali
  • 194
  • 8
5

You could use OnMouseDown as well. OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider. This event is sent to all scripts of the Collider or GUIElement.

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement; // The new load level needs this

public class ExampleClass : MonoBehaviour
{
    void OnMouseDown()
    {
        // Edit:
        // Application.LoadLevel("SomeLevel");
        // Application.LoadLevel() is depreciating but still works

         SceneManager.LoadScene("SomeLevel"); // The new way to load levels

    }
}
AntonioTorro
  • 199
  • 4
  • 20
4

Do not use OnMouseDown() for mobile performance and multi-touch issues.

This code works on UI Objects for multi-touches

In my answer, I use Image element with a "Button" tag and it has a ButtonController script with a ButtonDown() public method that should be called when user touches the Image element.

Note: Image element has a 2D Collider.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class TouchScript : MonoBehaviour
{
    void Update()
    {
        PointerEventData pointer = new PointerEventData(EventSystem.current);
        List<RaycastResult> raycastResult = new List<RaycastResult>();

        foreach (Touch touch in Input.touches)
        {
            if(touch.phase.Equals(TouchPhase.Began))
            {
                pointer.position = touch.position;
                EventSystem.current.RaycastAll(pointer, raycastResult);

                foreach(RaycastResult result in raycastResult)
                {
                    if(result.gameObject.tag == "Button")
                    {
                        result.gameObject.GetComponent<ButtonController>().ButtonDown();
                    }              
                }
                raycastResult.Clear();
            }       
        }
    }
}