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

@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace BaseGames.Localization
{
/// <summary>
/// 本地化表 JSON 的对称解析 / 序列化(唯一格式真相源)。
///
/// 表格式:<c>{ "entries": [ { "key": "...", "value": "..." } ] }</c>
///
/// 运行时加载(<see cref="LocalizationManager"/>)与编辑器写盘(<c>LocalizationFileIO</c>
/// 都经此类,确保读写格式永远一致,杜绝"格式漂移"。
/// </summary>
public static class LocalizationSerializer
{
/// <summary>
/// 将表 JSON 文本解析为 key→value 字典。
/// 返回 null 表示格式无效entries 缺失)。空 key 条目被跳过。
/// </summary>
public static Dictionary<string, string> Parse(string jsonText)
{
if (string.IsNullOrEmpty(jsonText)) return null;
StringTableJson parsed;
try { parsed = JsonUtility.FromJson<StringTableJson>(jsonText); }
catch { return null; }
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;
}
/// <summary>
/// 将 key→value 字典序列化为表 JSON 文本(<c>{entries:[…]}</c> 格式pretty print
/// 该输出可被 <see cref="Parse"/> 与运行时加载器无损还原。
/// </summary>
/// <param name="dict">要序列化的字典。</param>
/// <param name="sortKeys">是否按 key 的序数顺序排序(默认 true减少版本控制 diff 噪声)。</param>
public static string Serialize(IReadOnlyDictionary<string, string> dict, bool sortKeys = true)
{
var table = new StringTableJson { entries = new List<StringEntry>(dict?.Count ?? 0) };
if (dict != null)
{
IEnumerable<string> keys = dict.Keys;
if (sortKeys)
{
var sorted = new List<string>(dict.Keys);
sorted.Sort(StringComparer.Ordinal);
keys = sorted;
}
foreach (var key in keys)
table.entries.Add(new StringEntry { key = key, value = dict[key] ?? string.Empty });
}
// JsonUtility 正确转义引号/反斜杠/换行,且保留中日韩字符原文(不转 \uXXXX
return JsonUtility.ToJson(table, prettyPrint: true);
}
// ── 序列化辅助类型(运行时与编辑器共用)────────────────────────────────
[Serializable]
internal class StringTableJson
{
public List<StringEntry> entries;
}
[Serializable]
internal class StringEntry
{
public string key;
public string value;
}
}
}