少年修仙传客户端基础资源
dabaoji
2023-11-13 0a23e1b83f8fda8ab9f9e32fe71c26c431faf63a
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using UnityEngine.UI;
using System;
using System.Text.RegularExpressions;
using System.Reflection;
 
public class CheckFontSwitch
{
    static string windowRootPath = Application.dataPath + "/ResourcesOut/UI/Window";
    static string windowRootPath2 = Application.dataPath + "/ResourcesOut/UI/PriorityWindow";
    static string prefabRootPath = Application.dataPath + "/ResourcesOut/UI/Prefab";
 
    public static void CheckAndReplaceFontSwitch()
    {
        try
        {
            var allFiles = new List<FileInfo>();
            allFiles.AddRange(new DirectoryInfo(windowRootPath).GetFiles("*.prefab", SearchOption.TopDirectoryOnly));
            allFiles.AddRange(new DirectoryInfo(windowRootPath2).GetFiles("*.prefab", SearchOption.TopDirectoryOnly));
            allFiles.AddRange(new DirectoryInfo(prefabRootPath).GetFiles("*.prefab", SearchOption.TopDirectoryOnly));
 
            var count = allFiles.Count;
            var index = 0;
 
            foreach (var file in allFiles)
            {
                var assetPath = file.FullName.Replace("\\", "/").Replace(Application.dataPath, "Assets");
                var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
 
                var texts = prefab.GetComponentsInChildren<Text>(true);
                foreach (var text in texts)
                {
                    if (text.font != null)
                    {
                        if (text.font.name == FontUtility.preferred.name)
                        {
                            var fontSwitch = text.gameObject.AddMissingComponent<FontSwitch>();
                            fontSwitch.fontType = FontSwitch.FontType.Preferred;
                            text.font = null;
                        }
                        else if (text.font.name == FontUtility.secondary.name)
                        {
                            var fontSwitch = text.gameObject.AddMissingComponent<FontSwitch>();
                            fontSwitch.fontType = FontSwitch.FontType.Secondary;
                            text.font = null;
                        }
                    }
                }
 
                index++;
                EditorUtility.SetDirty(prefab);
                EditorUtility.DisplayProgressBar("检测和替换FontSwitch", StringUtility.Contact("正在替换:", prefab.name), (float)index / count);
            }
 
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
 
            EditorUtility.ClearProgressBar();
        }
        catch (Exception ex)
        {
            Debug.Log(ex);
        }
 
    }
 
    static List<string> chineseCharactorErrors = new List<string>();
    public static void FindChineseCharacter()
    {
        try
        {
            chineseCharactorErrors.Clear();
            chineseCharactorErrors.Add(StringUtility.Contact("序号", "\t", "预置体", "\t", "组件名", "\t", "内容", "\t", "是否需要整改"));
            var allFiles = new List<FileInfo>();
            allFiles.AddRange(new DirectoryInfo(windowRootPath).GetFiles("*.prefab", SearchOption.TopDirectoryOnly));
            allFiles.AddRange(new DirectoryInfo(windowRootPath2).GetFiles("*.prefab", SearchOption.TopDirectoryOnly));
            allFiles.AddRange(new DirectoryInfo(prefabRootPath).GetFiles("*.prefab", SearchOption.TopDirectoryOnly));
 
            var inspectorModeInfo = typeof(SerializedObject).GetProperty("inspectorMode", BindingFlags.NonPublic | BindingFlags.Instance);
 
            var count = allFiles.Count;
            var index = 0;
 
            foreach (var file in allFiles)
            {
                var assetPath = file.FullName.Replace("\\", "/").Replace(Application.dataPath, "Assets");
                var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
                var prefabText = File.ReadAllText(file.FullName);
                var texts = prefab.GetComponentsInChildren<Text>(true);
 
                foreach (var text in texts)
                {
                    if (ContainChineseCharacter(text.text))
                    {
                        var content = text.text.Replace("\t", "").Replace("\r\n", "").Replace("\n", "").Replace("\r", "");
 
                        var serializedObject = new SerializedObject(text);
                        inspectorModeInfo.SetValue(serializedObject, InspectorMode.Debug, null);
                        var localIdProp = serializedObject.FindProperty("m_LocalIdentfierInFile");
                        var fieldId = localIdProp.longValue;
 
                        var serialized = ContainGuid(prefabText, fieldId.ToString());
                        var isFromLanguage = text is TextEx && (text as TextEx).isKey;
 
                        chineseCharactorErrors.Add(StringUtility.Contact(chineseCharactorErrors.Count, "\t", prefab.name, "\t", text.name, "\t", content, "\t", !serialized && !isFromLanguage));
                        Debug.LogFormat("UI预置体Text配置错误-->预置体是:{0},组件名:{1},内容是:{2}", prefab, text.name, text.text);
                    }
                }
 
                index++;
                EditorUtility.SetDirty(prefab);
                EditorUtility.DisplayProgressBar("查找中文字", StringUtility.Contact("正在检查:", prefab.name), (float)index / count);
            }
 
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
 
            EditorUtility.ClearProgressBar();
        }
        catch (Exception ex)
        {
            Debug.Log(ex);
        }
        finally
        {
            File.WriteAllLines(Application.dataPath + "/UIPrefabChineseCharacterError.txt", chineseCharactorErrors.ToArray());
        }
 
    }
 
    static string chineseCharacterPattern = "[\u4e00-\u9fa5]{2,4}";
    static bool ContainChineseCharacter(string _input)
    {
        if (string.IsNullOrEmpty(_input))
        {
            return false;
        }
        else
        {
            return Regex.IsMatch(_input, chineseCharacterPattern);
        }
    }
 
    static bool ContainGuid(string _serializedFile, string _guid)
    {
        if (string.IsNullOrEmpty(_serializedFile))
        {
            return false;
        }
        else
        {
 
            var matches = Regex.Matches(_serializedFile, _guid);
            return matches.Count > 2;
        }
    }
 
 
}