AI补足注释(已经快看不懂了)
实现i18n 优化部分模块的逻辑以优化性能 修复物品展示框打开时按下ESC唤出暂停菜单但没有暂停的bug Signed-off-by: Eicy <im@crash.work>
This commit is contained in:
@@ -6,46 +6,50 @@ using Newtonsoft.Json;
|
||||
|
||||
namespace Keyboard
|
||||
{
|
||||
// 管理键位设置,包括加载、保存和获取按键
|
||||
public class KeySettingManager : MonoBehaviour
|
||||
{
|
||||
public List<KeyMapping> keyMappings = new();
|
||||
public string filePath;
|
||||
public List<KeyMapping> keyMappings = new(); // 存储键位映射
|
||||
public string filePath; // 键位设置文件路径
|
||||
|
||||
private static KeySettingManager _instance;
|
||||
private static KeySettingManager _instance; // 单例实例
|
||||
|
||||
// 单例属性
|
||||
public static KeySettingManager Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance)
|
||||
return _instance;
|
||||
_instance = FindFirstObjectByType<KeySettingManager>() ??
|
||||
if (_instance) return _instance; // 如果实例已存在,返回实例
|
||||
// 否则,查找场景中的实例或创建新实例
|
||||
_instance = FindFirstObjectByType<KeySettingManager>() ??
|
||||
new GameObject("KeySettingManager").AddComponent<KeySettingManager>();
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
public Vector2 Direction { get; private set; }
|
||||
public Vector2 Direction { get; private set; } // 存储方向
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// 确保只有一个实例存在
|
||||
if (_instance != null && _instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
Destroy(gameObject); // 销毁重复的实例
|
||||
}
|
||||
else
|
||||
{
|
||||
_instance = this;
|
||||
_instance = this; // 设置单例实例
|
||||
}
|
||||
|
||||
DontDestroyOnLoad(gameObject);
|
||||
filePath = Application.persistentDataPath + "/" + "KeySetting.json";
|
||||
LoadKeySettings();
|
||||
DontDestroyOnLoad(gameObject); // 确保在场景切换时不被销毁
|
||||
filePath = Application.persistentDataPath + "/" + "KeySetting.json"; // 设置文件路径
|
||||
LoadKeySettings(); // 加载键位设置
|
||||
}
|
||||
|
||||
//加载键位设置
|
||||
// 加载键位设置
|
||||
private void LoadKeySettings()
|
||||
{
|
||||
// 如果文件存在,读取并反序列化
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
var json = File.ReadAllText(filePath);
|
||||
@@ -53,85 +57,58 @@ namespace Keyboard
|
||||
}
|
||||
else
|
||||
{
|
||||
//如果文件不存在,则创建默认键位设置
|
||||
keyMappings.Add(new KeyMapping("InterAct", KeyCode.E));
|
||||
keyMappings.Add(new KeyMapping("Left", KeyCode.A));
|
||||
keyMappings.Add(new KeyMapping("Right", KeyCode.D));
|
||||
keyMappings.Add(new KeyMapping("Up", KeyCode.W));
|
||||
keyMappings.Add(new KeyMapping("Down", KeyCode.S));
|
||||
keyMappings.Add(new KeyMapping("Run", KeyCode.LeftShift));
|
||||
SaveKeySettings();
|
||||
// 如果文件不存在,创建默认键位设置
|
||||
CreateDefaultKeyMappings();
|
||||
SaveKeySettings(); // 保存默认设置
|
||||
}
|
||||
}
|
||||
|
||||
//保存键位设置
|
||||
// 创建默认键位设置
|
||||
private void CreateDefaultKeyMappings()
|
||||
{
|
||||
keyMappings.Add(new KeyMapping("InterAct", KeyCode.E));
|
||||
keyMappings.Add(new KeyMapping("Left", KeyCode.A));
|
||||
keyMappings.Add(new KeyMapping("Right", KeyCode.D));
|
||||
keyMappings.Add(new KeyMapping("Up", KeyCode.W));
|
||||
keyMappings.Add(new KeyMapping("Down", KeyCode.S));
|
||||
keyMappings.Add(new KeyMapping("Run", KeyCode.LeftShift));
|
||||
}
|
||||
|
||||
// 保存键位设置
|
||||
private void SaveKeySettings()
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(keyMappings, Formatting.Indented);
|
||||
File.WriteAllText(filePath, json);
|
||||
File.WriteAllText(filePath, json); // 写入JSON文件
|
||||
}
|
||||
|
||||
//获取键位
|
||||
// 获取指定动作的按键
|
||||
public KeyCode GetKey(string actionName)
|
||||
{
|
||||
return (from mapping in keyMappings where mapping.actionName == actionName select mapping.keyCode)
|
||||
.FirstOrDefault();
|
||||
return keyMappings.FirstOrDefault(mapping => mapping.actionName == actionName).keyCode; // 返回对应的按键
|
||||
}
|
||||
|
||||
//设置键位
|
||||
// 设置指定动作的按键
|
||||
public void SetKey(string actionName, KeyCode newKeyCode)
|
||||
{
|
||||
foreach (var mapping in keyMappings.Where(mapping => mapping.actionName == actionName))
|
||||
var mapping = keyMappings.FirstOrDefault(m => m.actionName == actionName);
|
||||
if (mapping != null)
|
||||
{
|
||||
mapping.keyCode = newKeyCode;
|
||||
break;
|
||||
mapping.keyCode = newKeyCode; // 更新键位
|
||||
SaveKeySettings(); // 保存更改
|
||||
}
|
||||
|
||||
SaveKeySettings();
|
||||
}
|
||||
|
||||
//更新Direction
|
||||
// 更新方向
|
||||
private void Update()
|
||||
{
|
||||
if (Input.GetKey(GetKey("Left")))
|
||||
{
|
||||
Direction = new Vector2(-1, Direction.y);
|
||||
}
|
||||
// 重置方向为零
|
||||
Direction = Vector2.zero;
|
||||
|
||||
if (Input.GetKey(GetKey("Right")))
|
||||
{
|
||||
Direction = new Vector2(1, Direction.y);
|
||||
}
|
||||
|
||||
if (Input.GetKey(GetKey("Up")))
|
||||
{
|
||||
Direction = new Vector2(Direction.x, 1);
|
||||
}
|
||||
|
||||
if (Input.GetKey(GetKey("Down")))
|
||||
{
|
||||
Direction = new Vector2(Direction.x, -1);
|
||||
}
|
||||
|
||||
if (Input.GetKey(GetKey("Left")) && Input.GetKey(GetKey("Right")))
|
||||
{
|
||||
Direction = new Vector2(0, Direction.y);
|
||||
}
|
||||
|
||||
if (!Input.GetKey(GetKey("Left")) && !Input.GetKey(GetKey("Right")))
|
||||
{
|
||||
Direction = new Vector2(0, Direction.y);
|
||||
}
|
||||
|
||||
if (Input.GetKey(GetKey("Up")) && Input.GetKey(GetKey("Down")))
|
||||
{
|
||||
Direction = new Vector2(Direction.x, 0);
|
||||
}
|
||||
|
||||
if (!Input.GetKey(GetKey("Up")) && !Input.GetKey(GetKey("Down")))
|
||||
{
|
||||
Direction = new Vector2(Direction.x, 0);
|
||||
}
|
||||
// 根据按键更新方向
|
||||
if (Input.GetKey(GetKey("Left"))) Direction += Vector2.left;
|
||||
if (Input.GetKey(GetKey("Right"))) Direction += Vector2.right;
|
||||
if (Input.GetKey(GetKey("Up"))) Direction += Vector2.up;
|
||||
if (Input.GetKey(GetKey("Down"))) Direction += Vector2.down;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user