Unity项目脚本编码格式转换为UTF8编码格式
2017-01-18 21:31
549 查看
今天由于项目从Window转MAC的需要,对本地lua脚本文件进行格式转换,将全部.lua脚本文件的格式由UTF8有BOM转化为无BOM格式,一下是编辑器功能脚本,需要的朋友可以借鉴一下:
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text;
/// <summary>
/// 这个是讲lua文件转化为UTF-8格式的工具
/// </summary>
public class SetLuaToUTF8 : EditorWindow
{
// 常量 //
private const string EDITOR_VERSION = "v0.01"; // 这个编辑器的版本号 //
// gui 相关 //
private Vector2 m_scrollPos; // 记录 gui 界面的滚动 //
private string m_luaPath = "Lua/UI/"; // 文件路径
private string m_fileSuffix = ".lua"; // 文件后缀
/// <summary>
/// 数据目录
/// </summary>
static string AppDataPath
{
get { return Application.dataPath.ToLower(); }
}
[MenuItem("Lua/File to UTF-8 Encoding")]
static void Init()
{
Debug.Log("初始化转化lua文件为UTF-8格式");
// Get existing open window or if none, make a new one:
SetLuaToUTF8 window = (SetLuaToUTF8)EditorWindow.GetWindow(typeof(SetLuaToUTF8));
}
// UI 按钮显示 //
void OnGUI()
{
m_scrollPos = GUILayout.BeginScrollView(m_scrollPos, GUILayout.Width(Screen.width), GUILayout.Height(Screen.height));
GUILayout.BeginVertical(); // begin
//-------------- 调试按钮 --------------//
// 导出 UI 路径 //
GUILayout.Label("===========================");
GUILayout.BeginHorizontal();
GUILayout.Label("要转化的路径: "); //
m_luaPath = GUILayout.TextField(m_luaPath, 64);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("文件后缀: "); //
m_fileSuffix = GUILayout.TextField(m_fileSuffix, 64);
GUILayout.EndHorizontal();
if (GUILayout.Button("\n 文件转utf-8格式 \n")) //
{
this.Conversion();
}
//-------------- end 调试按钮 --------------//
GUILayout.Space(5);
GUILayout.Label("工具版本号: " + EDITOR_VERSION); //
GUILayout.EndVertical(); // end
GUILayout.EndScrollView();
}
// 开始转化
private void Conversion()
{
if (m_luaPath.Equals(string.Empty))
{
return;
}
if (!IsFolderExists(m_luaPath)) {
Debug.LogError("找不到文件夹路径!");
return;
}
string path = AppDataPath + "/" + m_luaPath;
string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
foreach (string file in files)
{
if (!file.EndsWith(m_fileSuffix)) continue;
string strTempPath = file.Replace(@"\", "/");
Debug.Log("文件路径:" + strTempPath);
ConvertFileEncoding(strTempPath, null, new UTF8Encoding(false));
}
AssetDatabase.Refresh();
Debug.Log("格式转换完成!");
}
/// 检测是否存在文件夹
private static bool IsFolderExists(string folderPath)
{
if (folderPath.Equals(string.Empty))
{
return false;
}
return Directory.Exists(GetFullPath(folderPath));
}
/// 返回Application.dataPath下完整目录
private static string GetFullPath(string srcName)
{
if (srcName.Equals(string.Empty))
{
return Application.dataPath;
}
if (srcName[0].Equals('/'))
{
srcName.Remove(0, 1);
}
return Application.dataPath + "/" + srcName;
}
/// <summary>
/// 文件编码转换
/// </summary>
/// <param name="sourceFile">源文件</param>
/// <param name="destFile">目标文件,如果为空,则覆盖源文件</param>
/// <param name="targetEncoding">目标编码</param>
private static void ConvertFileEncoding(string sourceFile, string destFile, Encoding targetEncoding)
{
destFile = string.IsNullOrEmpty(destFile) ? sourceFile : destFile;
File.WriteAllText(destFile, File.ReadAllText(sourceFile, Encoding.UTF8), targetEncoding);
}
}
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text;
/// <summary>
/// 这个是讲lua文件转化为UTF-8格式的工具
/// </summary>
public class SetLuaToUTF8 : EditorWindow
{
// 常量 //
private const string EDITOR_VERSION = "v0.01"; // 这个编辑器的版本号 //
// gui 相关 //
private Vector2 m_scrollPos; // 记录 gui 界面的滚动 //
private string m_luaPath = "Lua/UI/"; // 文件路径
private string m_fileSuffix = ".lua"; // 文件后缀
/// <summary>
/// 数据目录
/// </summary>
static string AppDataPath
{
get { return Application.dataPath.ToLower(); }
}
[MenuItem("Lua/File to UTF-8 Encoding")]
static void Init()
{
Debug.Log("初始化转化lua文件为UTF-8格式");
// Get existing open window or if none, make a new one:
SetLuaToUTF8 window = (SetLuaToUTF8)EditorWindow.GetWindow(typeof(SetLuaToUTF8));
}
// UI 按钮显示 //
void OnGUI()
{
m_scrollPos = GUILayout.BeginScrollView(m_scrollPos, GUILayout.Width(Screen.width), GUILayout.Height(Screen.height));
GUILayout.BeginVertical(); // begin
//-------------- 调试按钮 --------------//
// 导出 UI 路径 //
GUILayout.Label("===========================");
GUILayout.BeginHorizontal();
GUILayout.Label("要转化的路径: "); //
m_luaPath = GUILayout.TextField(m_luaPath, 64);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("文件后缀: "); //
m_fileSuffix = GUILayout.TextField(m_fileSuffix, 64);
GUILayout.EndHorizontal();
if (GUILayout.Button("\n 文件转utf-8格式 \n")) //
{
this.Conversion();
}
//-------------- end 调试按钮 --------------//
GUILayout.Space(5);
GUILayout.Label("工具版本号: " + EDITOR_VERSION); //
GUILayout.EndVertical(); // end
GUILayout.EndScrollView();
}
// 开始转化
private void Conversion()
{
if (m_luaPath.Equals(string.Empty))
{
return;
}
if (!IsFolderExists(m_luaPath)) {
Debug.LogError("找不到文件夹路径!");
return;
}
string path = AppDataPath + "/" + m_luaPath;
string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
foreach (string file in files)
{
if (!file.EndsWith(m_fileSuffix)) continue;
string strTempPath = file.Replace(@"\", "/");
Debug.Log("文件路径:" + strTempPath);
ConvertFileEncoding(strTempPath, null, new UTF8Encoding(false));
}
AssetDatabase.Refresh();
Debug.Log("格式转换完成!");
}
/// 检测是否存在文件夹
private static bool IsFolderExists(string folderPath)
{
if (folderPath.Equals(string.Empty))
{
return false;
}
return Directory.Exists(GetFullPath(folderPath));
}
/// 返回Application.dataPath下完整目录
private static string GetFullPath(string srcName)
{
if (srcName.Equals(string.Empty))
{
return Application.dataPath;
}
if (srcName[0].Equals('/'))
{
srcName.Remove(0, 1);
}
return Application.dataPath + "/" + srcName;
}
/// <summary>
/// 文件编码转换
/// </summary>
/// <param name="sourceFile">源文件</param>
/// <param name="destFile">目标文件,如果为空,则覆盖源文件</param>
/// <param name="targetEncoding">目标编码</param>
private static void ConvertFileEncoding(string sourceFile, string destFile, Encoding targetEncoding)
{
destFile = string.IsNullOrEmpty(destFile) ? sourceFile : destFile;
File.WriteAllText(destFile, File.ReadAllText(sourceFile, Encoding.UTF8), targetEncoding);
}
}
相关文章推荐
- 将编码格式为utf8转换为GBK的一个脚本
- 一个转换编码格式的VBS脚本
- Python的编码格式转换
- LINUX 转换文件编码 GBK->UTF-8文件编码批量转换脚本[zt]
- vim 文本 编码 gb2312 转换为 utf8 格式 回车与换行
- 团队项目开发"编码规范"之八:代码格式检查
- 将汉字转换为拼音字母(注意编码保存的格式)
- PHP下中文编码各种格式间的转换类
- JAVA字符串转时间及编码格式转换等
- 编码格式的转换
- vs2005与vs2008之间的项目格式相互转换方法
- android json数据传递时编码格式的转换
- sell脚本,将GB2312编码转换成UTF8编码。
- 如何查看文件编码格式及转换文件编码?
- 将普通字符串转换成MD5编码格式字符
- ASP+FSO生成的网页文件默认编码格式以及转换成UTF-8编码方法
- js脚本_小写金额转换成大写(支持千分位格式及负数输入,整数部分可达44位-千正~元,小数部分可达5位-角~丝) AmountInWords(dValue, maxDec)
- C盘转换为NTFS格式,一键清理系统垃圾文件脚本.
- linux系统下文件编码格式的转换
- 使用8位字节的编码格式将字节流安全的转换成String