加入了浮动小游戏功能和设置界面
运行时请手动修改运行库目录fdBrowser
This commit is contained in:
@@ -4,71 +4,114 @@ using System.Collections.Generic;
|
||||
public class KeyBindingManager : MonoBehaviour
|
||||
{
|
||||
private static Dictionary<string, KeyCode> keyBindings = new Dictionary<string, KeyCode>();
|
||||
private static readonly string[] colors = { "red", "green", "yellow", "purple", "blue" };
|
||||
private static readonly KeyCode[] defaultKeys = { KeyCode.D, KeyCode.F, KeyCode.Space, KeyCode.J, KeyCode.K };
|
||||
private static bool initialized = false;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Debug.Log("KeyBindingManager Awake() 被调用,开始加载按键映射...");
|
||||
|
||||
if (keyBindings.Count == 0)
|
||||
if (!initialized)
|
||||
{
|
||||
LoadKeyBindings();
|
||||
}
|
||||
Debug.Log("KeyBindingManager Awake() 被调用,开始加载按键映射...");
|
||||
}
|
||||
|
||||
/// <summary> 获取颜色对应的按键 </summary>
|
||||
public static KeyCode GetKeyForColor(string color)
|
||||
{
|
||||
if (keyBindings.TryGetValue(color.ToLower(), out KeyCode key))
|
||||
if (!initialized)
|
||||
{
|
||||
LoadKeyBindings();
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(color)) return KeyCode.None;
|
||||
var keyLower = color.ToLower();
|
||||
if (keyBindings.TryGetValue(keyLower, out KeyCode key))
|
||||
{
|
||||
return key;
|
||||
}
|
||||
Debug.LogError($"未找到颜色 {color} 对应的按键!请检查 KeyBindingManager 是否正确初始化。");
|
||||
// fallback: try to map by index order
|
||||
for (int i = 0; i < colors.Length; i++)
|
||||
{
|
||||
if (colors[i] == keyLower)
|
||||
{
|
||||
return defaultKeys[i];
|
||||
}
|
||||
}
|
||||
return KeyCode.None;
|
||||
}
|
||||
|
||||
/// <summary> 修改按键绑定 </summary>
|
||||
public static void ChangeKeyBinding(string color, KeyCode newKey)
|
||||
{
|
||||
if (keyBindings.ContainsKey(color.ToLower()))
|
||||
{
|
||||
keyBindings[color.ToLower()] = newKey;
|
||||
}
|
||||
else
|
||||
{
|
||||
keyBindings.Add(color.ToLower(), newKey);
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(color)) return;
|
||||
var keyLower = color.ToLower();
|
||||
keyBindings[keyLower] = newKey;
|
||||
SaveKeyBindings();
|
||||
}
|
||||
|
||||
/// <summary> 存储按键绑定到 `PlayerPrefs` </summary>
|
||||
private static void SaveKeyBindings()
|
||||
{
|
||||
foreach (var kvp in keyBindings)
|
||||
for (int i = 0; i < colors.Length; i++)
|
||||
{
|
||||
PlayerPrefs.SetInt($"KeyBinding_{kvp.Key}", (int)kvp.Value);
|
||||
var col = colors[i];
|
||||
KeyCode k = defaultKeys[i];
|
||||
if (keyBindings.TryGetValue(col, out KeyCode stored)) k = stored;
|
||||
PlayerPrefs.SetInt($"KeyBinding_{col}", (int)k);
|
||||
}
|
||||
PlayerPrefs.Save();
|
||||
}
|
||||
|
||||
/// <summary> 从 `PlayerPrefs` 加载按键绑定 </summary>
|
||||
private static void LoadKeyBindings()
|
||||
{
|
||||
string[] colors = { "red", "green", "yellow", "purple", "blue" };
|
||||
KeyCode[] defaultKeys = { KeyCode.D, KeyCode.F, KeyCode.Space, KeyCode.J, KeyCode.K };
|
||||
|
||||
keyBindings.Clear();
|
||||
for (int i = 0; i < colors.Length; i++)
|
||||
{
|
||||
if (PlayerPrefs.HasKey($"KeyBinding_{colors[i]}"))
|
||||
string col = colors[i];
|
||||
if (PlayerPrefs.HasKey($"KeyBinding_{col}"))
|
||||
{
|
||||
keyBindings[colors[i]] = (KeyCode)PlayerPrefs.GetInt($"KeyBinding_{colors[i]}");
|
||||
keyBindings[col] = (KeyCode)PlayerPrefs.GetInt($"KeyBinding_{col}");
|
||||
}
|
||||
else
|
||||
{
|
||||
keyBindings[colors[i]] = defaultKeys[i];
|
||||
keyBindings[col] = defaultKeys[i];
|
||||
}
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
Debug.Log("KeyBindings 初始化成功:" + string.Join(", ", keyBindings));
|
||||
}
|
||||
|
||||
// Return a human-friendly display string for a KeyCode (symbols shown as their character)
|
||||
public static string GetDisplayName(KeyCode key)
|
||||
{
|
||||
if (key == KeyCode.Space) return "Space";
|
||||
switch (key)
|
||||
{
|
||||
case KeyCode.Quote: return "'"; // single quote
|
||||
case KeyCode.Semicolon: return ";";
|
||||
case KeyCode.Comma: return ",";
|
||||
case KeyCode.Period: return ".";
|
||||
case KeyCode.Slash: return "/";
|
||||
case KeyCode.Backslash: return "\\";
|
||||
case KeyCode.LeftBracket: return "[";
|
||||
case KeyCode.RightBracket: return "]";
|
||||
case KeyCode.Minus: return "-";
|
||||
case KeyCode.Equals: return "=";
|
||||
case KeyCode.BackQuote: return "`";
|
||||
case KeyCode.Keypad0: return "Num0";
|
||||
case KeyCode.Keypad1: return "Num1";
|
||||
case KeyCode.Keypad2: return "Num2";
|
||||
case KeyCode.Keypad3: return "Num3";
|
||||
case KeyCode.Keypad4: return "Num4";
|
||||
case KeyCode.Keypad5: return "Num5";
|
||||
case KeyCode.Keypad6: return "Num6";
|
||||
case KeyCode.Keypad7: return "Num7";
|
||||
case KeyCode.Keypad8: return "Num8";
|
||||
case KeyCode.Keypad9: return "Num9";
|
||||
default:
|
||||
// For letters and named keys, use ToString(); for better readability, split "Alpha0" -> "0"
|
||||
string s = key.ToString();
|
||||
if (s.StartsWith("Alpha") && s.Length > 5) return s.Substring(5);
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user