204 lines
6.2 KiB
C#
204 lines
6.2 KiB
C#
|
|
|||
|
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<ReInputManager>
|
|||
|
{
|
|||
|
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<Vector2>() + " " + context.phase);
|
|||
|
EventCenter.BroadCast(ReInputEvent.Move, context.ReadValue<Vector2>());
|
|||
|
}
|
|||
|
|
|||
|
#region 按键重绑定
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 按键重绑定
|
|||
|
/// </summary>
|
|||
|
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("<Mouse>")
|
|||
|
.WithControlsExcluding("<Mouse>/leftbutton")
|
|||
|
.WithControlsExcluding("<Mouse>/rightbutton")
|
|||
|
.OnMatchWaitForAnother(0.1f)
|
|||
|
.WithCancelingThrough("<Keyboard>/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();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 检查是否存在对应的绑定索引
|
|||
|
/// </summary>
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 获取绑定索引,根据绑定的key值
|
|||
|
/// </summary>
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 还原绑定
|
|||
|
/// </summary>
|
|||
|
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;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|