lcy
4 天以前 8e31a795cd6e271d085c2765dd85e8d72f2b27a5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
/// <summary>文本组件类型枚举</summary>
public enum TextComponentType
{
    None = 0,
    Text = 1,
    TextEx = 2,
    GradientText = 3
}
 
/// <summary>单个语言的排版和适配数据节点</summary>
[Serializable]
public class LanguageConfigItem
{
    [Header("RectTransform 配置")]
    public Vector2 anchoredPosition = Vector2.zero;
    public Vector2 sizeDelta = new Vector2(100f, 30f);
    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("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;
 
    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);
    }
 
    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;
    }
 
    public void ApplyToText(Text txt)
    {
        if (txt == null) return;
        if (font != null) txt.font = font;
        
        txt.fontStyle = fontStyle;
        txt.fontSize = fontSize;
        txt.lineSpacing = lineSpacing;
        txt.horizontalOverflow = horizontalOverflow;
        txt.verticalOverflow = verticalOverflow;
        txt.resizeTextForBestFit = resizeTextForBestFit;
        if (resizeTextForBestFit)
        {
            txt.resizeTextMinSize = resizeTextMinSize;
            txt.resizeTextMaxSize = resizeTextMaxSize;
        }
        txt.alignment = alignment;
        txt.alignByGeometry = alignByGeometry;
    }
 
    public void ReadFromText(Text txt)
    {
        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;
    }
 
    public LanguageConfigItem Clone() => (LanguageConfigItem)MemberwiseClone();
}
 
/// <summary>支持 Unity 序列化的字典</summary>
[Serializable]
public class LanguageConfigDictionary
{
    public List<string> keys = new List<string>();
    public List<LanguageConfigItem> values = new List<LanguageConfigItem>();
 
    public LanguageConfigItem 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, LanguageConfigItem 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 TextLanguageAdapter : MonoBehaviour
{
    public const string DefaultLangId = "default";
 
    [SerializeField, Tooltip("语言配置字典")] 
    private LanguageConfigDictionary m_LanguageConfigs = new LanguageConfigDictionary();
    
    [SerializeField, Tooltip("目标文本组件类型")] 
    private TextComponentType m_TargetTextType = TextComponentType.None;
    
    [SerializeField, Tooltip("关联的文本组件引用")] 
    private Component m_TargetTextComponent;
 
    private bool m_IsApplied = false;
 
    public TextComponentType TargetTextType => m_TargetTextType;
    public Component TargetTextComponent => m_TargetTextComponent;
    public LanguageConfigDictionary LanguageConfigs => m_LanguageConfigs;
 
    private void Awake() => DetectTargetComponent();
 
    private void OnEnable()
    {
        if (!Application.isPlaying || !m_IsApplied)
        {
            string langId = Application.isPlaying ? Language.Id : DefaultLangId;
            ApplyConfig(langId);
        }
    }
 
#if UNITY_EDITOR
    private void Reset()
    {
        DetectTargetComponent();
        if (!HasConfig(DefaultLangId)) ReadCurrentToConfig(DefaultLangId);
    }
#endif
 
    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);
    public List<string> GetConfiguredLanguages() => new List<string>(m_LanguageConfigs.keys);
 
    public void ApplyConfig(string languageId)
    {
        var config = GetConfig(languageId);
        if (config == null) return;
 
        config.ApplyToRectTransform(GetComponent<RectTransform>());
        if (m_TargetTextComponent is Text txt) config.ApplyToText(txt);
 
        m_IsApplied = true;
    }
 
    public void ReadCurrentToConfig(string languageId)
    {
        var config = new LanguageConfigItem();
        config.ReadFromRectTransform(GetComponent<RectTransform>());
        if (m_TargetTextComponent is Text txt) config.ReadFromText(txt);
 
        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_TargetTextComponent != null)
        {
            DetermineTextType(m_TargetTextComponent);
            return;
        }
 
        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);
    }
 
    private void DetermineTextType(Component component)
    {
        m_TargetTextType = component switch
        {
            GradientText _ => TextComponentType.GradientText,
            TextEx _ => TextComponentType.TextEx,
            Text _ => TextComponentType.Text,
            _ => TextComponentType.None
        };
    }
 
#if UNITY_EDITOR
    [ContextMenu("刷新组件检测")]
    public void Editor_ForceRefreshDetection()
    {
        DetectTargetComponent();
        UnityEditor.EditorUtility.SetDirty(this);
    }
 
    [ContextMenu("读取当前配置")]
    public void Editor_ReadCurrentConfig()
    {
        ReadCurrentToConfig(DefaultLangId);
        UnityEditor.EditorUtility.SetDirty(this);
    }
 
    [ContextMenu("应用默认配置")]
    public void Editor_ApplyDefaultConfig() => ApplyConfig(DefaultLangId);
#endif
}