拿到個(gè)二開項(xiàng)目浇雹,公司準(zhǔn)備韓國(guó)發(fā)布橄霉,查看腳本中發(fā)現(xiàn)(活動(dòng)描述晌姚,屬性名等是寫在腳本中的)沧奴。
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>();
// 在菜單來創(chuàng)建 選項(xiàng) 痘括, 點(diǎn)擊該選項(xiàng)執(zhí)行搜索代碼
[MenuItem("Tools/獲取項(xiàng)目腳本中的中文")]
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("查找完成");
}
//參數(shù)1 為要查找的總路徑, 參數(shù)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);
}
}
}
// 判斷 當(dāng)前字符是否為中文
static bool isChinese(char c)
{
return c >= 0x4E00 && c <= 0x9FA5;
}
//判斷 當(dāng)前行是否含有中文
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;
}
// 根據(jù)路徑 獲取文件內(nèi)容
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();
}
//文件的創(chuàng)建,寫入
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("="))
{
// 判斷=號(hào)前是否有中文
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(".."))
{
// 判斷=號(hào)前是否有中文
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();
// 辨別重復(fù)行
if (strinfoList.Contains(str))
{
return;
}
strinfoList.Add(str);
StreamWriter sw;//流信息
FileInfo t = new FileInfo(path +"/" + name);
if (!t.Exists)
{//判斷文件是否存在
sw = t.CreateText();//不存在纲菌,創(chuàng)建
}
else
{
sw = t.AppendText();//存在挠日,則打開
}
sw.WriteLine(str);//以行的形式寫入信息
sw.Close();//關(guān)閉流
sw.Dispose();//銷毀流
}
//刪除
[MenuItem("Tools/清空")]
static void DeleteTxt()
{
strinfoList.Clear();
string path = Application.dataPath + "/luaCHFont.txt";
File.WriteAllText(path, string.Empty);
Debug.Log("清除成功");
}
}