Game Object under Cursor UI#
Input / Game Object under Cursor UI
Can Read
Cannot Write
Description#
The topmost user interface Game Object found under the Mouse cursor
Code#
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.Pool;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("Game Object under Cursor UI")]
[Category("Input/Game Object under Cursor UI")]
[Description("The topmost user interface Game Object found under the Mouse cursor")]
[Image(typeof(IconCursor), ColorTheme.Type.TextLight)]
[Keywords("Mouse", "Pointer", "Canvas", "Raycast", "Hover", "Widget")]
[Serializable]
public class TypeGameObjectInputCursorUI : TypeGameObject
{
[NonSerialized] private EventSystem m_EventSystem;
[NonSerialized] private PointerEventData m_PointerData;
public override GameObject Get(Args args)
{
Mouse mouse = Mouse.current;
if (mouse == null) return null;
EventSystem eventSystem = EventSystem.current;
if (eventSystem == null) return null;
if (this.m_PointerData == null || this.m_EventSystem != eventSystem)
{
this.m_EventSystem = eventSystem;
this.m_PointerData = new PointerEventData(eventSystem);
}
this.m_PointerData.position = mouse.position.ReadValue();
List<RaycastResult> results = ListPool<RaycastResult>.Get();
try
{
eventSystem.RaycastAll(this.m_PointerData, results);
GameObject result = results.Count > 0 ? results[0].gameObject : null;
return result;
}
finally
{
ListPool<RaycastResult>.Release(results);
}
}
public override string ToString()
{
return "Game Object under Cursor UI";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}