Created
July 29, 2023 22:50
-
-
Save bgk-/8a1454639ddcf86e0ff4fa0d7d09eb7e to your computer and use it in GitHub Desktop.
Gamepad UI Module - Input Detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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