unity 中文 ANSI 转 utf-8

参考

windows 默认 格式 ANSI
unity 中文 ANSI 转 utf-8_第1张图片
unity 中文 ANSI 转 utf-8_第2张图片

using UnityEngine;
using UnityEditor;
using System.Text;
using System.IO;

/// 
/// 控制器
/// 
public class EncodeConverEditor
{

    //创建UTF-8代码
    [MenuItem("Assets/Create/C# Script UTF-8", false, 10)]
    static void CreateCodeText()
    {
        // 获取选中的文件夹或文件的路径
        string selectedPath = AssetDatabase.GetAssetPath(Selection.activeObject);

        string className = "NewBehaviourScript";
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.AppendLine(string.Format("using UnityEngine;"));
        stringBuilder.AppendLine();
        stringBuilder.AppendLine(string.Format("/// "));
        stringBuilder.AppendLine(string.Format("/// 控制器"));
        stringBuilder.AppendLine(string.Format("/// "));
        stringBuilder.AppendLine(string.Format("public class {0} : MonoBehaviour", className));
        stringBuilder.AppendLine("{");
        stringBuilder.AppendLine();
        stringBuilder.AppendLine();
        stringBuilder.AppendLine("}");
        string codeText = stringBuilder.ToString();

        //string codeText = "using UnityEngine;\r\n\r\npublic class NewBehaviourScript1 : MonoBehaviour\r\n{\r\n\r\n}\r\n";

        File.WriteAllText(selectedPath + $"/{className}.cs", codeText, System.Text.Encoding.UTF8);

        // 刷新Unity资源窗口
        AssetDatabase.Refresh();
    }


    [MenuItem("Assets/2. TO UTF8")]
    static void TOutf8()
    {
        Caching.ClearCache();
        Object obj = Selection.activeObject;
        string rootPath = AssetDatabase.GetAssetPath(obj);


        if (obj != null && obj is TextAsset)
        {
            Toutf8(System.Environment.CurrentDirectory + "/" + rootPath);
        }
        else
        {
            //string filter = "t:Scene t:Prefab t:Texture";
            string filter = "t:Script";
            string[] resFiles = AssetDatabase.FindAssets(filter, new string[] { rootPath });

            for (int i = 0; i < resFiles.Length; i++)
            {
                //"Assets/Resources/Cube (1).prefab"
                resFiles[i] = AssetDatabase.GUIDToAssetPath(resFiles[i]);
                //"Cube (1)" 
                string fileName = System.IO.Path.GetFileNameWithoutExtension(resFiles[i]);

                string path = resFiles[i];
                Toutf8(System.Environment.CurrentDirectory + "/" + path);
            }
        }

        Debug.Log("查找to utf-8 结束 ");
        AssetDatabase.Refresh();
    }


    private static void Toutf8(string assetPath)
    {
        Encoding encoding = Encoding.GetEncoding(936); //ANSI
        string text = File.ReadAllText(assetPath, encoding);

        System.IO.File.WriteAllText(assetPath, text, Encoding.UTF8);

    }

}

你可能感兴趣的:(unity,游戏引擎)