拿到个二开项目,公司准备韩国发布,查看脚本中发现(活动描述,属性名等是写在脚本中的)。
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.Networking;
public class GetAllCHFont : MonoBehaviour
{
static List<string> strinfoList = new List<string>();
// 在菜单来创建 选项 , 点击该选项执行搜索代码
[MenuItem("Tools/获取项目脚本中的中文")]
static void CheckSceneSetting()
{
List<string> dirs = new List<string>();
GetDirs(Application.dataPath+ "/LuaFramework/Lua/game", ref dirs);
DeleteTxt();
for (int i = 0; i < dirs.Count; i++)
{
if (!dirs[i].Contains("config\\"))
{
string path = string.Format("{0}/{1}", Application.dataPath, dirs[i]);
ReadData(path);
}
}
Debug.Log("查找完成");
}
//参数1 为要查找的总路径, 参数2 保存路径
private static void GetDirs(string dirPath, ref List<string> dirs)
{
foreach (string path in Directory.GetFiles(dirPath))
{
//Debug.LogError(path);
//获取所有文件夹中包含后缀为 .lua 的路径
if (Path.GetExtension(path) == ".lua")
{
dirs.Add(path.Substring(path.IndexOf("LuaFramework")));
}
}
if (Directory.GetDirectories(dirPath).Length > 0) //遍历所有文件夹
{
foreach (string path in Directory.GetDirectories(dirPath))
{
GetDirs(path, ref dirs);
}
}
}
// 判断 当前字符是否为中文
static bool isChinese(char c)
{
return c >= 0x4E00 && c <= 0x9FA5;
}
//判断 当前行是否含有中文
static bool checkString(string str)
{
char[] ch = str.ToCharArray();
if (str != null)
{
for (int i = 0; i < ch.Length; i++)
{
if (isChinese(ch[i]))
{
return true;
}
}
}
return false;
}
// 根据路径 获取文件内容
static void ReadData(string path)
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
if (null == sr) return;
string str = sr.ReadToEnd();
string[] strs = str.Split('\n');
for (int i = 0; i < strs.Length; i++)
{
// 只需要含有中文的行
if (checkString(strs[i]))
{
Createfile(Application.dataPath, "luaCHFont.txt", strs[i].Trim());
}
}
sr.Close();
}
//文件的创建,写入
static void Createfile(string path, string name, string info)
{
// --注释不写入
if (info.Contains("--"))
{
string shaicha = info.Substring(0, info.IndexOf("--"));
if (!checkString(shaicha))
{
return;
}
}
//@注释不写入
if (info.Contains("@des") || info.Contains("@param") || info.Contains("@key") || info.Contains("@return") || info.Contains("*/"))
{
return;
}
// 日志打印不写入
if (info.Contains("print(") || info.Contains("print2") || info.Contains("logWarn(") || info.Contains("logError("))
{
return;
}
// 整理字符串
string str = info.Replace("\t", "");
str = str.Replace("Notify.ShowText", "");
str = str.Replace("Dialog.ShowTwo", "");
if (str.Contains("string.format"))
{
string _str = str.Substring(0, str.IndexOf("string.format"));
if (!checkString(_str))
{
str = str.Substring(str.IndexOf("string.format") + 13);
}
}
if (str.Contains("buy("))
{
string _str = str.Substring(0, str.IndexOf("buy("));
if (!checkString(_str))
{
str = str.Substring(str.IndexOf("buy(") + 3);
}
}
if (str.Contains("="))
{
// 判断=号前是否有中文
string _str = str.Substring(0, str.IndexOf("="));
if (!checkString(_str))
{
str = str.Substring(str.IndexOf("=") + 1);
}
}
if (str.Contains("="))
{
string _str = str.Substring(str.IndexOf("="));
if (!checkString(_str))
{
str = str.Substring(0, str.IndexOf("="));
}
}
if (str.Contains(".."))
{
// 判断=号前是否有中文
string _str = str.Substring(0, str.IndexOf(".."));
if (!checkString(_str))
{
str = str.Substring(str.IndexOf("..") + 1);
}
}
if (str.Contains(".."))
{
string _str = str.Substring(str.IndexOf(".."));
if (!checkString(_str))
{
str = str.Substring(0, str.IndexOf(".."));
}
}
str = str.Trim();
// 辨别重复行
if (strinfoList.Contains(str))
{
return;
}
strinfoList.Add(str);
StreamWriter sw;//流信息
FileInfo t = new FileInfo(path +"/" + name);
if (!t.Exists)
{//判断文件是否存在
sw = t.CreateText();//不存在,创建
}
else
{
sw = t.AppendText();//存在,则打开
}
sw.WriteLine(str);//以行的形式写入信息
sw.Close();//关闭流
sw.Dispose();//销毁流
}
//删除
[MenuItem("Tools/清空")]
static void DeleteTxt()
{
strinfoList.Clear();
string path = Application.dataPath + "/luaCHFont.txt";
File.WriteAllText(path, string.Empty);
Debug.Log("清除成功");
}
}