Skip to content

Instantly share code, notes, and snippets.

@AldeRoberge
Created January 14, 2025 01:21
Show Gist options
  • Save AldeRoberge/a5adff98dd42a0b7812f06fca68a371c to your computer and use it in GitHub Desktop.
Save AldeRoberge/a5adff98dd42a0b7812f06fca68a371c to your computer and use it in GitHub Desktop.
Allows to check if a pointer is over a UI (works on touch devices too)
using UnityEngine;
using UnityEngine.EventSystems;
namespace Wikwemot_AR.Modules.Input.Runtime.Scripts
{
// See : https://discussions.unity.com/t/ispointerovergameobject-not-working-with-touch-input/155469/3
public static class IsPointerOverGameObjectUtils
{
/// <returns>true if mouse or first touch is over any event system object ( usually gui elements )</returns>
public static bool IsPointerOverGameObject()
{
//check mouse
if (EventSystem.current.IsPointerOverGameObject())
{
Debug.Log("[TOUCHES] Mouse is over a UI element");
return true;
}
//check touch
if (UnityEngine.Input.touchCount <= 0 || UnityEngine.Input.touches[0].phase != TouchPhase.Began)
{
Debug.Log("[TOUCHES] No touch detected or touch is not in the Began phase");
return false;
}
if (EventSystem.current.IsPointerOverGameObject(UnityEngine.Input.GetTouch(0).fingerId))
{
Debug.Log("[TOUCHES] Touch is over a UI element, there are " + UnityEngine.Input.touchCount + " touches");
return true;
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment