convertutf8..cs 提交

dev_chengFeng
jiaoyangna 2020-08-07 15:19:25 +08:00
parent 723531298d
commit 63f1dcb2c9
1 changed files with 3 additions and 149 deletions

View File

@ -1,263 +1,117 @@
 
using UnityEngine; using UnityEngine;
using UnityEditor; using UnityEditor;
using System.IO; using System.IO;
using System.Text; using System.Text;
/// <summary> /// <summary>
/// 这个是讲lua文件转化为UTF-8格式的工具 /// 这个是讲lua文件转化为UTF-8格式的工具
/// </summary> /// </summary>
public class SetLuaToUTF8 : EditorWindow public class SetLuaToUTF8 : EditorWindow
{ {
// 常量 //
// 常量 //
private const string EDITOR_VERSION = "v0.01"; // 这个编辑器的版本号 // private const string EDITOR_VERSION = "v0.01"; // 这个编辑器的版本号 //
// gui 相关 // // gui 相关 //
private Vector2 m_scrollPos; // 记录 gui 界面的滚动 // private Vector2 m_scrollPos; // 记录 gui 界面的滚动 //
private string m_luaPath = "ManagedResources/~Lua/"; // 文件路径 private string m_luaPath = "ManagedResources/~Lua/"; // 文件路径
private string m_fileSuffix = ".lua"; // 文件后缀 private string m_fileSuffix = ".lua"; // 文件后缀
/// <summary> /// <summary>
/// 数据目录 /// 数据目录
/// </summary> /// </summary>
static string AppDataPath static string AppDataPath
{ {
get { return Application.dataPath.ToLower(); } get { return Application.dataPath.ToLower(); }
} }
[MenuItem("Lua/File to UTF-8 Encoding")] [MenuItem("Lua/File to UTF-8 Encoding")]
static void Init() static void Init()
{ {
Debug.Log("初始化转化lua文件为UTF-8格式"); Debug.Log("初始化转化lua文件为UTF-8格式");
// Get existing open window or if none, make a new one: // Get existing open window or if none, make a new one:
SetLuaToUTF8 window = (SetLuaToUTF8)EditorWindow.GetWindow(typeof(SetLuaToUTF8)); SetLuaToUTF8 window = (SetLuaToUTF8)EditorWindow.GetWindow(typeof(SetLuaToUTF8));
} }
// UI 按钮显示 // // UI 按钮显示 //
void OnGUI() void OnGUI()
{ {
m_scrollPos = GUILayout.BeginScrollView(m_scrollPos, GUILayout.Width(Screen.width), GUILayout.Height(Screen.height)); m_scrollPos = GUILayout.BeginScrollView(m_scrollPos, GUILayout.Width(Screen.width), GUILayout.Height(Screen.height));
GUILayout.BeginVertical(); // begin GUILayout.BeginVertical(); // begin
//-------------- 调试按钮 --------------// //-------------- 调试按钮 --------------//
// 导出 UI 路径 // // 导出 UI 路径 //
GUILayout.Label("==========================="); GUILayout.Label("===========================");
GUILayout.BeginHorizontal(); GUILayout.BeginHorizontal();
GUILayout.Label("要转化的路径: "); // GUILayout.Label("要转化的路径: "); //
m_luaPath = GUILayout.TextField(m_luaPath, 64); m_luaPath = GUILayout.TextField(m_luaPath, 64);
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
GUILayout.BeginHorizontal(); GUILayout.BeginHorizontal();
GUILayout.Label("文件后缀: "); // GUILayout.Label("文件后缀: "); //
m_fileSuffix = GUILayout.TextField(m_fileSuffix, 64); m_fileSuffix = GUILayout.TextField(m_fileSuffix, 64);
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
if (GUILayout.Button("\n 文件转utf-8格式 \n")) // if (GUILayout.Button("\n 文件转utf-8格式 \n")) //
{ {
this.Conversion(); this.Conversion();
} }
//-------------- end 调试按钮 --------------// //-------------- end 调试按钮 --------------//
GUILayout.Space(5); GUILayout.Space(5);
GUILayout.Label("工具版本号: " + EDITOR_VERSION); // GUILayout.Label("工具版本号: " + EDITOR_VERSION); //
GUILayout.EndVertical(); // end GUILayout.EndVertical(); // end
GUILayout.EndScrollView(); GUILayout.EndScrollView();
} }
// 开始转化 // 开始转化
private void Conversion() private void Conversion()
{ {
if (m_luaPath.Equals(string.Empty)) if (m_luaPath.Equals(string.Empty))
{ {
return; return;
} }
if (!IsFolderExists(m_luaPath)) if (!IsFolderExists(m_luaPath))
{ {
Debug.LogError("找不到文件夹路径!"); Debug.LogError("找不到文件夹路径!");
return; return;
} }
string path = AppDataPath + "/" + m_luaPath; string path = AppDataPath + "/" + m_luaPath;
string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories); string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
foreach (string file in files) foreach (string file in files)
{ {
if (!file.EndsWith(m_fileSuffix)) continue;
if (!file.EndsWith(m_fileSuffix)) continue;
string strTempPath = file.Replace(@"\", "/"); string strTempPath = file.Replace(@"\", "/");
Debug.Log("文件路径:" + strTempPath); Debug.Log("文件路径:" + strTempPath);
ConvertFileEncoding(strTempPath, null, new UTF8Encoding(false)); ConvertFileEncoding(strTempPath, null, new UTF8Encoding(false));
} }
AssetDatabase.Refresh(); AssetDatabase.Refresh();
Debug.Log("格式转换完成!"); Debug.Log("格式转换完成!");
} }
/// 检测是否存在文件夹 /// 检测是否存在文件夹
private static bool IsFolderExists(string folderPath) private static bool IsFolderExists(string folderPath)
{ {
if (folderPath.Equals(string.Empty)) if (folderPath.Equals(string.Empty))
{ {
return false;
return false;
} }
return Directory.Exists(GetFullPath(folderPath)); return Directory.Exists(GetFullPath(folderPath));
} }
/// 返回Application.dataPath下完整目录 /// 返回Application.dataPath下完整目录
private static string GetFullPath(string srcName) private static string GetFullPath(string srcName)
{ {
if (srcName.Equals(string.Empty)) if (srcName.Equals(string.Empty))
{ {
return Application.dataPath; return Application.dataPath;
} }
if (srcName[0].Equals('/')) if (srcName[0].Equals('/'))
{ {
srcName.Remove(0, 1); srcName.Remove(0, 1);
} }
return Application.dataPath + "/" + srcName; return Application.dataPath + "/" + srcName;
} }
/// <summary> /// <summary>
/// 文件编码转换 /// 文件编码转换
/// </summary> /// </summary>
/// <param name="sourceFile">源文件</param> /// <param name="sourceFile">源文件</param>
/// <param name="destFile">目标文件,如果为空,则覆盖源文件</param> /// <param name="destFile">目标文件,如果为空,则覆盖源文件</param>
/// <param name="targetEncoding">目标编码</param> /// <param name="targetEncoding">目标编码</param>
private static void ConvertFileEncoding(string sourceFile, string destFile, Encoding targetEncoding) private static void ConvertFileEncoding(string sourceFile, string destFile, Encoding targetEncoding)
{ {
destFile = string.IsNullOrEmpty(destFile) ? sourceFile : destFile; destFile = string.IsNullOrEmpty(destFile) ? sourceFile : destFile;
File.WriteAllText(destFile, File.ReadAllText(sourceFile, Encoding.UTF8), targetEncoding); File.WriteAllText(destFile, File.ReadAllText(sourceFile, Encoding.UTF8), targetEncoding);
} }
} }