很多项目初期,是没有考虑多语言或者多语言框架的情况。
而且为了方便平时查看、调试,我们都会使用中文作为默认语言在代码中赋值。
所以会造成项目后期,代码里的中文字符太多,做多语言的时候,要挨个脚本去找,非常麻烦。
为了解决这个繁琐问题,我们就可以使用编辑器来快速筛选中文
上代码:
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 |
using UnityEngine; using UnityEditor; using System.Collections.Generic; using System.IO; using System.Collections; using System.Text.RegularExpressions; using System.Text; public class FindChineseTool : MonoBehaviour { [MenuItem("Tools/查找代码中文")] public static void Pack() { Rect wr = new Rect(300, 400, 400, 100); FindChineseWindow window = (FindChineseWindow)EditorWindow.GetWindowWithRect(typeof(FindChineseWindow), wr, true, "查找项目中的中文字符"); window.Show(); } } public class FindChineseWindow : EditorWindow { private ArrayList csList = new ArrayList(); private int eachFrameFind = 4; private int currentIndex = 0; private bool isBeginUpdate = false; private string outputText; public string filePath = "/Scripts"; private string strForShader = ""; private void GetAllFile(DirectoryInfo dir) { FileInfo[] allFile = dir.GetFiles(); foreach (FileInfo fi in allFile) { if (fi.DirectoryName.Contains("FindChineseTool"))//排除指定名称的代码 continue; if (fi.FullName.IndexOf(".meta") == -1 && fi.FullName.IndexOf(".cs") != -1) { csList.Add(fi.DirectoryName + "/" + fi.Name); } } DirectoryInfo[] allDir = dir.GetDirectories(); foreach (DirectoryInfo d in allDir) { GetAllFile(d); } } public void OnGUI() { filePath = EditorGUILayout.TextField("路径:", filePath); EditorGUILayout.Space(); EditorGUILayout.Space(); if (GUILayout.Button("开始遍历目录")) { csList.Clear(); DirectoryInfo d = new DirectoryInfo(Application.dataPath + filePath); GetAllFile(d); GetAllFile(d); outputText = "游戏内代码文件的数量:" + csList.Count; isBeginUpdate = true; outputText = "开始遍历项目"; } GUILayout.Label(outputText, EditorStyles.boldLabel); } void Update() { if (isBeginUpdate && currentIndex < csList.Count) { int count = (csList.Count - currentIndex) > eachFrameFind ? eachFrameFind : (csList.Count - currentIndex); for (int i = 0; i < count; i++) { string url = csList[currentIndex].ToString(); currentIndex = currentIndex + 1; url = url.Replace("\\", "/"); printChinese(url); } if (currentIndex >= csList.Count) { isBeginUpdate = false; currentIndex = 0; outputText = "遍历结束,总共" + csList.Count; } } } private bool HasChinese(string str) { return Regex.IsMatch(str, @"[\u4e00-\u9fa5]"); } private Regex regex = new Regex("\"[^\"]*\""); private void printChinese(string path) { if (File.Exists(path)) { string[] fileContents = File.ReadAllLines(path, Encoding.Default); int count = fileContents.Length; for (int i = 0; i < count; i++) { string printStr = fileContents[i].Trim(); if (printStr.IndexOf("//") == 0) //说明是注释 continue; if (printStr.IndexOf("Debug.Log") == 0) //说明是注释 continue; //if (printStr.IndexOf("ALog.Log") == 0) //说明是注释 // continue; MatchCollection matches = regex.Matches(printStr); foreach (Match match in matches) { if (HasChinese(match.Value)) { string[] fullPath = path.Split('/'); path = fullPath[fullPath.Length - 1]; Debug.Log("路径:"+path + " 行数:" + i + " 内容:" + printStr); break; } } } fileContents = null; } } } |
- 本文固定链接: http://www.u3d8.com/?p=1970
- 转载请注明: 网虫虫 在 u3d8.com 发表过