| New file |
| | |
| | | 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 |
| | | } |