From 8e31a795cd6e271d085c2765dd85e8d72f2b27a5 Mon Sep 17 00:00:00 2001
From: lcy <1459594991@qq.com>
Date: 星期四, 02 四月 2026 18:12:40 +0800
Subject: [PATCH] 592 多语言适配
---
Main/Component/UI/Core/TextLanguageAdapter.cs | 228 ++++++++++++++++++--------------------------------------
1 files changed, 74 insertions(+), 154 deletions(-)
diff --git a/Main/Component/UI/Core/TextLanguageAdapter.cs b/Main/Component/UI/Core/TextLanguageAdapter.cs
index 027e39a..edcd1c2 100644
--- a/Main/Component/UI/Core/TextLanguageAdapter.cs
+++ b/Main/Component/UI/Core/TextLanguageAdapter.cs
@@ -3,9 +3,7 @@
using UnityEngine;
using UnityEngine.UI;
-/// <summary>
-/// 鏂囨湰缁勪欢绫诲瀷鏋氫妇
-/// </summary>
+/// <summary>鏂囨湰缁勪欢绫诲瀷鏋氫妇</summary>
public enum TextComponentType
{
None = 0,
@@ -14,13 +12,10 @@
GradientText = 3
}
-/// <summary>
-/// 澶氳瑷�鎺掔増閰嶇疆椤癸紝瀛樺偍鍗曚釜璇█鐨勫竷灞�鍜岄�傞厤鏁版嵁
-/// </summary>
+/// <summary>鍗曚釜璇█鐨勬帓鐗堝拰閫傞厤鏁版嵁鑺傜偣</summary>
[Serializable]
public class LanguageConfigItem
{
- // ============ RectTransform 閰嶇疆 ============
[Header("RectTransform 閰嶇疆")]
public Vector2 anchoredPosition = Vector2.zero;
public Vector2 sizeDelta = new Vector2(100f, 30f);
@@ -30,102 +25,83 @@
public Vector3 localScale = Vector3.one;
public Vector3 localRotation = Vector3.zero;
- // ============ Text 鎺掔増涓庨�傞厤閰嶇疆 ============
[Header("Text 鎺掔増涓庨�傞厤閰嶇疆")]
public Font font;
public FontStyle fontStyle = FontStyle.Normal;
public int fontSize = 14;
public float lineSpacing = 1f;
-
public HorizontalWrapMode horizontalOverflow = HorizontalWrapMode.Wrap;
public VerticalWrapMode verticalOverflow = VerticalWrapMode.Truncate;
-
public bool resizeTextForBestFit = false;
public int resizeTextMinSize = 10;
public int resizeTextMaxSize = 40;
-
public TextAnchor alignment = TextAnchor.UpperLeft;
public bool alignByGeometry = false;
- /// <summary> 灏嗛厤缃簲鐢ㄥ埌 RectTransform </summary>
- public void ApplyToRectTransform(RectTransform rectTransform)
+ public void ApplyToRectTransform(RectTransform rt)
{
- if (rectTransform == null) return;
- rectTransform.anchorMin = anchorMin;
- rectTransform.anchorMax = anchorMax;
- rectTransform.pivot = pivot;
- rectTransform.anchoredPosition = anchoredPosition;
- rectTransform.sizeDelta = sizeDelta;
- rectTransform.localScale = localScale;
- rectTransform.localRotation = Quaternion.Euler(localRotation);
+ if (rt == null) return;
+ rt.anchorMin = anchorMin;
+ rt.anchorMax = anchorMax;
+ rt.pivot = pivot;
+ rt.anchoredPosition = anchoredPosition;
+ rt.sizeDelta = sizeDelta;
+ rt.localScale = localScale;
+ rt.localRotation = Quaternion.Euler(localRotation);
}
- /// <summary> 浠� RectTransform 璇诲彇閰嶇疆 </summary>
- public void ReadFromRectTransform(RectTransform rectTransform)
+ public void ReadFromRectTransform(RectTransform rt)
{
- if (rectTransform == null) return;
- anchorMin = rectTransform.anchorMin;
- anchorMax = rectTransform.anchorMax;
- pivot = rectTransform.pivot;
- anchoredPosition = rectTransform.anchoredPosition;
- sizeDelta = rectTransform.sizeDelta;
- localScale = rectTransform.localScale;
- localRotation = rectTransform.localRotation.eulerAngles;
+ if (rt == null) return;
+ anchorMin = rt.anchorMin;
+ anchorMax = rt.anchorMax;
+ pivot = rt.pivot;
+ anchoredPosition = rt.anchoredPosition;
+ sizeDelta = rt.sizeDelta;
+ localScale = rt.localScale;
+ localRotation = rt.localRotation.eulerAngles;
}
- /// <summary> 灏嗛厤缃簲鐢ㄥ埌 Text </summary>
- public void ApplyToText(Text textComponent)
+ public void ApplyToText(Text txt)
{
- if (textComponent == null) return;
- if (font != null) textComponent.font = font;
+ if (txt == null) return;
+ if (font != null) txt.font = font;
- textComponent.fontStyle = fontStyle;
- textComponent.fontSize = fontSize;
- textComponent.lineSpacing = lineSpacing;
- textComponent.horizontalOverflow = horizontalOverflow;
- textComponent.verticalOverflow = verticalOverflow;
- textComponent.resizeTextForBestFit = resizeTextForBestFit;
-
+ txt.fontStyle = fontStyle;
+ txt.fontSize = fontSize;
+ txt.lineSpacing = lineSpacing;
+ txt.horizontalOverflow = horizontalOverflow;
+ txt.verticalOverflow = verticalOverflow;
+ txt.resizeTextForBestFit = resizeTextForBestFit;
if (resizeTextForBestFit)
{
- textComponent.resizeTextMinSize = resizeTextMinSize;
- textComponent.resizeTextMaxSize = resizeTextMaxSize;
+ txt.resizeTextMinSize = resizeTextMinSize;
+ txt.resizeTextMaxSize = resizeTextMaxSize;
}
-
- textComponent.alignment = alignment;
- textComponent.alignByGeometry = alignByGeometry;
+ txt.alignment = alignment;
+ txt.alignByGeometry = alignByGeometry;
}
- /// <summary> 浠� Text 璇诲彇閰嶇疆 </summary>
- public void ReadFromText(Text textComponent)
+ public void ReadFromText(Text txt)
{
- if (textComponent == null) return;
- font = textComponent.font;
- fontStyle = textComponent.fontStyle;
- fontSize = textComponent.fontSize;
- lineSpacing = textComponent.lineSpacing;
- horizontalOverflow = textComponent.horizontalOverflow;
- verticalOverflow = textComponent.verticalOverflow;
- resizeTextForBestFit = textComponent.resizeTextForBestFit;
- resizeTextMinSize = textComponent.resizeTextMinSize;
- resizeTextMaxSize = textComponent.resizeTextMaxSize;
- alignment = textComponent.alignment;
- alignByGeometry = textComponent.alignByGeometry;
+ if (txt == null) return;
+ font = txt.font;
+ fontStyle = txt.fontStyle;
+ fontSize = txt.fontSize;
+ lineSpacing = txt.lineSpacing;
+ horizontalOverflow = txt.horizontalOverflow;
+ verticalOverflow = txt.verticalOverflow;
+ resizeTextForBestFit = txt.resizeTextForBestFit;
+ resizeTextMinSize = txt.resizeTextMinSize;
+ resizeTextMaxSize = txt.resizeTextMaxSize;
+ alignment = txt.alignment;
+ alignByGeometry = txt.alignByGeometry;
}
- /// <summary>
- /// 蹇�熷厠闅嗛厤缃�
- /// 鐢变簬鍐呴儴鍧囦负鍊肩被鍨嬫垨闇�娴呮嫹璐濈殑寮曠敤(Font)锛屽彲浠ョ洿鎺ヤ娇鐢� MemberwiseClone
- /// </summary>
- public LanguageConfigItem Clone()
- {
- return (LanguageConfigItem)this.MemberwiseClone();
- }
+ public LanguageConfigItem Clone() => (LanguageConfigItem)MemberwiseClone();
}
-/// <summary>
-/// 璇█閰嶇疆瀛楀吀锛圲nity 鍐呯疆搴忓垪鍖栨敮鎸佺殑鍙� List 缁撴瀯锛�
-/// </summary>
+/// <summary>鏀寔 Unity 搴忓垪鍖栫殑瀛楀吀</summary>
[Serializable]
public class LanguageConfigDictionary
{
@@ -136,16 +112,13 @@
{
if (string.IsNullOrEmpty(key)) return null;
int index = keys.IndexOf(key);
- return (index >= 0 && index < values.Count) ? values[index] : null;
+ return index >= 0 ? values[index] : null;
}
public void Set(string key, LanguageConfigItem value)
{
int index = keys.IndexOf(key);
- if (index >= 0 && index < values.Count)
- {
- values[index] = value;
- }
+ if (index >= 0) values[index] = value;
else
{
keys.Add(key);
@@ -161,48 +134,36 @@
if (index >= 0)
{
keys.RemoveAt(index);
- if (index < values.Count) values.RemoveAt(index);
+ values.RemoveAt(index);
}
}
}
-/// <summary>
-/// 鏂囨湰澶氳瑷�鎺掔増閫傞厤缁勪欢
-/// </summary>
+/// <summary>澶氳瑷�鎺掔増閫傞厤鍣�</summary>
[RequireComponent(typeof(RectTransform))]
public class TextLanguageAdapter : MonoBehaviour
{
- public const string DefaultLangId = "default"; // 鎻愬彇甯搁噺閬垮厤纭紪鐮侀敊璇�
+ public const string DefaultLangId = "default";
- [SerializeField]
- [Tooltip("姣忕璇█鐨勯厤缃紝浣跨敤璇█ID浣滀负key锛堝zh銆乪n銆乫t锛夛紝default涓洪粯璁ら厤缃�")]
+ [SerializeField, Tooltip("璇█閰嶇疆瀛楀吀")]
private LanguageConfigDictionary m_LanguageConfigs = new LanguageConfigDictionary();
-
- [SerializeField]
- [Tooltip("鑷姩妫�娴嬪埌鐨勬枃鏈粍浠剁被鍨�")]
+
+ [SerializeField, Tooltip("鐩爣鏂囨湰缁勪欢绫诲瀷")]
private TextComponentType m_TargetTextType = TextComponentType.None;
-
- [SerializeField]
- [Tooltip("鍏宠仈鐨勬枃鏈粍浠跺紩鐢�")]
+
+ [SerializeField, Tooltip("鍏宠仈鐨勬枃鏈粍浠跺紩鐢�")]
private Component m_TargetTextComponent;
private bool m_IsApplied = false;
- #region 鍏叡灞炴��
public TextComponentType TargetTextType => m_TargetTextType;
- public Component TargetTextComponent => m_TargetTextComponent; // 寮�鏀剧粰 Editor 浣跨敤锛岄伩鍏嶅弽灏�
+ public Component TargetTextComponent => m_TargetTextComponent;
public LanguageConfigDictionary LanguageConfigs => m_LanguageConfigs;
- #endregion
- #region Unity鐢熷懡鍛ㄦ湡
- protected void Awake()
- {
- DetectTargetComponent();
- }
+ private void Awake() => DetectTargetComponent();
- protected void OnEnable()
+ private void OnEnable()
{
- // 淇濊瘉浠呭簲鐢ㄤ竴娆℃帓鐗堜互鑺傜渷鎬ц兘锛岄櫎闈炲湪缂栬緫鍣ㄩ潪杩愯妯″紡涓嬮渶瑕佸己鍒�
if (!Application.isPlaying || !m_IsApplied)
{
string langId = Application.isPlaying ? Language.Id : DefaultLangId;
@@ -211,32 +172,14 @@
}
#if UNITY_EDITOR
- protected void Reset()
+ private void Reset()
{
DetectTargetComponent();
- if (!HasConfig(DefaultLangId))
- {
- ReadCurrentToConfig(DefaultLangId);
- }
+ if (!HasConfig(DefaultLangId)) ReadCurrentToConfig(DefaultLangId);
}
#endif
- #endregion
- #region 鍏叡閰嶇疆鏂规硶
- public LanguageConfigItem GetConfig(string languageId)
- {
- // 1. 灏濊瘯鑾峰彇鐩爣璇█閰嶇疆
- LanguageConfigItem config = m_LanguageConfigs.Get(languageId);
- if (config != null) return config;
-
- // 2. 鎵句笉鍒板垯鍥為��鍒伴粯璁ら厤缃�
- config = m_LanguageConfigs.Get(DefaultLangId);
- if (config != null) return config;
-
- Debug.LogError($"[TextLanguageAdapter] 鎵句笉鍒拌瑷� {languageId} 鐨勯厤缃紝涓斾笉瀛樺湪 default 榛樿閰嶇疆锛�");
- return null;
- }
-
+ public LanguageConfigItem GetConfig(string languageId) => m_LanguageConfigs.Get(languageId);
public void SetConfig(string languageId, LanguageConfigItem config) => m_LanguageConfigs.Set(languageId, config);
public void RemoveConfig(string languageId) => m_LanguageConfigs.Remove(languageId);
public bool HasConfig(string languageId) => m_LanguageConfigs.ContainsKey(languageId);
@@ -244,27 +187,20 @@
public void ApplyConfig(string languageId)
{
- LanguageConfigItem config = GetConfig(languageId);
+ var config = GetConfig(languageId);
if (config == null) return;
- if (TryGetComponent(out RectTransform rectTransform))
- config.ApplyToRectTransform(rectTransform);
-
- if (m_TargetTextComponent is Text textComponent)
- config.ApplyToText(textComponent);
+ config.ApplyToRectTransform(GetComponent<RectTransform>());
+ if (m_TargetTextComponent is Text txt) config.ApplyToText(txt);
m_IsApplied = true;
}
public void ReadCurrentToConfig(string languageId)
{
- LanguageConfigItem config = new LanguageConfigItem();
-
- if (TryGetComponent(out RectTransform rectTransform))
- config.ReadFromRectTransform(rectTransform);
-
- if (m_TargetTextComponent is Text textComponent)
- config.ReadFromText(textComponent);
+ var config = new LanguageConfigItem();
+ config.ReadFromRectTransform(GetComponent<RectTransform>());
+ if (m_TargetTextComponent is Text txt) config.ReadFromText(txt);
SetConfig(languageId, config);
}
@@ -272,14 +208,10 @@
public static string GetLanguageShowName(string languageId)
{
if (Language.languageShowDict != null && Language.languageShowDict.TryGetValue(languageId, out string showName))
- {
return showName;
- }
return languageId;
}
- #endregion
- #region 绉佹湁缁勪欢妫�娴�
private void DetectTargetComponent()
{
if (m_TargetTextComponent != null)
@@ -288,19 +220,11 @@
return;
}
- // 鎸変紭鍏堢骇鏌ユ壘缁勪欢
- m_TargetTextComponent = FindComponent<GradientText>()
- ?? FindComponent<TextEx>()
- ?? (Component)FindComponent<Text>();
+ m_TargetTextComponent = GetComponent<GradientText>() ?? GetComponentInChildren<GradientText>(true) as Component
+ ?? GetComponent<TextEx>() ?? GetComponentInChildren<TextEx>(true) as Component
+ ?? GetComponent<Text>() ?? GetComponentInChildren<Text>(true) as Component;
DetermineTextType(m_TargetTextComponent);
- }
-
- /// <summary> 杈呭姪鏂规硶锛氬湪鑷韩鍜屽瓙鐗╀綋涓煡鎵剧粍浠� </summary>
- private T FindComponent<T>() where T : Component
- {
- T comp = GetComponent<T>();
- return comp != null ? comp : GetComponentInChildren<T>(true);
}
private void DetermineTextType(Component component)
@@ -313,7 +237,6 @@
_ => TextComponentType.None
};
}
- #endregion
#if UNITY_EDITOR
[ContextMenu("鍒锋柊缁勪欢妫�娴�")]
@@ -331,9 +254,6 @@
}
[ContextMenu("搴旂敤榛樿閰嶇疆")]
- public void Editor_ApplyDefaultConfig()
- {
- ApplyConfig(DefaultLangId);
- }
+ public void Editor_ApplyDefaultConfig() => ApplyConfig(DefaultLangId);
#endif
}
\ No newline at end of file
--
Gitblit v1.8.0