啊 ,好多英文,头晕,脑壳疼,看不懂?
没关系,EasyWork v0.01 版本来了
用的是百度翻译,使用前请先申请百度翻译开发者
Ctrl+F 搜索 害羞的代码 ,更改为自己的账号密码
代码放到工程中,选中资源文件夹,右键选择 EasyWork
直接上代码
/********************************************************
文件: 一键翻译.cs
作者: UserName
日期: CreateTime
寄语: 虎年 虎虎生威 大吉大利
功能: Nothing
*********************************************************/
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using UnityEditor;
using System.Threading.Tasks;
public class 一键翻译
{
public class TranstionResult
{
//错误码,翻译结果无法正常返回
public string Error_code { get; set; }
public string Error_msg { get; set; }
public string from { get; set; }
public string to { get; set; }
public string Query { get; set; }
//翻译正确,返回的结果
//这里是数组的原因是百度翻译支持多个单词或多段文本的翻译,在发送的字段q中用换行符(\n)分隔
public Translation[] trans_result { get; set; }
public Dictionary<string, string> GetStringList()
{
Dictionary<string, string> list = new Dictionary<string, string>();
foreach (var item in trans_result)
{
list.Add(item.src, item.dst);
}
return list;
}
}
[MenuItem("Assets/阿飞的EasyWork/修改当前所有子文件名为中文")]
public static async void Click()
{
List<CustomFile> fils =GetChildFileAll();
int num = 0;
while (num < fils.Count)
{
string totranslationText = "";
for (int i = num; i < num + 50; i++)
{
if (i >= fils.Count)
continue;
totranslationText += fils[i].Name + "\n";
}
num += 50;
Dictionary<string, string> values = new Dictionary<string, string>();
TranstionResult result = TranslationEnglishToChinese(totranslationText);
values = result.GetStringList();
foreach (var item in values)
{
for (int i = 0; i < fils.Count; i++)
{
if (fils[i].Name == item.Key)
{
fils[i].TransChineseName(item.Value);
}
}
}
await Task.Delay(10);
}
Debug.Log("翻译完成");
}
public static string 获取Assets中选中的文件夹()
{
UnityEngine.Object[] arr = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.TopLevel);
return (Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/')) + "/" + AssetDatabase.GetAssetPath(arr[0]));
}
public static List<CustomFile> GetChildFileAll()
{
List<CustomFile> customFiles = new List<CustomFile>();
string rootPath = 获取Assets中选中的文件夹();
//获取指定路径下面的所有资源文件
if (Directory.Exists(rootPath))
{
DirectoryInfo direction = new DirectoryInfo(rootPath);
FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
//Debug.Log(string.Format("<color=green>文件数量 : {0}</color>", files.Length));
for (int i = 0; i < files.Length; i++)
{
if (files[i].Name.EndsWith(".meta"))
{
continue;
}
CustomFile tmpFile = new CustomFile();
tmpFile.Name = files[i].Name;
tmpFile.Path = files[i].FullName;
string[] vs = files[i].FullName.Split('.');
tmpFile.format = vs[vs.Length - 1];
customFiles.Add(tmpFile);
}
}
return customFiles;
}
public class Translation
{
public string src { get; set; }
public string dst { get; set; }
}
public static TranstionResult TranslationEnglishToChinese(string value)
{
// 原文
string q = value;
Debug.Log(q);
// 源语言
string from = "en";
// 目标语言
string to = "zh";
// 改成您的APP ID
string appId = "害羞的代码";
System.Random rd = new System.Random();
string salt = rd.Next(100000).ToString();
// 改成您的密钥
string secretKey = "害羞的代码";
string sign = EncryptString(appId + q + salt + secretKey);
string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
url += "q=" + HttpUtility.UrlEncode(q);
url += "&from=" + from;
url += "&to=" + to;
url += "&appid=" + appId;
url += "&salt=" + salt;
url += "&sign=" + sign;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
request.UserAgent = null;
request.Timeout = 6000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
Debug.Log(retString);
TranstionResult temp = LitJson.JsonMapper.ToObject<TranstionResult>(retString);
return temp;
}
// 计算MD5值
public static string EncryptString(string str)
{
MD5 md5 = MD5.Create();
// 将字符串转换成字节数组
byte[] byteOld = Encoding.UTF8.GetBytes(str);
// 调用加密方法
byte[] byteNew = md5.ComputeHash(byteOld);
// 将加密结果转换为字符串
StringBuilder sb = new StringBuilder();
foreach (byte b in byteNew)
{
// 将字节转换成16进制表示的字符串,
sb.Append(b.ToString("x2"));
}
// 返回加密的字符串
return sb.ToString();
}
public static void 修改文件名(string filePath, string newFileName)
{
if (File.Exists(filePath))
{
filePath = ConvertToAssetsPath(filePath);
Debug.Log("修改成功" + filePath + " ==>" + newFileName);
AssetDatabase.RenameAsset(filePath, newFileName);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
else
{
Debug.LogError(string.Format("未找到文件 :{1} 修改失败 >> 文件路径 {0}", filePath, newFileName));
}
}
public static string ConvertToAssetsPath(string path)
{
string newpath = "";
string[] roots = path.Split('\\');
bool isadd = false;
for (int i = 0; i < roots.Length; i++)
{
if (roots[i].Equals("Assets") && isadd == false)
{
isadd = true;
}
if (isadd)
{
if (i == roots.Length - 1)
{
newpath += roots[i];
}
else
{
newpath += roots[i] + "/";
}
}
}
return newpath;
}
public static void ChangeFileName(string filePath, string newFileName)
{
if (File.Exists(filePath))
{
filePath = ConvertToAssetsPath(filePath);
Debug.Log("修改成功" + filePath + " ==>" + newFileName);
AssetDatabase.RenameAsset(filePath, newFileName);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
else
{
Debug.LogError(string.Format("未找到文件 :{1} 修改失败 >> 文件路径 {0}", filePath, newFileName));
}
}
public class CustomFile
{
public string Name;
public string Path;
public string format;
public void TransChineseName(string chineseName)
{
ChangeFileName(Path, chineseName);
}
}
}
请忽略代码乱写一通
有些简写不能翻译,数字,符号看起来不那么优雅
后面有时间来处理