49 lines
1.7 KiB
C#
49 lines
1.7 KiB
C#
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.InputSystem.LowLevel;
|
|
|
|
/// <summary>
|
|
/// Tracks which input device was most recently active, globally, across the whole game.
|
|
/// NPCStandIn (and any other UI that shows button prompts) reads InputDeviceTracker.Current
|
|
/// to decide whether to show "[F]", "(X)", "(✕)", etc.
|
|
/// Requires the Input System package (Window > Package Manager > Input System),
|
|
/// and Project Settings > Player > Active Input Handling set to
|
|
/// "Input System Package (New)" or "Both".
|
|
/// </summary>
|
|
public static class InputDeviceTracker
|
|
{
|
|
public enum DeviceType { Keyboard, Xbox, PlayStation, Generic }
|
|
|
|
public static DeviceType Current { get; private set; } = DeviceType.Keyboard;
|
|
|
|
[RuntimeInitializeOnLoadMethod]
|
|
static void Init()
|
|
{
|
|
InputSystem.onEvent += OnEvent;
|
|
}
|
|
|
|
static void OnEvent(InputEventPtr eventPtr, InputDevice device)
|
|
{
|
|
// Only care about actual state changes (button/stick/key presses), not noise.
|
|
if (!eventPtr.IsA<StateEvent>() && !eventPtr.IsA<DeltaStateEvent>()) return;
|
|
|
|
switch (device)
|
|
{
|
|
case Keyboard:
|
|
case Mouse:
|
|
Current = DeviceType.Keyboard;
|
|
break;
|
|
|
|
case Gamepad gp:
|
|
string n = gp.displayName.ToLowerInvariant();
|
|
if (n.Contains("dualshock") || n.Contains("dualsense") || n.Contains("playstation"))
|
|
Current = DeviceType.PlayStation;
|
|
else if (n.Contains("xbox") || n.Contains("xinput"))
|
|
Current = DeviceType.Xbox;
|
|
else
|
|
Current = DeviceType.Generic;
|
|
break;
|
|
}
|
|
}
|
|
}
|