Created
July 20, 2023 07:07
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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