UI系统组件

This commit is contained in:
2026-06-06 09:00:11 +08:00
parent fe4fd60083
commit d794b83ebe
107 changed files with 25690 additions and 476 deletions

View File

@@ -1,7 +1,7 @@
// Assets/Scripts/Localization/LocalizationManager.cs
// 本地化管理器(运行时 JSON 文件驱动)。
//
// 数据格式(放在 Resources/Localization/{Language}/{TableName}.json
// 数据格式(放在 Assets/_Game/Data/Localization/{Language}/{TableName}.json
// {
// "entries": [
// { "key": "ui_start", "value": "开始游戏" },
@@ -317,7 +317,7 @@ namespace BaseGames.Localization
return cached;
// 编辑器按资产路径读取(与运行时 Addressable 地址对应的物理位置)
string path = $"Assets/_Game/Data/Localization/{language}/{table}.json";
string path = LocalizationPaths.AssetPath(language, table);
var asset = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>(path);
var dict = asset == null ? null : ParseTableText(asset.text);
s_editorPreviewCache[cacheKey] = dict;
@@ -349,7 +349,7 @@ namespace BaseGames.Localization
/// </summary>
private static Dictionary<string, string> LoadTable(Language language, string table)
{
string address = $"Localization/{language}/{table}";
string address = LocalizationPaths.Address(language, table);
// 统一经 AssetLoader 门面:缺键安全检查 + 同步加载 + 释放
if (!AssetLoader.Exists(address, typeof(TextAsset))) return null;
@@ -360,21 +360,12 @@ namespace BaseGames.Localization
}
/// <summary>
/// 将 JSON 文本解析为 key→value 字典(内部共享解析逻辑)
/// 将 JSON 文本解析为 key→value 字典。
/// 委托给 <see cref="LocalizationSerializer.Parse"/>,与编辑器写盘共用同一格式逻辑。
/// 返回 null 表示格式无效。
/// </summary>
private static Dictionary<string, string> ParseTableText(string jsonText)
{
var parsed = JsonUtility.FromJson<StringTableJson>(jsonText);
if (parsed?.entries == null) return null;
var dict = new Dictionary<string, string>(parsed.entries.Count, StringComparer.Ordinal);
foreach (var entry in parsed.entries)
if (!string.IsNullOrEmpty(entry.key))
dict[entry.key] = entry.value ?? string.Empty;
return dict;
}
=> LocalizationSerializer.Parse(jsonText);
// ── 缓存键(值类型,消除字符串插值 GC──────────────────────────────
private readonly struct CacheKey : IEquatable<CacheKey>
@@ -398,20 +389,5 @@ namespace BaseGames.Localization
public override int GetHashCode()
=> HashCode.Combine((int)_language, _table);
}
// ── 序列化辅助类型 ────────────────────────────────────────────────────
[Serializable]
private class StringTableJson
{
public List<StringEntry> entries;
}
[Serializable]
private class StringEntry
{
public string key;
public string value;
}
}
}