using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; namespace Ether { public enum DeviceType { Keyboard, Gamepad, } public class ReInputManager : SingletonAutoMono { ReInputActions inputActions; public override void Init() { inputActions = new ReInputActions(); inputActions.PC.Enable(); foreach (var actionMap in inputActions.asset.actionMaps) { foreach (var action in actionMap.actions) { string rebindJson = PlayerPrefs.GetString($"ReInputSave_{action.name}"); if (!string.IsNullOrEmpty(rebindJson)) { action.LoadBindingOverridesFromJson(rebindJson); } } } inputActions.PC.Move.performed += OnMove; inputActions.PC.Move.canceled += OnMove; //int bindIndex = GetActionBindIndex(inputActions.PC.Move, "s"); //if (bindIndex != -1) //{ // OnReBindAction(inputActions.PC.Move, bindIndex); //} InputSystem.onDeviceChange += OnDeviceChange; } private void OnMove(InputAction.CallbackContext context) { // 当输入操作被执行时调用 Debug.Log("输入操作被执行:" + context.ReadValue() + " " + context.phase); EventCenter.BroadCast(ReInputEvent.Move, context.ReadValue()); } #region 按键重绑定 /// /// 按键重绑定 /// public void OnReBindAction(InputAction action, int bindIndex, DeviceType deviceType = DeviceType.Keyboard, Action bindingEndCallback = null) { inputActions.PC.Disable(); Debug.Log($"当前绑定:{action.bindings[bindIndex].effectivePath} 请输入绑定按键..."); action.PerformInteractiveRebinding(bindIndex) .WithControlsExcluding("") .WithControlsExcluding("/leftbutton") .WithControlsExcluding("/rightbutton") .OnMatchWaitForAnother(0.1f) .WithCancelingThrough("/escape") .OnCancel((operation) => { inputActions.PC.Enable(); bindingEndCallback?.Invoke(); operation.Dispose(); operation = null; }) .OnComplete((operation) => { inputActions.PC.Enable(); if (CheckDuplicateBings(action, bindIndex)) { action.RemoveBindingOverride(bindIndex); operation?.Dispose(); operation = null; OnReBindAction(action, bindIndex); return; } bindingEndCallback?.Invoke(); string mapJson = action.SaveBindingOverridesAsJson(); PlayerPrefs.SetString($"ReInputSave_{action.name}", mapJson); Debug.Log("绑定成功:" + action.bindings[bindIndex].effectivePath); operation.Dispose(); operation = null; }) .Start(); } /// /// 检查是否存在对应的绑定索引 /// private bool CheckDuplicateBings(InputAction action, int bindIndex, bool allCompositeParts = false) { InputBinding newBinding = action.bindings[bindIndex]; foreach (var binding in action.actionMap.bindings) { if (binding.action == newBinding.action) { continue; } if (binding.effectivePath == newBinding.effectivePath) { return true; } } if (allCompositeParts) { for (int i = 0; i < bindIndex; i++) { if (action.bindings[i].effectivePath == newBinding.overridePath) { return true; } } } return false; } /// /// 获取绑定索引,根据绑定的key值 /// private int GetActionBindIndex(InputAction action, string bindName, DeviceType deviceType = DeviceType.Keyboard) { foreach (var ctrl in action.controls) { if (ctrl.device.name == deviceType.ToString()) { if (bindName.ToUpper() == ctrl.name.ToUpper()) { return action.GetBindingIndexForControl(ctrl); } } } return -1; } /// /// 还原绑定 /// public void OnReBindReset(InputAction action, int bindIndex, DeviceType deviceType = DeviceType.Keyboard, Action bindingEndCallback = null) { inputActions.PC.Disable(); if (CheckDuplicateBings(action, bindIndex)) { action.RemoveBindingOverride(bindIndex); bindingEndCallback?.Invoke(); } } #endregion void OnDeviceChange(InputDevice device, InputDeviceChange change) { if (change == InputDeviceChange.Added) { Debug.Log($"设备已连接: {device.name}"); } else if (change == InputDeviceChange.Removed) { Debug.Log($"设备已断开: {device.name}"); } } public void OnDestroy() { Clear(); } public override void Clear() { inputActions.PC.Move.performed -= OnMove; inputActions.PC.Move.canceled -= OnMove; InputSystem.onDeviceChange -= OnDeviceChange; } } }