Skip to content

Instantly share code, notes, and snippets.

@GibsS
Created July 20, 2023 07:07
Show Gist options
  • Save GibsS/3f067eceea48396316928306da08046d to your computer and use it in GitHub Desktop.
Save GibsS/3f067eceea48396316928306da08046d to your computer and use it in GitHub Desktop.
A Unity helper class to check if the player's pointer is over the UI. Useful to avoid doing world actions if the pointer is over a bit of UI. Works on desktop and mobile.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public static class IsPointerOverUI {
public static bool Check() {
if (EventSystem.current == null) return false;
PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
return results.Count > 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment