128 lines
3.8 KiB
C#
128 lines
3.8 KiB
C#
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using Ichni.UI;
|
|||
|
|
using TMPro;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.InputSystem;
|
|||
|
|
using UnityEngine.Serialization;
|
|||
|
|
using UnityEngine.UI;
|
|||
|
|
|
|||
|
|
namespace Ichni.Menu
|
|||
|
|
{
|
|||
|
|
public partial class RebindingWindow : SettingsWindow
|
|||
|
|
{
|
|||
|
|
public InputActionAsset inputActions;
|
|||
|
|
|
|||
|
|
public Dictionary<string, List<RebindActionData>> rebindActions;
|
|||
|
|
public TextButton resetButton;
|
|||
|
|
|
|||
|
|
public string rebindsFilePath => Application.persistentDataPath + "/GameData/Rebindings.json";
|
|||
|
|
public InputActionRebindingExtensions.RebindingOperation rebindingOperation;
|
|||
|
|
|
|||
|
|
public override void Initialize()
|
|||
|
|
{
|
|||
|
|
// 为每个需要重绑定的动作按钮添加监听器
|
|||
|
|
foreach (RebindActionData data in rebindActions["Keyboard"])
|
|||
|
|
{
|
|||
|
|
// 在C# 8.0+ 中,可以使用静态匿名函数来避免闭包分配
|
|||
|
|
data.rebindButton.SetUp(data.title, data.subtitle, data.actionName, data.bindingIndex);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
resetButton.SetUp("MenuUI/Settings_ResetRebinding");
|
|||
|
|
resetButton.updateValueAction = ResetAllBindings;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public override void SetValuesFromSettings()
|
|||
|
|
{
|
|||
|
|
LoadBindings();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public override void SwitchOut()
|
|||
|
|
{
|
|||
|
|
rebindingOperation?.Dispose();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public partial class RebindingWindow
|
|||
|
|
{
|
|||
|
|
public void LoadBindings()
|
|||
|
|
{
|
|||
|
|
// 检查EasySave中是否存在存档
|
|||
|
|
if (ES3.FileExists(rebindsFilePath))
|
|||
|
|
{
|
|||
|
|
string rebindsJson = ES3.Load<string>("Rebinds", rebindsFilePath);
|
|||
|
|
inputActions.LoadBindingOverridesFromJson(rebindsJson);
|
|||
|
|
Debug.Log("键位已从EasySave加载。");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Debug.Log("未找到已保存的键位,使用默认设置。");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 刷新所有UI显示
|
|||
|
|
UpdateAllButtonUIText();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void SaveBindings()
|
|||
|
|
{
|
|||
|
|
string rebindsJson = inputActions.SaveBindingOverridesAsJson();
|
|||
|
|
ES3.Save("Rebinds", rebindsJson, rebindsFilePath);
|
|||
|
|
Debug.Log("键位已保存到EasySave。");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public partial class RebindingWindow
|
|||
|
|
{
|
|||
|
|
// 重置所有键位到默认
|
|||
|
|
public void ResetAllBindings()
|
|||
|
|
{
|
|||
|
|
// 移除所有覆盖(即恢复到Asset文件中的原始设置)
|
|||
|
|
inputActions.RemoveAllBindingOverrides();
|
|||
|
|
|
|||
|
|
// 从EasySave中删除存档
|
|||
|
|
if (ES3.KeyExists("Rebind", rebindsFilePath))
|
|||
|
|
{
|
|||
|
|
ES3.DeleteKey("Rebind", rebindsFilePath);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Debug.Log("所有键位已重置为默认值。");
|
|||
|
|
|
|||
|
|
// 更新UI
|
|||
|
|
UpdateAllButtonUIText();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public partial class RebindingWindow
|
|||
|
|
{
|
|||
|
|
// 更新所有按钮的UI文本
|
|||
|
|
private void UpdateAllButtonUIText()
|
|||
|
|
{
|
|||
|
|
foreach (RebindActionData data in rebindActions["Keyboard"])
|
|||
|
|
{
|
|||
|
|
data.UpdateUIText();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[System.Serializable]
|
|||
|
|
public class RebindActionData
|
|||
|
|
{
|
|||
|
|
public string title;
|
|||
|
|
[FormerlySerializedAs("description")] public string subtitle;
|
|||
|
|
|
|||
|
|
[Tooltip("必须与Input Actions Asset中的Action名称完全匹配")]
|
|||
|
|
public string actionName;
|
|||
|
|
|
|||
|
|
[Tooltip("该Action要修改的绑定索引。主绑定通常是0")]
|
|||
|
|
public int bindingIndex = 0;
|
|||
|
|
|
|||
|
|
[Tooltip("用于触发重绑定的按钮")]
|
|||
|
|
public KeyRebindButton rebindButton;
|
|||
|
|
|
|||
|
|
public void UpdateUIText()
|
|||
|
|
{
|
|||
|
|
rebindButton.UpdateUIText(actionName, bindingIndex);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|