Skip to content

Instantly share code, notes, and snippets.

@bgk-
Created July 29, 2023 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgk-/8a1454639ddcf86e0ff4fa0d7d09eb7e to your computer and use it in GitHub Desktop.
Save bgk-/8a1454639ddcf86e0ff4fa0d7d09eb7e to your computer and use it in GitHub Desktop.
Gamepad UI Module - Input Detection
public class InputDetection : MonoBehaviour
{
[SerializeField] private GamepadUIInputModule gamepadUIInputModule;
[SerializeField] private InputSystemUIInputModule inputSystemUIInputModule;
public enum Type
{
Gamepad,
Keyboard
}
public static event Action<Type> OnInputSystemChange;
private static Type _currentType;
public static Type CurrentType
{
get => _currentType;
private set
{
if (_currentType == value) return;
_currentType = value;
OnInputSystemChange?.Invoke(value);
}
}
private void Awake()
{
if (Gamepad.current == null)
{
gamepadUIInputModule.enabled = false;
inputSystemUIInputModule.enabled = true;
CurrentType = Type.Keyboard;
}
InputSystem.onEvent.Call(OnEvent);
}
private void OnEvent(InputEventPtr obj)
{
switch (InputSystem.GetDeviceById(obj.deviceId))
{
case Keyboard when inputSystemUIInputModule != null && !inputSystemUIInputModule.enabled:
case Mouse m when m.leftButton.isPressed && inputSystemUIInputModule != null && !inputSystemUIInputModule.enabled:
ToggleType(Type.Keyboard);
break;
case Gamepad when gamepadUIInputModule != null && !gamepadUIInputModule.enabled:
ToggleType(Type.Gamepad);
break;
}
}
private void ToggleType(Type type)
{
switch (type)
{
case Type.Gamepad:
inputSystemUIInputModule.enabled = false;
gamepadUIInputModule.enabled = true;
break;
case Type.Keyboard:
gamepadUIInputModule.enabled = false;
inputSystemUIInputModule.enabled = true;
break;
}
CurrentType = type;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment