您的位置:首页 > 移动开发 > Unity3D

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息