AI补足注释(已经快看不懂了)
实现i18n 优化部分模块的逻辑以优化性能 修复物品展示框打开时按下ESC唤出暂停菜单但没有暂停的bug Signed-off-by: Eicy <im@crash.work>
This commit is contained in:
@@ -1,21 +1,21 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using Localization;
|
||||
|
||||
namespace Dialog
|
||||
{
|
||||
// 对话类,包含对话的相关信息
|
||||
public class Dialog
|
||||
{
|
||||
public readonly int Index;
|
||||
public readonly string Content;
|
||||
public readonly string Type;
|
||||
public readonly string Animation;
|
||||
public readonly string Character;
|
||||
public readonly string DialogEvent;
|
||||
public readonly string DialogEventArg;
|
||||
public readonly int Index; // 对话索引
|
||||
public string Content; // 对话内容(会被本地化处理)
|
||||
public readonly string Type; // 对话类型
|
||||
public readonly string Animation; // 动画效果
|
||||
public readonly string Character; // 角色名称
|
||||
public readonly string DialogEvent; // 对话事件
|
||||
public readonly string DialogEventArg; // 对话事件参数
|
||||
|
||||
public Dialog(int index, string content, string type, string animation, string character, string dialogEvent,
|
||||
string dialogEventArg)
|
||||
public Dialog(int index, string content, string type, string animation, string character, string dialogEvent, string dialogEventArg)
|
||||
{
|
||||
Index = index;
|
||||
Content = content;
|
||||
@@ -27,10 +27,11 @@ namespace Dialog
|
||||
}
|
||||
}
|
||||
|
||||
// 物品文本类,包含物品的名称和描述
|
||||
public class ItemText
|
||||
{
|
||||
public readonly string Name;
|
||||
public readonly string Description;
|
||||
public string Name; // 物品名称
|
||||
public string Description; // 物品描述
|
||||
|
||||
public ItemText(string name, string description)
|
||||
{
|
||||
@@ -38,60 +39,52 @@ namespace Dialog
|
||||
Description = description;
|
||||
}
|
||||
}
|
||||
|
||||
// 对话管理器,负责加载和管理对话和物品文本
|
||||
public class DialogManager : MonoBehaviour
|
||||
{
|
||||
private readonly List<Dialog> _dialog = new();
|
||||
private readonly List<ItemText> _itemTexts = new();
|
||||
// 存储对话和物品文本的字典
|
||||
private readonly Dictionary<int, Dialog> _dialogDictionary = new();
|
||||
private readonly Dictionary<string, ItemText> _itemTextDictionary = new();
|
||||
private static DialogManager _instance;
|
||||
|
||||
public static DialogManager Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance)
|
||||
return _instance;
|
||||
_instance = FindFirstObjectByType<DialogManager>() ??
|
||||
new GameObject("DialogData").AddComponent<DialogManager>();
|
||||
// 确保只存在一个对话管理器实例
|
||||
if (_instance != null) return _instance;
|
||||
_instance = FindFirstObjectByType<DialogManager>() ??
|
||||
new GameObject("DialogManager").AddComponent<DialogManager>();
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// 初始化对话管理器
|
||||
if (_instance != null && _instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
Destroy(gameObject); // 如果已存在,则销毁当前对象
|
||||
}
|
||||
else
|
||||
{
|
||||
LoadCsv("Dialog/DialogData");
|
||||
LoadItemTexts("Dialog/ItemText");
|
||||
_instance = this;
|
||||
DontDestroyOnLoad(gameObject); // 保持在场景切换中不销毁
|
||||
LoadData("Dialog/DialogData", DataType.Dialog); // 加载对话数据
|
||||
LoadData("Dialog/ItemText", DataType.ItemText); // 加载物品文本数据
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadItemTexts(string resourcePath)
|
||||
// 数据类型枚举,用于区分加载的数据类型
|
||||
private enum DataType
|
||||
{
|
||||
var texts = Resources.Load<TextAsset>(resourcePath);
|
||||
if (texts == null)
|
||||
{
|
||||
Debug.LogError($"Unable to find CSV file at path: {resourcePath}");
|
||||
return;
|
||||
}
|
||||
var lines = texts.text.Split('\n');
|
||||
for (var i = 1; i < lines.Length; i++)
|
||||
{
|
||||
var values = lines[i].Split(',');
|
||||
if (values.Length < 2) continue;
|
||||
var text = new ItemText(
|
||||
values[0],
|
||||
values[1]
|
||||
);
|
||||
_itemTexts.Add(text);
|
||||
}
|
||||
Dialog,
|
||||
ItemText
|
||||
}
|
||||
|
||||
private void LoadCsv(string resourcePath)
|
||||
|
||||
// 加载指定路径的数据
|
||||
private void LoadData(string resourcePath, DataType dataType)
|
||||
{
|
||||
var textAsset = Resources.Load<TextAsset>(resourcePath);
|
||||
if (textAsset == null)
|
||||
@@ -100,44 +93,65 @@ namespace Dialog
|
||||
return;
|
||||
}
|
||||
|
||||
// 按行读取文本
|
||||
var lines = textAsset.text.Split('\n');
|
||||
for (var i = 1; i < lines.Length; i++)
|
||||
{
|
||||
var values = lines[i].Split(',');
|
||||
if (values.Length < 7) continue;
|
||||
var dialog = new Dialog(
|
||||
int.Parse(values[0]),
|
||||
values[1],
|
||||
values[2],
|
||||
values[3],
|
||||
values[4],
|
||||
values[5],
|
||||
values[6]
|
||||
);
|
||||
switch (dataType)
|
||||
{
|
||||
case DataType.Dialog:
|
||||
// 加载对话数据
|
||||
if (values.Length < 7) continue; // 检查字段数量
|
||||
var dialog = new Dialog(
|
||||
int.Parse(values[0].Trim()),
|
||||
values[1].Trim(),
|
||||
values[2].Trim(),
|
||||
values[3].Trim(),
|
||||
values[4].Trim(),
|
||||
values[5].Trim(),
|
||||
values[6].Trim()
|
||||
);
|
||||
_dialogDictionary[dialog.Index] = dialog; // 添加到字典
|
||||
dialog.Content = LocalizationManager.Instance.GetLocalizedValue(dialog.Content); // 本地化对话内容
|
||||
break;
|
||||
|
||||
_dialog.Add(dialog);
|
||||
case DataType.ItemText:
|
||||
// 加载物品文本数据
|
||||
if (values.Length < 2) continue; // 检查字段数量
|
||||
var itemNameKey = values[0].Trim();
|
||||
var itemDescriptionKey = values[1].Trim();
|
||||
var itemText = new ItemText(itemNameKey, itemDescriptionKey);
|
||||
_itemTextDictionary[itemNameKey] = itemText; // 添加到字典
|
||||
itemText.Name = LocalizationManager.Instance.GetLocalizedValue(itemText.Name); // 本地化物品名称
|
||||
itemText.Description = LocalizationManager.Instance.GetLocalizedValue(itemText.Description); // 本地化物品描述
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 根据索引获取对话
|
||||
public Dialog GetDialogByIndex(int index)
|
||||
{
|
||||
foreach (var dialog in _dialog.Where(dialogue => dialogue.Index == index))
|
||||
if (_dialogDictionary.TryGetValue(index, out var dialog))
|
||||
{
|
||||
return dialog;
|
||||
return dialog; // 返回找到的对话
|
||||
}
|
||||
|
||||
Debug.LogWarning($"Dialog with index {index} not found.");
|
||||
return null;
|
||||
return null; // 如果未找到则返回 null
|
||||
}
|
||||
|
||||
public ItemText GetItemText(string nName)
|
||||
// 根据名称获取物品文本
|
||||
public ItemText GetItemText(string name)
|
||||
{
|
||||
foreach (var text in _itemTexts.Where(text => text.Name == nName))
|
||||
if (_itemTextDictionary.TryGetValue(name, out var itemText))
|
||||
{
|
||||
return text;
|
||||
return itemText; // 返回找到的物品文本
|
||||
}
|
||||
Debug.LogWarning($"Dialog with name {nName} not found.");
|
||||
return null;
|
||||
|
||||
Debug.LogWarning($"ItemText with name {name} not found.");
|
||||
return null; // 如果未找到则返回 null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user