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;
}
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.
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.
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
}
}
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With