49 lines
1.4 KiB
C#
49 lines
1.4 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.TestTools;
|
|
|
|
namespace BracerLib.Utility
|
|
{
|
|
[ExcludeFromCoverage]
|
|
public static class UIUtility
|
|
{
|
|
private static bool performRefresh;
|
|
private static PointerEventData eventBuffer = new PointerEventData(EventSystem.current);
|
|
private static List<RaycastResult> raycastResultBuffer = new List<RaycastResult>(4);
|
|
|
|
static UIUtility()
|
|
{
|
|
SceneManager.activeSceneChanged += (before, after) => Refresh();
|
|
}
|
|
|
|
public static bool IsMouseOverUI(Vector2 position, int uiLayer)
|
|
{
|
|
var eventSystem = EventSystem.current;
|
|
eventBuffer.position = position;
|
|
eventSystem.RaycastAll(eventBuffer, raycastResultBuffer);
|
|
|
|
for (var i = 0; i < raycastResultBuffer.Count; i++)
|
|
{
|
|
var raycastResult = raycastResultBuffer[i];
|
|
|
|
if (raycastResult.gameObject.layer == uiLayer)
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private static void Refresh()
|
|
{
|
|
var current = EventSystem.current;
|
|
|
|
if (current == null)
|
|
return;
|
|
|
|
eventBuffer = new PointerEventData(EventSystem.current);
|
|
}
|
|
}
|
|
}
|