多轮审查和修复

This commit is contained in:
2026-05-12 15:34:08 +08:00
parent f55d2a57c3
commit ebbbb7332e
805 changed files with 838724 additions and 1905 deletions

View File

@@ -0,0 +1,51 @@
using UnityEngine;
namespace BaseGames.Localization
{
/// <summary>
/// 语言设置持久化 SO架构 16_SupportingModules §4.1)。
/// 支持的语言列表由设计师填写;当前语言持久化到 PlayerPrefs。
/// </summary>
[CreateAssetMenu(menuName = "Settings/LanguageManager", fileName = "SET_LanguageManager")]
public class LanguageManagerSO : ScriptableObject
{
private const string PrefsKey = "game_language";
[Tooltip("游戏支持的语言 locale code 列表(如 zh-CN、en-US")]
public string[] supportedLocales = { "zh-CN", "en-US" };
[Tooltip("默认语言 locale code")]
public string defaultLocale = "zh-CN";
public string CurrentLocale { get; private set; }
private void OnEnable()
{
CurrentLocale = PlayerPrefs.GetString(PrefsKey, defaultLocale);
}
/// <summary>设置当前语言并持久化到 PlayerPrefs同时通知 Unity Localization若已安装。</summary>
public void SetLocale(string localeCode)
{
CurrentLocale = localeCode;
PlayerPrefs.SetString(PrefsKey, localeCode);
PlayerPrefs.Save();
ApplyLocale(localeCode);
}
/// <summary>初始化时应用已保存语言。</summary>
public void ApplySaved() => ApplyLocale(CurrentLocale);
private static void ApplyLocale(string localeCode)
{
#if UNITY_LOCALIZATION
var locales = UnityEngine.Localization.Settings.LocalizationSettings.AvailableLocales;
var locale = locales?.GetLocale(new UnityEngine.Localization.LocaleIdentifier(localeCode));
if (locale != null)
UnityEngine.Localization.Settings.LocalizationSettings.SelectedLocale = locale;
#else
Debug.Log($"[LanguageManager] 语言设为 {localeCode}Unity Localization 包未安装,仅记录 PlayerPrefs。");
#endif
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0305b3bda1379324883e51f0fb0d5cb4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,59 @@
using System;
using UnityEngine;
namespace BaseGames.Localization
{
/// <summary>
/// 本地化字符串访问工具类(架构 16_SupportingModules §4
/// 当 Unity Localization 包com.unity.localization安装后使用 LocalizationSettings 实现完整功能;
/// 未安装时退回到 Key 直传(返回 entryKey 本身)。
/// </summary>
public static class LocalizationManager
{
#if UNITY_LOCALIZATION
private const string DefaultTable = "UI";
/// <summary>从指定表中获取本地化字符串。</summary>
public static string Get(string entryKey, string tableName = DefaultTable)
{
try
{
var op = UnityEngine.Localization.Settings.LocalizationSettings
.StringDatabase.GetLocalizedStringAsync(tableName, entryKey);
return op.IsDone ? op.Result : entryKey;
}
catch (Exception e)
{
Debug.LogWarning($"[Localization] Key '{entryKey}' in table '{tableName}' 读取失败: {e.Message}");
return entryKey;
}
}
/// <summary>带格式化参数的本地化字符串。</summary>
public static string GetFormat(string entryKey, string tableName, params object[] args)
{
string template = Get(entryKey, tableName);
try { return string.Format(template, args); }
catch (Exception e)
{
Debug.LogWarning($"[Localization] GetFormat '{entryKey}' 格式化失败: {e.Message}");
return template;
}
}
#else
/// <summary>Unity Localization 包未安装:直接返回 Key。</summary>
public static string Get(string entryKey, string tableName = "UI")
=> entryKey;
public static string GetFormat(string entryKey, string tableName, params object[] args)
{
try { return string.Format(entryKey, args); }
catch (Exception e)
{
Debug.LogWarning($"[Localization] GetFormat '{entryKey}' 格式化失败: {e.Message}");
return entryKey;
}
}
#endif
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 46d9a88adb6ede743a783e306209d4e2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: