三国卡牌客户端基础资源仓库
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
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
 
[CustomEditor(typeof(TextLanguageAdapter), true)]
[CanEditMultipleObjects]
public class TextLanguageAdapterEditor : Editor
{
    private Dictionary<string, bool> m_FoldoutStates = new Dictionary<string, bool>();
 
    private void OnEnable()
    {
        TextLanguageAdapterHelper.Initialize();
        
        if (target is TextLanguageAdapter adapter)
        {
            if (!adapter.HasConfig(TextLanguageAdapter.DefaultLangId))
            {
                adapter.ReadCurrentToConfig(TextLanguageAdapter.DefaultLangId);
                EditorUtility.SetDirty(adapter);
            }
            foreach (var langId in adapter.GetConfiguredLanguages()) m_FoldoutStates.TryAdd(langId, false);
        }
    }
 
    public override void OnInspectorGUI()
    {
        var adapter = target as TextLanguageAdapter;
        
        DrawBasicInfo(adapter);
        EditorGUILayout.Space();
        DrawLanguageConfigs(adapter);
 
        if (GUI.changed) EditorUtility.SetDirty(target);
    }
 
    private void DrawBasicInfo(TextLanguageAdapter adapter)
    {
        using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox))
        {
            EditorGUILayout.LabelField("基本信息", EditorStyles.boldLabel);
            
            EditorGUI.BeginChangeCheck();
            
            // 允许手动拖拽目标组件和设置类型
            Component newTarget = (Component)EditorGUILayout.ObjectField("目标组件", adapter.TargetTextComponent, typeof(Component), true);
            TextComponentType newType = (TextComponentType)EditorGUILayout.EnumPopup("组件类型", adapter.TargetTextType);
            
            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(adapter, "Update Basic Info");
                
                // 智能辅助:当用户拖拽新组件时,尝试自动匹配一下类型,但也允许手动覆盖
                if (newTarget != null && newTarget != adapter.TargetTextComponent)
                {
                    string typeName = newTarget.GetType().Name;
                    if (typeName == "GradientText") newType = TextComponentType.GradientText;
                    else if (typeName == "TextEx") newType = TextComponentType.TextEx;
                    else if (newTarget is UnityEngine.UI.Text) newType = TextComponentType.Text;
                }
 
                adapter.TargetTextComponent = newTarget;
                adapter.TargetTextType = newType;
            }
 
            EditorGUILayout.HelpBox("请手动拖拽要适配的文本组件,并确认组件类型是否正确。", MessageType.Info);
        }
    }
 
    private void DrawLanguageConfigs(TextLanguageAdapter adapter)
    {
        using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox))
        {
            EditorGUILayout.LabelField("语言排版配置", EditorStyles.boldLabel);
            EditorGUILayout.HelpBox("default仅用于编辑时恢复初始状态,无对应语言配置时用组件当前状态", MessageType.Info);
            
            foreach (var langId in adapter.GetConfiguredLanguages()) DrawLanguageConfigItem(adapter, langId);
 
            EditorGUILayout.Space();
            DrawAddLanguageConfig(adapter);
        }
    }
 
    private void DrawLanguageConfigItem(TextLanguageAdapter adapter, string langId)
    {
        m_FoldoutStates.TryAdd(langId, false);
 
        int presetIndex = System.Array.IndexOf(TextLanguageAdapterHelper.PresetLanguageIds, langId);
        string name = presetIndex >= 0 ? TextLanguageAdapterHelper.PresetLanguageNames[presetIndex] : TextLanguageAdapter.GetLanguageShowName(langId);
        string label = string.IsNullOrEmpty(name) ? langId : $"{langId} ({name})";
 
        EditorGUI.indentLevel++;
        if (langId == TextLanguageAdapter.DefaultLangId) GUI.backgroundColor = new Color(1f, 0.9f, 0.7f);
        
        m_FoldoutStates[langId] = EditorGUILayout.Foldout(m_FoldoutStates[langId], label, true);
        GUI.backgroundColor = Color.white;
 
        if (m_FoldoutStates[langId])
        {
            EditorGUI.indentLevel++;
            var config = adapter.LanguageConfigs.Get(langId);
            if (config != null)
            {
                DrawConfigItem(config);
                EditorGUILayout.Space();
                DrawConfigItemActions(adapter, langId);
            }
            EditorGUI.indentLevel--;
        }
        EditorGUI.indentLevel--;
    }
 
    private void DrawConfigItem(LanguageConfigItem cfg)
    {
        EditorGUILayout.LabelField("RectTransform 配置", EditorStyles.miniBoldLabel);
        EditorGUI.BeginChangeCheck();
 
        cfg.anchoredPosition = EditorGUILayout.Vector2Field("Anchored Position", cfg.anchoredPosition);
        cfg.sizeDelta = EditorGUILayout.Vector2Field("Size Delta", cfg.sizeDelta);
        cfg.anchorMin = EditorGUILayout.Vector2Field("Anchor Min", cfg.anchorMin);
        cfg.anchorMax = EditorGUILayout.Vector2Field("Anchor Max", cfg.anchorMax);
        cfg.pivot = EditorGUILayout.Vector2Field("Pivot", cfg.pivot);
        cfg.localScale = EditorGUILayout.Vector3Field("Local Scale", cfg.localScale);
        cfg.localRotation = EditorGUILayout.Vector3Field("Local Rotation", cfg.localRotation);
        
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("文本排版与适配配置", EditorStyles.miniBoldLabel);
        cfg.font = (Font)EditorGUILayout.ObjectField("Font", cfg.font, typeof(Font), false);
        cfg.fontStyle = (FontStyle)EditorGUILayout.EnumPopup("Font Style", cfg.fontStyle);
        cfg.fontSize = EditorGUILayout.IntField("Font Size", cfg.fontSize);
        cfg.lineSpacing = EditorGUILayout.FloatField("Line Spacing", cfg.lineSpacing);
        cfg.alignment = (TextAnchor)EditorGUILayout.EnumPopup("Alignment", cfg.alignment);
        cfg.alignByGeometry = EditorGUILayout.Toggle("Align By Geometry", cfg.alignByGeometry);
        cfg.horizontalOverflow = (HorizontalWrapMode)EditorGUILayout.EnumPopup("Horizontal Overflow", cfg.horizontalOverflow);
        cfg.verticalOverflow = (VerticalWrapMode)EditorGUILayout.EnumPopup("Vertical Overflow", cfg.verticalOverflow);
        cfg.resizeTextForBestFit = EditorGUILayout.Toggle("Best Fit", cfg.resizeTextForBestFit);
 
        if (cfg.resizeTextForBestFit)
        {
            EditorGUI.indentLevel++;
            cfg.resizeTextMinSize = EditorGUILayout.IntField("Min Size", cfg.resizeTextMinSize);
            cfg.resizeTextMaxSize = EditorGUILayout.IntField("Max Size", cfg.resizeTextMaxSize);
            EditorGUI.indentLevel--;
        }
 
        if (EditorGUI.EndChangeCheck()) EditorUtility.SetDirty(target);
    }
 
    private void DrawConfigItemActions(TextLanguageAdapter adapter, string langId)
    {
        using (new EditorGUILayout.HorizontalScope())
        {
            GUILayout.Space(EditorGUI.indentLevel * 15f); 
 
            if (GUILayout.Button("从当前读取", GUILayout.Width(80)))
            {
                Undo.RecordObject(adapter, "Read Current Config");
                adapter.ReadCurrentToConfig(langId);
            }
 
            if (GUILayout.Button("应用", GUILayout.Width(60)))
            {
                Undo.RecordObject(adapter, "Apply Config");
                adapter.ApplyConfig(langId);
            }
 
            using (new EditorGUI.DisabledScope(langId == TextLanguageAdapter.DefaultLangId))
            {
                if (GUILayout.Button("删除", GUILayout.Width(50)))
                {
                    Undo.RecordObject(adapter, "Remove Config");
                    adapter.RemoveConfig(langId);
                    m_FoldoutStates.Remove(langId);
                }
            }
        }
    }
 
    private void DrawAddLanguageConfig(TextLanguageAdapter adapter)
    {
        EditorGUILayout.BeginHorizontal();
        GUILayout.Space(15f);
        EditorGUILayout.LabelField("添加预设语言:", GUILayout.Width(100));
 
        float viewWidth = EditorGUIUtility.currentViewWidth - 30f;
        float currentWidth = 115f; 
        float buttonWidth = 64f;   
 
        for (int i = 0; i < TextLanguageAdapterHelper.PresetLanguageIds.Length; i++)
        {
            string langId = TextLanguageAdapterHelper.PresetLanguageIds[i];
            if (!adapter.HasConfig(langId))
            {
                if (currentWidth + buttonWidth > viewWidth)
                {
                    EditorGUILayout.EndHorizontal();
                    EditorGUILayout.BeginHorizontal();
                    GUILayout.Space(115f);
                    currentWidth = 115f;
                }
 
                if (GUILayout.Button(TextLanguageAdapterHelper.PresetLanguageNames[i], GUILayout.Width(60)))
                {
                    Undo.RecordObject(adapter, "Add Language Config");
                    var newConfig = adapter.HasConfig(TextLanguageAdapter.DefaultLangId)
                        ? adapter.LanguageConfigs.Get(TextLanguageAdapter.DefaultLangId).Clone()
                        : new LanguageConfigItem();
                    adapter.SetConfig(langId, newConfig);
                    m_FoldoutStates[langId] = true;
                }
                currentWidth += buttonWidth;
            }
        }
        EditorGUILayout.EndHorizontal();
    }
}