From 555a6337bc48bf863a18b506f6c794a3f0bfd95a Mon Sep 17 00:00:00 2001
From: lcy <1459594991@qq.com>
Date: 星期五, 15 五月 2026 17:18:21 +0800
Subject: [PATCH] 592 多语言适配 实现CommonLanguageAdapter
---
Main/Component/UI/Core/CommonLanguageAdapter.cs.meta | 11 ++
Main/Component/UI/Core/CommonLanguageAdapter.cs | 195 ++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 206 insertions(+), 0 deletions(-)
diff --git a/Main/Component/UI/Core/CommonLanguageAdapter.cs b/Main/Component/UI/Core/CommonLanguageAdapter.cs
new file mode 100644
index 0000000..a51a1bf
--- /dev/null
+++ b/Main/Component/UI/Core/CommonLanguageAdapter.cs
@@ -0,0 +1,195 @@
+using System;
+using System.Collections.Generic;
+using UnityEngine;
+
+/// <summary>
+/// 鍗曚釜璇█鐨勯�氱敤鎺掔増閫傞厤鏁版嵁鑺傜偣 (浠呭寘鍚� RectTransform 鍜� enabled)
+/// </summary>
+[Serializable]
+public class CommonLanguageConfigItem
+{
+ [Header("RectTransform 閰嶇疆")]
+ public Vector2 anchoredPosition = Vector2.zero;
+ public Vector2 sizeDelta = new Vector2(100f, 100f);
+ public Vector2 anchorMin = new Vector2(0.5f, 0.5f);
+ public Vector2 anchorMax = new Vector2(0.5f, 0.5f);
+ public Vector2 pivot = new Vector2(0.5f, 0.5f);
+ public Vector3 localScale = Vector3.one;
+ public Vector3 localRotation = Vector3.zero;
+
+ [Header("鍩虹閰嶇疆")]
+ public bool enabled = true;
+
+ public void ApplyToRectTransform(RectTransform rt)
+ {
+ 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);
+
+ // 搴旂敤 enabled 鐘舵�佸埌 GameObject
+ if (rt.gameObject.activeSelf != enabled)
+ {
+ rt.gameObject.SetActive(enabled);
+ }
+ }
+
+ public void ReadFromRectTransform(RectTransform rt)
+ {
+ 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;
+
+ // 璇诲彇褰撳墠 GameObject 鐨� active 鐘舵��
+ enabled = rt.gameObject.activeSelf;
+ }
+
+ public CommonLanguageConfigItem Clone() => (CommonLanguageConfigItem)MemberwiseClone();
+}
+
+/// <summary>
+/// 鏀寔 Unity 搴忓垪鍖栫殑瀛楀吀
+/// </summary>
+[Serializable]
+public class CommonLanguageConfigDictionary
+{
+ public List<string> keys = new List<string>();
+ public List<CommonLanguageConfigItem> values = new List<CommonLanguageConfigItem>();
+
+ public CommonLanguageConfigItem Get(string key)
+ {
+ if (string.IsNullOrEmpty(key)) return null;
+ int index = keys.IndexOf(key);
+ return index >= 0 ? values[index] : null;
+ }
+
+ public void Set(string key, CommonLanguageConfigItem value)
+ {
+ int index = keys.IndexOf(key);
+ if (index >= 0) values[index] = value;
+ else
+ {
+ keys.Add(key);
+ values.Add(value);
+ }
+ }
+
+ public bool ContainsKey(string key) => keys.Contains(key);
+
+ public void Remove(string key)
+ {
+ int index = keys.IndexOf(key);
+ if (index >= 0)
+ {
+ keys.RemoveAt(index);
+ values.RemoveAt(index);
+ }
+ }
+}
+
+/// <summary>
+/// 澶氳瑷�閫氱敤鎺掔増閫傞厤鍣�
+/// </summary>
+[RequireComponent(typeof(RectTransform))]
+public class CommonLanguageAdapter : MonoBehaviour
+{
+ public const string DefaultLangId = "default";
+
+ [SerializeField, Tooltip("璇█閰嶇疆瀛楀吀")]
+ private CommonLanguageConfigDictionary m_LanguageConfigs = new CommonLanguageConfigDictionary();
+
+ [SerializeField, Tooltip("鐩爣 RectTransform 寮曠敤 (鐣欑┖榛樿涓鸿嚜韬�)")]
+ private RectTransform m_TargetRectTransform;
+
+ private bool m_IsApplied = false;
+
+ public RectTransform TargetRectTransform
+ {
+ get => m_TargetRectTransform;
+ set => m_TargetRectTransform = value;
+ }
+ public CommonLanguageConfigDictionary LanguageConfigs => m_LanguageConfigs;
+
+ private void Awake() => DetectTargetComponent();
+
+ private void OnEnable()
+ {
+ if (!Application.isPlaying || !m_IsApplied)
+ {
+ // 鑾峰彇褰撳墠璇█鐜锛岃繖閲屽吋瀹规偍鐨勮瑷�绯荤粺閫昏緫
+ string langId = Application.isPlaying ? (Language.Id ?? DefaultLangId) : DefaultLangId;
+ ApplyConfig(langId);
+ }
+ }
+
+#if UNITY_EDITOR
+ private void Reset()
+ {
+ DetectTargetComponent();
+ if (!HasConfig(DefaultLangId)) ReadCurrentToConfig(DefaultLangId);
+ }
+#endif
+
+ public CommonLanguageConfigItem GetConfig(string languageId) => m_LanguageConfigs.Get(languageId);
+ public void SetConfig(string languageId, CommonLanguageConfigItem config) => m_LanguageConfigs.Set(languageId, config);
+ public void RemoveConfig(string languageId) => m_LanguageConfigs.Remove(languageId);
+ public bool HasConfig(string languageId) => m_LanguageConfigs.ContainsKey(languageId);
+ public List<string> GetConfiguredLanguages() => new List<string>(m_LanguageConfigs.keys);
+
+ public void ApplyConfig(string languageId)
+ {
+ var config = GetConfig(languageId);
+ if (config == null) return;
+
+ // 搴旂敤閰嶇疆鍒扮洰鏍囩粍浠讹紙榛樿涓鸿嚜韬殑 RectTransform锛�
+ RectTransform targetRt = m_TargetRectTransform != null ? m_TargetRectTransform : GetComponent<RectTransform>();
+ config.ApplyToRectTransform(targetRt);
+
+ m_IsApplied = true;
+ }
+
+ public void ReadCurrentToConfig(string languageId)
+ {
+ var config = new CommonLanguageConfigItem();
+ RectTransform targetRt = m_TargetRectTransform != null ? m_TargetRectTransform : GetComponent<RectTransform>();
+ config.ReadFromRectTransform(targetRt);
+
+ SetConfig(languageId, config);
+ }
+
+ public static string GetLanguageShowName(string languageId)
+ {
+ if (Language.languageShowDict != null && Language.languageShowDict.TryGetValue(languageId, out string showName))
+ return showName;
+ return languageId;
+ }
+
+ private void DetectTargetComponent()
+ {
+ if (m_TargetRectTransform == null)
+ {
+ m_TargetRectTransform = GetComponent<RectTransform>();
+ }
+ }
+
+#if UNITY_EDITOR
+ [ContextMenu("璇诲彇褰撳墠閰嶇疆")]
+ public void Editor_ReadCurrentConfig()
+ {
+ ReadCurrentToConfig(DefaultLangId);
+ UnityEditor.EditorUtility.SetDirty(this);
+ }
+
+ [ContextMenu("搴旂敤榛樿閰嶇疆")]
+ public void Editor_ApplyDefaultConfig() => ApplyConfig(DefaultLangId);
+#endif
+}
\ No newline at end of file
diff --git a/Main/Component/UI/Core/CommonLanguageAdapter.cs.meta b/Main/Component/UI/Core/CommonLanguageAdapter.cs.meta
new file mode 100644
index 0000000..261b64b
--- /dev/null
+++ b/Main/Component/UI/Core/CommonLanguageAdapter.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: bd911ec9f084cdb48abaf923104bcd9c
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
--
Gitblit v1.8.0