Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Unity3d How detect touch on UI or not?

Tags:

unity3d

touch

In make Unity3d mobile application. And I have a problem: How detect touch on UI or not?

I try this (but now work)

UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()

and this

private static List<RaycastResult> tempRaycastResults = new List<RaycastResult>();

public bool PointIsOverUI(float x, float y)
{
    var eventDataCurrentPosition = new PointerEventData(EventSystem.current);

    eventDataCurrentPosition.position = new Vector2(x, y);

    tempRaycastResults.Clear();

    EventSystem.current.RaycastAll(eventDataCurrentPosition, tempRaycastResults);

    return tempRaycastResults.Count > 0;
}
like image 275
Knaus Irina Avatar asked Oct 23 '15 11:10

Knaus Irina


People also ask

How do I know if my monitor is touched in unity?

To detect a touch in Unity it's quite simple we just have to use Input. GetTouch() and pass it an index. These examples gets the touch of the last game frame.

How do I see UI in unity?

If the UI Layer is hidden, un-hide it. To do so just click on the Layers dropdown to the upper right of Unity's Editor and click on the layer you wish to un-hide.


2 Answers

For mobile you need to pass the id of the Touch to IsPointerOverGameObject

foreach (Touch touch in Input.touches)
{
    int id = touch.fingerId;
    if (EventSystem.current.IsPointerOverGameObject(id))
    {
        // ui touched
    }
 }
like image 118
Marius Junak Avatar answered Sep 18 '22 17:09

Marius Junak


Please try this :

// for Android check differently :

if(EventSystem.current.IsPointerOverGameObject(0) return false;

// for windows check as usual :

if (EventSystem.current.IsPointerOverGameObject())
return false;
like image 35
Али Паша Avatar answered Sep 20 '22 17:09

Али Паша