Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using UnityEngine.InputSystem;
- namespace _Project.Core.Input
- {
- public class InputDeviceSwapController : Single<InputDeviceSwapController>
- {
- public enum ControlScheme { MouseAndKeyboard, Gamepad }
- public static event EventHandler OnGamepadUnused;
- public static event EventHandler OnGamepadUsed;
- [field: SerializeField] public ControlScheme MyControlScheme { get; set; }
- [field: SerializeField] public bool IsCursorVisible { get; set; }
- private void OnEnable() => RegisterDeviceSwap();
- private void RegisterDeviceSwap() => InputSystem.onActionChange += HandleInputSystemOnActionChange;
- private void HandleInputSystemOnActionChange(object obj, InputActionChange change)
- {
- if (change == InputActionChange.ActionStarted)
- {
- var inputAction = (InputAction) obj;
- var lastControl = inputAction.activeControl;
- var lastDevice = lastControl.device;
- if (lastDevice == Gamepad.current)
- {
- //Using GamePad
- MyControlScheme = ControlScheme.Gamepad;
- OnGamepadUsed?.Invoke(this, EventArgs.Empty);
- Cursor.visible = false;
- }
- else if (lastDevice == Keyboard.current || lastDevice == Mouse.current)
- {
- //Using Mouse & Keyboard
- MyControlScheme = ControlScheme.MouseAndKeyboard;
- OnGamepadUnused?.Invoke(this, EventArgs.Empty);
- Cursor.visible = IsCursorVisible;
- }
- //Debug.Log($"device: {lastDevice.displayName}");
- }
- }
- public static bool IsUsingGamepad() => Get.MyControlScheme == ControlScheme.Gamepad;
- public static bool IsUsingMouseAndKeyboard() => Get.MyControlScheme == ControlScheme.MouseAndKeyboard;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement