三国卡牌客户端基础资源仓库
lcy
2026-04-02 033e950887264eddd11c32c21d4126d83e98a659
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
 
// ======================== 扫描数据模型 ========================
 
public class ScanResultItem
{
    public string PrefabPath { get; }
    public string GameObjectPath { get; }
    public List<string> MissingLanguages { get; set; } = new List<string>();
    public TextComponentType ComponentType { get; }
    public string PrefabGUID { get; }
 
    public ScanResultItem(string path, string goPath, TextComponentType type, string guid)
    {
        PrefabPath = path; 
        GameObjectPath = goPath; 
        ComponentType = type; 
        PrefabGUID = guid;
    }
    
    public string GetDisplayName() => $"{GameObjectPath} (缺少: {string.Join(", ", MissingLanguages)}) [{ComponentType}]";
}
 
public class PrefabScanResult
{
    public string PrefabPath { get; }
    public string PrefabGUID { get; }
    public List<ScanResultItem> Items { get; } = new List<ScanResultItem>();
 
    public PrefabScanResult(string path, string guid) 
    { 
        PrefabPath = path; 
        PrefabGUID = guid; 
    }
}
 
public class ScanResultSummary
{
    public string ScanDirectory { get; }
    public int TotalPrefabsScanned { get; set; }
    public int TotalAdaptersFound { get; private set; }
    public int AdaptersWithMissingConfig { get; private set; }
    public List<PrefabScanResult> PrefabResults { get; } = new List<PrefabScanResult>();
 
    public ScanResultSummary(string dir) => ScanDirectory = dir;
 
    public void AddResult(ScanResultItem item)
    {
        var prefabResult = PrefabResults.Find(p => p.PrefabPath == item.PrefabPath);
        if (prefabResult == null)
        {
            prefabResult = new PrefabScanResult(item.PrefabPath, item.PrefabGUID);
            PrefabResults.Add(prefabResult);
        }
        prefabResult.Items.Add(item);
        TotalAdaptersFound++;
        AdaptersWithMissingConfig++;
    }
}
 
// ======================== TreeView 实现 ========================
 
public class MetadataTreeViewItem : TreeViewItem
{
    public object Metadata { get; }
    public MetadataTreeViewItem(int id, string name, object meta) : base(id, 0, name) => Metadata = meta;
}
 
public class ScanResultTreeView : TreeView
{
    private ScanResultSummary m_Summary;
 
    public ScanResultTreeView(TreeViewState state, ScanResultSummary summary) : base(state)
    {
        m_Summary = summary;
        Reload();
        ExpandAll();
    }
 
    protected override TreeViewItem BuildRoot()
    {
        var root = new TreeViewItem { id = 0, depth = -1, displayName = "Root" };
        
        // 【关键修复 1】:必须初始化 children 列表。
        // 否则当扫描结果完美(0个错误)时,root.children 为 null 会导致 Unity 报错
        root.children = new List<TreeViewItem>();
 
        if (m_Summary == null || m_Summary.PrefabResults.Count == 0) 
            return root;
 
        int itemId = 1;
        foreach (var prefabResult in m_Summary.PrefabResults)
        {
            string prefabName = Path.GetFileNameWithoutExtension(prefabResult.PrefabPath);
            var prefabItem = new MetadataTreeViewItem(itemId++, $"{prefabName} ({prefabResult.Items.Count}个问题)", prefabResult);
 
            foreach (var adapterItem in prefabResult.Items)
            {
                var adapterTreeItem = new MetadataTreeViewItem(itemId++, adapterItem.GetDisplayName(), adapterItem);
                prefabItem.AddChild(adapterTreeItem);
            }
            root.AddChild(prefabItem);
        }
 
        // 【关键修复 2】:Unity 官方规范要求,手动使用 AddChild 构建树之后,必须调用此方法刷新深度和层级关系
        SetupDepthsFromParentsAndChildren(root);
 
        return root;
    }
 
    protected override void RowGUI(RowGUIArgs args)
    {
        var item = args.item as MetadataTreeViewItem;
        if (item != null && item.Metadata is PrefabScanResult)
            GUI.Label(args.rowRect, item.displayName, EditorStyles.boldLabel);
        else if (item != null && item.Metadata is ScanResultItem adapterItem)
            GUI.Label(args.rowRect, $"{adapterItem.GameObjectPath} (缺少: {string.Join(", ", adapterItem.MissingLanguages)})");
        else
            base.RowGUI(args);
    }
 
    protected override void DoubleClickedItem(int id)
    {
        var item = FindItem(id, rootItem) as MetadataTreeViewItem;
        if (item == null) return;
 
        if (item.Metadata is ScanResultItem adapterItem)
            PingGameObject(adapterItem.PrefabPath, adapterItem.GameObjectPath);
        else if (item.Metadata is PrefabScanResult prefabResult)
        {
            var obj = AssetDatabase.LoadAssetAtPath<GameObject>(prefabResult.PrefabPath);
            if (obj != null) 
            { 
                Selection.activeObject = obj; 
                EditorGUIUtility.PingObject(obj); 
            }
        }
    }
 
    private void PingGameObject(string prefabPath, string gameObjectPath)
    {
        var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
        if (prefab == null) return;
 
        Transform target = prefab.transform.Find(gameObjectPath);
        if (target != null)
        {
            Selection.activeObject = target.gameObject;
            EditorGUIUtility.PingObject(target.gameObject);
        }
        else
        {
            Selection.activeObject = prefab;
            EditorGUIUtility.PingObject(prefab);
            Debug.LogWarning($"[ScanTool] 找不到路径 '{gameObjectPath}',已选中整个预制体");
        }
    }
}
 
// ======================== 扫描工具主窗口 ========================
 
public class TextLanguageAdapterScanTool : EditorWindow
{
    private string m_ScanDirectory = "Assets";
    private Vector2 m_ScrollPosition;
    private ScanResultSummary m_ScanResult;
    private bool m_IsScanning;
    private float m_ScanProgress;
    private string m_ScanStatus;
    
    private ScanResultTreeView m_TreeView;
    private TreeViewState m_TreeViewState;
 
    // 批量操作的UI状态
    private int m_SourceLangIndex = 0;
    private int m_TargetLangIndex = 0;
    private bool m_OverwriteExisting = false;
 
    [MenuItem("程序/TextLanguageAdapter扫描与管理工具")]
    public static void ShowWindow()
    {
        var window = GetWindow<TextLanguageAdapterScanTool>("语言适配器扫描");
        window.minSize = new Vector2(600f, 500f);
    }
 
    private void OnEnable() => TextLanguageAdapterHelper.Initialize();
 
    private void OnGUI()
    {
        DrawHeader();
        EditorGUILayout.Space(5f);
        DrawScanSettings();
        EditorGUILayout.Space(5f);
        DrawScanButton();
        EditorGUILayout.Space(5f);
        DrawResults();
        EditorGUILayout.Space(5f);
        DrawBatchOperations();
    }
 
    private void DrawHeader()
    {
        using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox))
        {
            EditorGUILayout.LabelField("TextLanguageAdapter 配置缺失扫描与批量操作工具", EditorStyles.boldLabel);
            EditorGUILayout.HelpBox("扫描指定目录下所有预制体,检测组件的语言配置是否完整,或执行批量语言配置复制。", MessageType.Info);
        }
    }
 
    private void DrawScanSettings()
    {
        using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox))
        {
            EditorGUILayout.LabelField("扫描设置", EditorStyles.boldLabel);
 
            using (new EditorGUILayout.HorizontalScope())
            {
                EditorGUILayout.LabelField("目标目录:", GUILayout.Width(80f));
                m_ScanDirectory = EditorGUILayout.TextField(m_ScanDirectory);
                if (GUILayout.Button("选择...", GUILayout.Width(70f)))
                {
                    string path = EditorUtility.OpenFolderPanel("选择扫描目录", Application.dataPath, "");
                    if (!string.IsNullOrEmpty(path))
                        m_ScanDirectory = path.StartsWith(Application.dataPath) ? "Assets" + path.Substring(Application.dataPath.Length) : path;
                }
            }
 
            EditorGUILayout.Space(5f);
            EditorGUILayout.LabelField($"预设语言:");
            
            EditorGUILayout.BeginHorizontal();
            GUILayout.Space(15f);
            int displayCount = 0;
            foreach (var langId in TextLanguageAdapterHelper.PresetLanguageIds)
            {
                if (langId == TextLanguageAdapter.DefaultLangId) continue;
                EditorGUILayout.LabelField(langId, GUILayout.Width(50f));
                if (++displayCount % 8 == 0)
                {
                    EditorGUILayout.EndHorizontal();
                    EditorGUILayout.BeginHorizontal();
                    GUILayout.Space(15f);
                }
            }
            EditorGUILayout.EndHorizontal();
        }
    }
 
    private void DrawScanButton()
    {
        using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox))
        {
            using (new EditorGUILayout.HorizontalScope())
            {
                using (new EditorGUI.DisabledScope(m_IsScanning || string.IsNullOrEmpty(m_ScanDirectory)))
                {
                    if (GUILayout.Button("开始扫描", GUILayout.Height(30f))) StartScan();
                }
 
                if (m_IsScanning)
                {
                    EditorGUILayout.LabelField("扫描中...", GUILayout.Width(100f));
                    m_ScanProgress = EditorGUILayout.Slider(m_ScanProgress, 0f, 1f);
                    Repaint();
                }
            }
            if (!string.IsNullOrEmpty(m_ScanStatus)) EditorGUILayout.LabelField(m_ScanStatus);
        }
    }
 
    private void DrawResults()
    {
        using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox))
        {
            EditorGUILayout.LabelField("扫描结果", EditorStyles.boldLabel);
 
            if (m_ScanResult != null)
            {
                EditorGUILayout.LabelField($"预制体总数: {m_ScanResult.TotalPrefabsScanned} | Adapter总数: {m_ScanResult.TotalAdaptersFound} | 缺失配置: {m_ScanResult.AdaptersWithMissingConfig}");
                EditorGUILayout.Space(5f);
 
                if (m_TreeView != null)
                {
                    m_ScrollPosition = EditorGUILayout.BeginScrollView(m_ScrollPosition, GUILayout.MinHeight(150f));
                    var rect = EditorGUILayout.GetControlRect(false, m_TreeView.totalHeight);
                    m_TreeView.OnGUI(rect);
                    EditorGUILayout.EndScrollView();
                }
 
                EditorGUILayout.Space(5f);
                using (new EditorGUILayout.HorizontalScope())
                {
                    if (GUILayout.Button("展开全部")) m_TreeView?.ExpandAll();
                    if (GUILayout.Button("折叠全部")) m_TreeView?.CollapseAll();
                }
            }
            else
            {
                EditorGUILayout.HelpBox("点击「开始扫描」按钮进行扫描", MessageType.None);
            }
        }
    }
 
    // ======================== 新增:批量操作功能 ========================
    private void DrawBatchOperations()
    {
        using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox))
        {
            EditorGUILayout.LabelField("批量操作 (针对目标目录下的所有预制体上的TextLanguageAdapter组件)", EditorStyles.boldLabel);
 
            if (TextLanguageAdapterHelper.PresetLanguageIds == null || TextLanguageAdapterHelper.PresetLanguageIds.Length == 0)
                return;
 
            using (new EditorGUILayout.HorizontalScope())
            {
                EditorGUILayout.LabelField("旧语言:", GUILayout.Width(60f));
                m_SourceLangIndex = EditorGUILayout.Popup(m_SourceLangIndex, TextLanguageAdapterHelper.PresetLanguageNames);
                
                GUILayout.Space(20f);
 
                EditorGUILayout.LabelField("新语言:", GUILayout.Width(60f));
                m_TargetLangIndex = EditorGUILayout.Popup(m_TargetLangIndex, TextLanguageAdapterHelper.PresetLanguageNames);
            }
 
            m_OverwriteExisting = EditorGUILayout.Toggle("覆盖已存在的目标配置", m_OverwriteExisting);
 
            EditorGUILayout.Space(5f);
 
            bool isSameLanguage = m_SourceLangIndex == m_TargetLangIndex;
            using (new EditorGUI.DisabledScope(isSameLanguage || m_IsScanning || string.IsNullOrEmpty(m_ScanDirectory)))
            {
                if (GUILayout.Button("批量复制配置", GUILayout.Height(30f)))
                {
                    string sourceLang = TextLanguageAdapterHelper.PresetLanguageIds[m_SourceLangIndex];
                    string targetLang = TextLanguageAdapterHelper.PresetLanguageIds[m_TargetLangIndex];
 
                    if (EditorUtility.DisplayDialog("高危操作确认",
                        $"此操作将遍历【{m_ScanDirectory}】下所有预制体。\n\n" +
                        $"把它们的 [{sourceLang}] 配置复制并应用到 [{targetLang}] 配置上。\n\n" +
                        $"此操作不可撤销!建议提前使用 Git/SVN 提交代码。\n确定要继续吗?",
                        "确定执行", "取消"))
                    {
                        ExecuteBatchCopy(sourceLang, targetLang);
                    }
                }
            }
 
            if (isSameLanguage)
            {
                EditorGUILayout.HelpBox("旧语言与新语言不能相同", MessageType.Warning);
            }
        }
    }
 
    private void ExecuteBatchCopy(string sourceLang, string targetLang)
    {
        string[] prefabGuids = AssetDatabase.FindAssets("t:Prefab", new[] { m_ScanDirectory });
        if (prefabGuids.Length == 0) return;
 
        int modifiedPrefabCount = 0;
        int modifiedAdapterCount = 0;
 
        try
        {
            for (int i = 0; i < prefabGuids.Length; i++)
            {
                string path = AssetDatabase.GUIDToAssetPath(prefabGuids[i]);
                EditorUtility.DisplayProgressBar("批量复制配置", $"处理中 ({i + 1}/{prefabGuids.Length}): {path}", (float)i / prefabGuids.Length);
 
                // 【核心修改】:直接加载资产内存,不使用 LoadPrefabContents 进行实例化
                GameObject prefabAsset = AssetDatabase.LoadAssetAtPath<GameObject>(path);
                bool isModified = false;
 
                foreach (var adapter in prefabAsset.GetComponentsInChildren<TextLanguageAdapter>(true))
                {
                    if (adapter.HasConfig(sourceLang))
                    {
                        if (m_OverwriteExisting || !adapter.HasConfig(targetLang))
                        {
                            // 必须标记对象为脏,否则 Unity 不会把资产的修改存盘
                            Undo.RecordObject(adapter, "Batch Copy Language Config");
                            
                            var clonedConfig = adapter.GetConfig(sourceLang).Clone();
                            adapter.SetConfig(targetLang, clonedConfig);
                            
                            EditorUtility.SetDirty(adapter);
                            isModified = true;
                            modifiedAdapterCount++;
                        }
                    }
                }
 
                if (isModified)
                {
                    modifiedPrefabCount++;
                }
            }
        }
        finally
        {
            EditorUtility.ClearProgressBar();
            // 统一保存所有标记为 Dirty 的资产修改
            AssetDatabase.SaveAssets();
            
            PerformScan();
            
            EditorUtility.DisplayDialog("批量操作完成", 
                $"批量复制结束!\n\n修改的预制体数量: {modifiedPrefabCount} 个\n更新的适配器配置数量: {modifiedAdapterCount} 个", 
                "确认");
        }
    }
 
    // ======================== 核心扫描逻辑 ========================
 
    private void StartScan()
    {
        m_IsScanning = true;
        m_ScanProgress = 0f;
        m_ScanStatus = "准备扫描...";
 
        EditorApplication.CallbackFunction updateCallback = null;
        updateCallback = () =>
        {
            if (!m_IsScanning) 
            { 
                EditorApplication.update -= updateCallback; 
                return; 
            }
            PerformScan();
            m_IsScanning = false;
            EditorApplication.update -= updateCallback;
        };
        EditorApplication.update += updateCallback;
    }
 
    private void PerformScan()
    {
        m_ScanResult = new ScanResultSummary(m_ScanDirectory);
        string[] prefabGuids = AssetDatabase.FindAssets("t:Prefab", new[] { m_ScanDirectory });
        m_ScanResult.TotalPrefabsScanned = prefabGuids.Length;
 
        if (prefabGuids.Length == 0)
        {
            m_ScanStatus = "未找到任何预制体";
            return;
        }
 
        for (int i = 0; i < prefabGuids.Length; i++)
        {
            string path = AssetDatabase.GUIDToAssetPath(prefabGuids[i]);
            ScanPrefab(path, prefabGuids[i]);
            
            m_ScanProgress = (float)(i + 1) / prefabGuids.Length;
            m_ScanStatus = $"正在扫描: {Path.GetFileName(path)} ({i + 1}/{prefabGuids.Length})";
            
            if (i % 10 == 0) Repaint();
        }
 
        m_TreeViewState ??= new TreeViewState();
        m_TreeView = new ScanResultTreeView(m_TreeViewState, m_ScanResult);
        m_ScanStatus = $"扫描完成! 发现 {m_ScanResult.AdaptersWithMissingConfig} 个缺失配置";
        Repaint();
    }
 
    private void ScanPrefab(string path, string guid)
    {
        GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
        if (prefab == null) return;
 
        foreach (var adapter in prefab.GetComponentsInChildren<TextLanguageAdapter>(true))
        {
            List<string> missing = new List<string>();
            foreach (var langId in TextLanguageAdapterHelper.PresetLanguageIds)
            {
                if (langId == TextLanguageAdapter.DefaultLangId) continue;
                if (!adapter.HasConfig(langId)) missing.Add(langId);
            }
 
            if (missing.Count > 0)
            {
                var item = new ScanResultItem(path, GetGameObjectPath(adapter.gameObject, prefab), adapter.TargetTextType, guid)
                {
                    MissingLanguages = missing
                };
                m_ScanResult.AddResult(item);
            }
        }
    }
 
    private string GetGameObjectPath(GameObject go, GameObject root)
    {
        var parts = new List<string>();
        Transform curr = go.transform;
        while (curr != null && curr != root.transform)
        {
            parts.Insert(0, curr.name);
            curr = curr.parent;
        }
        return string.Join("/", parts);
    }
}