【Unity Editor工具制作-文本转UTF-8编码、用WPS表格打开表格、用WPS表格打开】

文本转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();
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值