Files
zeling_v2/Assets/_Game/Scripts/Localization/LanguageFontConfigSO.cs
2026-05-25 11:54:37 +08:00

60 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using TMPro;
using UnityEngine;
namespace BaseGames.Localization
{
/// <summary>
/// 每种语言对应的 TMP 字体配置。
/// 创建资产Assets/Data/Localization/FontConfig.asset
///
/// 用法:将此资产拖入 <see cref="BaseGames.Localization.LocalizedText"/> 的
/// <c>Font Config</c> 字段,切换语言时 LocalizedText 将自动替换字体和材质。
///
/// CJK 语言通常需要单独的字体资产,无需为每个文本节点逐一指定,
/// 只需在此 SO 中统一配置一次即可全局生效。
/// </summary>
[CreateAssetMenu(
menuName = "BaseGames/Localization/Language Font Config",
fileName = "FontConfig")]
public class LanguageFontConfigSO : ScriptableObject
{
[Serializable]
public class FontEntry
{
[Tooltip("对应的语言。")]
public Language language;
[Tooltip("该语言使用的 TMP 字体资产(留空表示沿用默认字体)。")]
public TMP_FontAsset fontAsset;
[Tooltip("该语言字体的材质预设(留空表示使用字体默认材质)。")]
public Material fontMaterial;
}
[Tooltip("每种语言的字体映射。未列出的语言保持默认字体不变。")]
public FontEntry[] entries = Array.Empty<FontEntry>();
/// <summary>
/// 尝试获取指定语言的字体配置。
/// 返回 false 表示该语言未配置,调用方应保持现有字体不变。
/// </summary>
public bool TryGetFont(Language language, out TMP_FontAsset font, out Material material)
{
if (entries != null)
{
foreach (var entry in entries)
{
if (entry.language != language) continue;
font = entry.fontAsset;
material = entry.fontMaterial;
return true;
}
}
font = null;
material = null;
return false;
}
}
}