using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.UI;
using UnityEngine.UI;
using UnityEngine.XR.Interaction.Toolkit.UI;
#pragma warning disable 0618 // ActionBasedController: deprecated in XRI 3, still what the gaze rig runs on
namespace jeanf.universalplayer
{
// This assembly references the sibling namespace jeanf.EventSystem (SO channels).
// Resolving the bare name 'EventSystem' climbs out to 'jeanf' and finds that
// NAMESPACE before any file-level alias, giving CS0118. The alias must live HERE,
// inside jeanf.universalplayer, so it is found first and wins.
using EventSystem = UnityEngine.EventSystems.EventSystem;
///
/// F8 (or LB+Select) in play mode: on-screen dump of the whole UGUI event
/// pipeline, built to answer "the hover ring reacts but the UI does nothing"
/// in M&K / gamepad. It shows, top to bottom:
/// 1. the active input module and whether it processes MOUSE / GAMEPAD input
/// (an XRUIInputModule with mouse OFF, or a module with no gamepad-bound
/// click, cannot drive desktop UI no matter what the canvas does);
/// 2. every root Canvas and which RAYCASTER it carries, with a verdict —
/// a world-space canvas with only a TrackedDeviceGraphicRaycaster (VR
/// ray/poke) and NO GraphicRaycaster is invisible to the mouse pointer,
/// so the desktop cursor can never click it (the reticle still tints,
/// because that tint comes from the separate physics ray);
/// 3. EventSystem.RaycastAll from the mouse position AND the screen center,
/// naming the raycaster that answered.
/// Reading it: 0 hits at the mouse over a control that visibly tints => a
/// raycaster problem (case 2). Hits show but clicks do nothing => the press
/// pipeline (case 1: module mouse/gamepad input, click bindings).
///
public class UiEventDebugOverlay : MonoBehaviour
{
private bool _visible;
private readonly List _results = new List();
private readonly StringBuilder _text = new StringBuilder(4096);
private GUIStyle _style;
private void Update()
{
if (Keyboard.current != null && Keyboard.current.f8Key.wasPressedThisFrame) _visible = !_visible;
if (Gamepad.current != null && Gamepad.current.selectButton.wasPressedThisFrame
&& Gamepad.current.leftShoulder.isPressed) _visible = !_visible; // LB+Select on gamepad
}
private void OnGUI()
{
if (!_visible) return;
if (_style == null) _style = new GUIStyle(GUI.skin.label) { fontSize = 12, richText = false };
_text.Length = 0;
var eventSystem = EventSystem.current;
_text.AppendLine("== UI EVENT DEBUG (F8 / LB+Select) ==");
_text.AppendLine($"scheme: {BroadcastControlsStatus.controlScheme} cursor lock: {Cursor.lockState} visible: {Cursor.visible}");
var mouse = Mouse.current;
_text.AppendLine($"mouse: {(mouse != null ? mouse.position.ReadValue().ToString("F0") : "")} gamepad: {(Gamepad.current != null ? "present" : "none")}");
_text.AppendLine("");
if (eventSystem == null)
{
_text.AppendLine("NO EventSystem in the scene — no UGUI event can EVER fire. Add one (EventSystem + an input module).");
}
else
{
AppendModule(eventSystem);
_text.AppendLine("");
AppendGazePressPipeline();
_text.AppendLine("");
AppendWorldUiInteractor();
_text.AppendLine("");
AppendPickup();
_text.AppendLine("");
AppendReticleHover();
_text.AppendLine("");
AppendCanvasInventory();
_text.AppendLine("");
var mousePosition = mouse != null ? mouse.position.ReadValue() : Vector2.zero;
AppendRaycast(eventSystem, "MOUSE pointer", mousePosition);
AppendRaycast(eventSystem, "screen center (gaze)", new Vector2(Screen.width * 0.5f, Screen.height * 0.5f));
}
var rect = new Rect(10, 10, 940, 640);
GUI.Box(rect, GUIContent.none);
GUI.Label(new Rect(rect.x + 8, rect.y + 6, rect.width - 16, rect.height - 12), _text.ToString(), _style);
}
// ---- 1. the input module and whether it can process desktop input ----
private void AppendModule(EventSystem eventSystem)
{
var module = eventSystem.currentInputModule;
_text.AppendLine($"EventSystem: '{eventSystem.gameObject.name}' module: {(module != null ? module.GetType().Name : "NULL — none active!")}");
_text.AppendLine($"selected: {(eventSystem.currentSelectedGameObject != null ? eventSystem.currentSelectedGameObject.name : "")}");
switch (module)
{
case XRUIInputModule xr:
_text.AppendLine($" XRUIInputModule — enableMouseInput: {(xr.enableMouseInput ? "yes" : "NO (desktop mouse cannot drive UI!)")}"
+ $" enableGamepadInput: {xr.enableGamepadInput} enableTouchInput: {xr.enableTouchInput} enableXRInput: {xr.enableXRInput}");
AppendClickAction(" leftClick", xr.leftClickAction != null ? xr.leftClickAction.action : null);
break;
case InputSystemUIInputModule ui:
_text.AppendLine(" InputSystemUIInputModule (standard). Note: GamepadScreenCursor only injects gamepad clicks into an XRUIInputModule — "
+ "gamepad A/trigger won't click through THIS module unless its Left Click action is gamepad-bound.");
AppendClickAction(" point", ui.point != null ? ui.point.action : null);
AppendClickAction(" leftClick", ui.leftClick != null ? ui.leftClick.action : null);
break;
case null:
_text.AppendLine(" (no active module — the EventSystem is disabled or its module component is missing)");
break;
default:
_text.AppendLine(" (unrecognized module type — mouse/gamepad capability unknown)");
break;
}
}
private void AppendClickAction(string label, InputAction action)
{
if (action == null)
{
_text.AppendLine($"{label}: ");
return;
}
var hasGamepad = false;
var hasMouse = false;
foreach (var b in action.bindings)
{
if (b.effectivePath != null && b.effectivePath.Contains("Gamepad")) hasGamepad = true;
if (b.effectivePath != null && b.effectivePath.Contains("Mouse")) hasMouse = true;
}
_text.AppendLine($"{label}: '{action.name}' enabled:{action.enabled} bindings:{action.bindings.Count} mouse:{(hasMouse ? "yes" : "no")} gamepad:{(hasGamepad ? "yes" : "NO")}");
}
// ---- 1b. the gaze-ray press pipeline (the pointer in locked desktop mode) ----
private void AppendGazePressPipeline()
{
var moduleCount = Object.FindObjectsByType(FindObjectsInactive.Include).Length;
if (moduleCount > 1)
_text.AppendLine($"!! {moduleCount} XRUIInputModules exist (incl. inactive) — duplicate EventSystems can hijack the pointer.");
var gazeClick = Object.FindAnyObjectByType(FindObjectsInactive.Include);
if (gazeClick == null)
{
_text.AppendLine("GazeDesktopClick: MISSING — locked-mode ray has no click/drag/scroll path (M&K falls back to the frozen mouse pointer, gamepad has nothing).");
return;
}
_text.AppendLine($"GazeDesktopClick: on '{gazeClick.gameObject.name}' enabled:{gazeClick.enabled} activeInHierarchy:{gazeClick.gameObject.activeInHierarchy}");
// THE transport: the module's screen pointer, driven by GazeDesktopClick's
// scheme-immune actions (same method for M&K and gamepad).
var point = gazeClick.DebugPointAction;
var click = gazeClick.DebugClickAction;
_text.AppendLine(point != null
? $" screen pointer POINT: enabled:{point.enabled} value:{point.ReadValue():F0}"
: " screen pointer POINT: ");
_text.AppendLine(click != null
? $" screen pointer CLICK: bindings:{click.bindings.Count} enabled:{click.enabled} PRESSED NOW: {click.IsPressed()} (hold your click/A while reading this!)"
: " screen pointer CLICK: ");
var lastPress = gazeClick.DebugLastPressEventTime < 0f
? "never"
: $"{Time.unscaledTime - gazeClick.DebugLastPressEventTime:F1}s ago";
_text.AppendLine($" click events fired: {gazeClick.DebugPressEventCount}x (last {lastPress})");
// The gaze ray is hover/tint feedback only (demoted from clicking).
var ray = gazeClick.DebugRay;
if (ray != null)
{
var xrModule = EventSystem.current != null ? EventSystem.current.currentInputModule as XRUIInputModule : null;
var registered = xrModule != null && xrModule.GetTrackedDeviceModel(ray, out _);
var uiHover = ray.TryGetCurrentUIRaycastResult(out var uiHit) && uiHit.gameObject != null
? uiHit.gameObject.name
: "";
_text.AppendLine($" gaze ray (hover feedback only): registered:{registered} UI hover: {uiHover}");
}
}
// ---- 1c. the world-canvas click path (TrackedDeviceGraphicRaycaster canvases) ----
private void AppendWorldUiInteractor()
{
var interactor = Object.FindAnyObjectByType(FindObjectsInactive.Include);
if (interactor == null)
{
_text.AppendLine("DesktopWorldUiInteractor: MISSING — world-space canvases (TrackedDevice-only) have NO desktop click/drag path.");
return;
}
_text.AppendLine($"DesktopWorldUiInteractor: on '{interactor.gameObject.name}' enabled:{interactor.enabled} "
+ $"activeInHierarchy:{interactor.gameObject.activeInHierarchy} scheme-active:{interactor.DebugSchemeActive}");
var press = interactor.DebugPressAction;
_text.AppendLine(press != null
? $" press: bindings:{press.bindings.Count} enabled:{press.enabled} PRESSED NOW: {press.IsPressed()} (hold your click/A while reading this!)"
: " press: ");
_text.AppendLine($" world-UI hover: {interactor.DebugHoverName} presses: {interactor.DebugPressCount}x clicks: {interactor.DebugClickCount}x dragging: {interactor.DebugDragging}");
}
// ---- 1d. the PICKUP chain (why "I click the tablet and nothing happens") ----
// Pickup dies silently in four places: no action, UI swallowed the press, the
// raycast missed (wrong layer / out of range / no collider), or the collider had
// no PickableObject on it or its parents. Walk the whole chain, live.
private void AppendPickup()
{
var take = Object.FindAnyObjectByType(FindObjectsInactive.Include);
if (take == null)
{
_text.AppendLine("PICKUP: no TakeObject in the scene — nothing can be picked up.");
return;
}
var action = take.DebugTakeAction;
_text.AppendLine($"PICKUP (TakeObject on '{take.gameObject.name}') holding: "
+ $"{(take.DebugObjectInHand != null ? $"'{take.DebugObjectInHand.name}'" : "")}");
_text.AppendLine(action != null
? $" take action: '{action.name}' enabled:{action.enabled} PRESSED NOW: {action.IsPressed()} (hold your click while reading this!)"
: " take action: ");
var mask = take.DebugLayerMask;
_text.AppendLine($" layer mask: {MaskToNames(mask)} max distance: {take.DebugMaxDistance:0.##}m");
if (take.DebugUiOwnsPress)
{
_text.AppendLine(" >> BLOCKED: world UI owns this press (and it is not the face of a pickable).");
return;
}
var camera = take.DebugCamera;
if (camera == null) { _text.AppendLine(" >> Main Camera not assigned — the grab ray has no origin."); return; }
// The exact ray Take() casts.
var mouse = Mouse.current;
var pointer = mouse != null
? (Vector3)mouse.position.ReadValue()
: new Vector3(Screen.width * 0.5f, Screen.height * 0.5f, 0f);
var ray = camera.ScreenPointToRay(pointer);
if (Physics.Raycast(ray, out var masked, take.DebugMaxDistance, mask))
{
var pickable = masked.collider.GetComponentInParent();
_text.AppendLine($" masked ray HIT '{masked.collider.name}' at {masked.distance:0.##}m "
+ $"(layer '{LayerMask.LayerToName(masked.collider.gameObject.layer)}')");
_text.AppendLine(pickable != null
? $" >> PickableObject: '{pickable.name}' (slot {pickable.Slot}, anchor {pickable.Anchor}) — a click SHOULD take it."
: " >> NO PickableObject on that collider or its parents — add one (or its collider belongs to something else).");
return;
}
// Missed. Say what is actually in front, which turns "nothing" into a cause.
if (!Physics.Raycast(ray, out var any, 25f, ~0, QueryTriggerInteraction.Collide))
{
_text.AppendLine(" >> the grab ray hits NOTHING within 25m. Does the target have a Collider? "
+ "(A world-space Canvas is NOT hittable by a physics raycast.)");
return;
}
var layer = any.collider.gameObject.layer;
var inMask = (mask.value & (1 << layer)) != 0;
var found = any.collider.GetComponentInParent();
_text.AppendLine($" masked ray MISSED. Nearest collider ahead: '{any.collider.name}' at {any.distance:0.##}m, "
+ $"layer '{LayerMask.LayerToName(layer)}'");
_text.AppendLine($" >> PickableObject on it: {(found != null ? $"YES ('{found.name}')" : "NO")}"
+ $" layer in mask: {(inMask ? "yes" : "NO <-- THE PROBLEM: add this layer to TakeObject's Layer Mask")}"
+ $" in range: {(any.distance <= take.DebugMaxDistance ? "yes" : $"NO ({any.distance:0.##}m > {take.DebugMaxDistance:0.##}m)")}");
}
private static string MaskToNames(LayerMask mask)
{
if (mask.value == 0) return "";
var names = new List();
for (var i = 0; i < 32; i++)
{
if ((mask.value & (1 << i)) == 0) continue;
var name = LayerMask.LayerToName(i);
if (!string.IsNullOrEmpty(name)) names.Add(name);
}
return names.Count > 0 ? string.Join(", ", names) : mask.value.ToString();
}
// ---- 1e. the RETICLE COLOUR (why it never returns to its resting colour) ----
// FOUR independent sources can tint it, and it only rests when ALL of them are
// off — so one that is stuck on holds the reticle lit forever. Show each.
private void AppendReticleHover()
{
var reticle = Object.FindAnyObjectByType(FindObjectsInactive.Include);
if (reticle == null)
{
_text.AppendLine("RETICLE: no ReticleHoverFeedback — the reticle cannot tint at all.");
return;
}
var xri = reticle.DebugXriHoverCount;
var gaze = reticle.DebugGazeRayUiHover;
var worldUi = reticle.DebugWorldUiHover;
var physics = reticle.DebugPhysicsHover;
var tinted = xri > 0 || gaze || worldUi || physics;
_text.AppendLine($"RETICLE colour: {(reticle.DebugInteractHeld ? "CLICK (held)" : tinted ? "HOVER" : "resting (default)")}"
+ $" [cached tinted:{reticle.DebugTinted}]");
_text.AppendLine($" hover sources -> XRI interactor:{xri} gaze-ray UI:{gaze} world-UI:{worldUi} physics:{physics}");
_text.AppendLine($" gazeRayUI: {reticle.DebugGazeRayUiDescription()}");
if (tinted)
_text.AppendLine(" >> the reticle is TINTED because of the source(s) above that are true. "
+ "It only returns to the resting colour when ALL FOUR are false.");
_text.AppendLine($" physics mask: {MaskToNames(reticle.DebugPhysicsMask)} "
+ $"(mask ray {reticle.DebugPhysicsDistance:0.##}m, usable ray {reticle.DebugInteractableDistance:0.##}m)");
_text.AppendLine(" " + reticle.DebugPhysicsHitDescription());
}
// ---- 2. every canvas and whether the mouse can click it ----
private void AppendCanvasInventory()
{
var canvases = Object.FindObjectsByType