Unity Editor工具制作
文本转UTF-8编码
[MenuItem("Assets/文本转UTF-8编码")]
public static void ChangeToUTF8()
{
TextAsset[] allAssets = Selection.GetFiltered<TextAsset>(SelectionMode.DeepAssets);
for (int i = 0; i < allAssets.Length; i++)
{
TextAsset textAsset = allAssets[i];
string path = AssetDatabase.GetAssetPath(textAsset);
string filePath = Application.dataPath + "/" + path.Substring("Assets/".Length);
if (GetTextFileEncodingType(filePath).Equals(Encoding.UTF8))
{
Debug.LogFormat("{0} 已经是UTF8编码,不转换", textAsset.name);
continue;
}
string content = File.ReadAllText(filePath, Encoding.GetEncoding("gb2312"));
File.WriteAllText(filePath, content, Encoding.UTF8);
Debug.LogFormat("{0} 转换完毕", textAsset.name);
AssetDatabase.Refresh();
}
}
/// <summary>
/// 获取文本文件的字符编码类型
/// </summary>
private static Encoding GetTextFileEncodingType(string fileName)
{
Encoding encoding = Encoding.Default;
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(fileStream, encoding);
byte[] buffer = binaryReader.ReadBytes((int)fileStream.Length);
binaryReader.Close();
fileStream.Close();
if (buffer.Length >= 3 && buffer[0] == 239 && buffer[1] == 187 && buffer[2] == 191)
{
encoding = Encoding.UTF8;
}
else if (buffer.Length >= 3 && buffer[0] == 254 && buffer[1] == 255 && buffer[2] == 0)
{
encoding = Encoding.BigEndianUnicode;
}
else if (buffer.Length >= 3 && buffer[0] == 255 && buffer[1] == 254 && buffer[2] == 65) {
encoding = Encoding.Unicode;
}
else if (IsUTF8Bytes(buffer))
{
encoding = Encoding.UTF8;
}
return encoding;
}
用NotePad打开
[MenuItem("Assets/用NotePad打开")]
public static void OpenTxtByNotePad()
{
Object[] objs = Selection.objects;
for (int i = 0; i < objs.Length; i++)
{
Object obj = objs[i];
var path = AssetDatabase.GetAssetPath(obj);
string exePath = GetNotePadPath();
string filePath = Path.Combine(Application.dataPath, path.Substring("Assets/".Length));
ProcessUtility.RunProgram(exePath, filePath, "");
}
}
/// <summary>
/// 获取Notepad++工作路径
/// </summary>
public static string GetNotePadPath()
{
RegistryKey reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\notepad++.exe");
if (reg == null)
{
return "notepad.exe";
}
return reg.GetValue("").ToString();
}
用WPS表格打开
下面展示一些 内联代码片
。
// A code block
var foo = 'bar';
[MenuItem("Assets/用WPS表格打开")]
public static void OpenTxtByExcel()
{
Object[] objs = Selection.objects;
for (int i = 0; i < objs.Length; i++)
{
Object obj = objs[i];
var path = AssetDatabase.GetAssetPath(obj);
string exePath = GetExcelPath();
string filePath = Path.Combine(Application.dataPath, path.Substring("Assets/".Length));
ProcessUtility.RunProgram(exePath, filePath, "");
}
}
/// <summary>
/// 获取Excel工作路径
/// </summary>
public static string GetExcelPath()
{
// 默认使用excel
//RegistryKey reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Excel.exe");//读取注册表
RegistryKey reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\et.exe");
if (reg == null)
{
reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\et.exe");
}
if (reg == null)
{
reg = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\et.exe");
}
return reg.GetValue("").ToString();
}