Unity汉化一个插件 制作插件汉化工具

我是编程一个菜鸟,英语又不好,有的插件非常牛!我想学一学,页面全是英文,完全不知所措,我该怎么办啊...

尝试在Unity中汉化一个插件

效果:

请添加图片描述

思路:

如何在Unity中把一个自己喜欢的插件变成中文?在Unity中编写插件一般会用到编辑器扩展
在编辑器扩展中想在Inspector显示自己想要的属性名或者别的什么,就需要用到编辑器扩展的API
把这些固定的API存到一个字典里,例如“EditorGUILayout.PropertyField”,“LabelField”...我可以尝试先读取我们想要汉化插件的Editor文件夹下的每一个代码的每一行
把每一行的每个字符与字典做一个对比
对比成功就说明此行代码可以被汉化,收集可以被汉化的代码行,然后把可以被汉化的代码行替换成我们想要的代码
替换成功后保存代码听起来好像没啥问题,试试看
  • 创建一个存储字典的代码
using System.Collections.Generic;
using UnityEngine;[CreateAssetMenu()]
public class SearchCharacterData : ScriptableObject
{[Header("检索字符对")]public List<Item> items;[Header("添加字符对")]public bool addItem = false;private void OnValidate(){if (addItem){addItem = false;items.Add(new Item());}}/// <summary>/// 物品数据/// </summary>[System.Serializable]public class Item{public string startStr;public string endStr;}
}
  • 完成之后我们就可以在项目中创建一个自定义字典了
    在这里插入图片描述

  • 在字典中添加几对常用AIP用来测试
    在这里插入图片描述

  • 创建一个编辑器窗口代码

using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;[System.Serializable]
public class Editor_ChinesizationTool : EditorWindow
{private static Editor_ChinesizationTool _window;[MenuItem("Tools/汉化编辑器")]public static void GUIDRefReplaceWin(){Rect wr = new Rect(0, 0, 300, 1000);//窗口大小_window = (Editor_ChinesizationTool)GetWindow(typeof(Editor_ChinesizationTool), true, "汉化编辑");// false 表示不能停靠的_window.Show();}
}
没想到要去翻译一个插件,竟然要自己先写一个...造孽啊~
  • 读文件
	 	/// <summary>/// 读取数据/// </summary>/// <returns></returns>public List<string> ReadFileInfo(bool IsUpdateNewData = true){Datas.Clear();CurrentDatas.Clear();CurrentSplitDatas.Clear();if (IsUpdateNewData) NewSplitDatas.Clear();StreamReader sr = null;//读取string assetsName = FileInfo.FullName;sr = File.OpenText(assetsName.Substring(assetsName.IndexOf("Assets")));//读取文件//读取所有行int line = 0;string data = null;do{data = sr.ReadLine();if (data != null){Datas.Add(data);CurrentDatas.Add(data);foreach (var item in searchCharacterData.items){string csData = FindString(data, item.startStr, item.endStr);if (csData != ""){CurrentSplitDatas.Add(new NewCSData(line, csData));if (IsUpdateNewData) NewSplitDatas.Add(new NewCSData(line, csData));break;}}}line++;} while (data != null);sr.Close();//关闭流sr.Dispose();//销毁流return CurrentDatas;}
  • 将改好的数据进行写入
		void WriteFileInfo(List<string> datas){StreamWriter sw;//写入if (!FileInfo.Exists){Debug.LogError("无法写入,没有该文件");return;}ClearData(path);sw = FileInfo.AppendText();//打开文件foreach (string linedata in datas){sw.WriteLine(linedata);}sw.Flush();//清除缓冲区sw.Close();//关闭流sw.Dispose();//销毁流//ReadFileInfo();}
  • 稍稍修正一下编辑器页面,随便导入一个插件试试看
    在这里插入图片描述

源码

注意!此文件需要放在Editor文件夹下才可以正常使用
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;[System.Serializable]
public class Editor_ChinesizationTool : EditorWindow
{private static Editor_ChinesizationTool _window;public string modelPath = "Assets";public List<CSData> cslist = new List<CSData>();public static Rect modelRect;int maxLineCount = 5;Vector2 csDataPos, contentPos;SearchCharacterData searchCharacterData = null;CSData CurrentCSData = null;[MenuItem("Tools/汉化编辑器")]public static void GUIDRefReplaceWin(){Rect wr = new Rect(0, 0, 300, 1000);//窗口大小_window = (Editor_ChinesizationTool)GetWindow(typeof(Editor_ChinesizationTool), true, "汉化编辑");// false 表示不能停靠的_window.Show();}private Material m_material;//1private void OnEnable(){m_material = new Material(Shader.Find("Hidden/Internal-Colored"));//2m_material.hideFlags = HideFlags.HideAndDontSave;//3}void OnGUI(){EditorGUILayout.Space();EditorGUILayout.LabelField("将文件夹拖拽到此处");EditorGUILayout.Space();GUI.SetNextControlName("input1");//设置下一个控件的名字modelRect = EditorGUILayout.GetControlRect();modelPath = EditorGUI.TextField(modelRect, modelPath);EditorGUILayout.Space();DragFolder();EditorGUILayout.Space();searchCharacterData = EditorGUILayout.ObjectField("", searchCharacterData, typeof(SearchCharacterData), true) as SearchCharacterData;// 导出材质if (searchCharacterData == null){GUILayout.Label("请添加字典");return;}if (GUILayout.Button("读取文件")){ReadFile();CurrentCSData = null;}if (CurrentCSData == null){int currentLineCount = 1;csDataPos = EditorGUILayout.BeginScrollView(csDataPos, GUILayout.Width(1000), GUILayout.Height(500));bool isLineEnd = true;maxLineCount = EditorGUILayout.IntField("每行显示脚本的个数", maxLineCount);foreach (CSData csdate in cslist){if (currentLineCount == 1){GUILayout.BeginHorizontal();isLineEnd = false;}if (GUILayout.Button(csdate.name)){CurrentCSData = csdate;}if (currentLineCount == maxLineCount){GUILayout.EndHorizontal();currentLineCount = 0;isLineEnd = true;}currentLineCount++;}if (isLineEnd == false){GUILayout.EndHorizontal();}GUILayout.EndScrollView();}if (CurrentCSData != null){EditorGUILayout.BeginVertical("HelpBox");GUILayout.BeginHorizontal();csDataPos = EditorGUILayout.BeginScrollView(csDataPos, GUILayout.Width(500), GUILayout.Height(700));#region 显示代码int line = 1;lineLocations.Clear();foreach (var date in CurrentCSData.CurrentDatas){GUILayout.Label(line + "  " + date);foreach (var item in CurrentCSData.CurrentSplitDatas){if (line == item.line){LineLocation lineLocation = new LineLocation();Rect rect = GUILayoutUtility.GetLastRect();lineLocation.FirstRect = new Vector2(rect.x, rect.y - csDataPos.y);lineLocation.FirstRectOffset = csDataPos;lineLocations.Add(lineLocation);}}line++;}GUILayout.EndScrollView();GUILayout.Space(100);#endregioncontentPos = EditorGUILayout.BeginScrollView(contentPos, GUILayout.Width(700), GUILayout.Height(700));for (int i = 0; i < CurrentCSData.CurrentSplitDatas.Count; i++){//GUILayout.BeginHorizontal();GUILayout.Label(CurrentCSData.CurrentSplitDatas[i].line + 1 + "  " + CurrentCSData.CurrentDatas[CurrentCSData.CurrentSplitDatas[i].line]);//找到可更换数据lineLocations[i].FirstRect.y += contentPos.y;lineLocations[i].LastRectOffset = contentPos;Rect rect = GUILayoutUtility.GetLastRect();lineLocations[i].LastRect = new Vector2(rect.x, rect.y);CurrentCSData.NewSplitDatas[i].data = EditorGUILayout.TextField(CurrentCSData.NewSplitDatas[i].data);//GUILayout.EndHorizontal();}foreach (var item in lineLocations){m_material.SetPass(0);//4GL.Begin(GL.LINES);GL.Color(Color.red);GL.Vertex3(item.FirstRect.x - 100+10, LimitMax(item.FirstRect.y + 30,690+ item.LastRectOffset.y, item.LastRectOffset.y), 0);GL.Vertex3(item.FirstRect.x - 105+10, LimitMax(item.FirstRect.y + 30, 690+ item.LastRectOffset.y, item.LastRectOffset.y), 0);GL.End();GL.Begin(GL.LINES);GL.Color(Color.black);GL.Vertex3(item.FirstRect.x - 100+10, LimitMax(item.FirstRect.y + 30,690 + item.LastRectOffset.y, item.LastRectOffset.y), 0);//================================================================================================GL.Vertex3(item.LastRect.x-10, LimitMax(item.LastRect.y + 10, 690 + item.LastRectOffset.y, item.LastRectOffset.y), 0); GL.End();GL.Begin(GL.LINES);GL.Color(Color.red);GL.Vertex3(item.LastRect.x-10, LimitMax(item.LastRect.y + 10, 690 + item.LastRectOffset.y, item.LastRectOffset.y), 0);GL.Vertex3(item.LastRect.x-5, LimitMax(item.LastRect.y + 10, 690 + item.LastRectOffset.y, item.LastRectOffset.y), 0);GL.End();//Debug.Log("FirstRect:" + item.FirstRect+"__"+ "LastRect:"+ item.LastRect);//break;}GUILayout.EndScrollView();GUILayout.EndHorizontal();GUILayout.BeginHorizontal();if (GUILayout.Button("确认更改")){CurrentCSData.ReadFileInfo(false);for (int i = 0; i < CurrentCSData.CurrentSplitDatas.Count; i++){CurrentCSData.CurrentDatas[CurrentCSData.CurrentSplitDatas[i].line] =ReplaceStr(CurrentCSData.CurrentDatas[CurrentCSData.CurrentSplitDatas[i].line],CurrentCSData.CurrentSplitDatas[i].data,CurrentCSData.NewSplitDatas[i].data);}for (int i = 0; i < CurrentCSData.Datas.Count; i++){CurrentCSData.Datas[i] = CurrentCSData.CurrentDatas[i];}CurrentCSData.WriterData();AssetDatabase.Refresh();}if (GUILayout.Button("重新选择文件")){CurrentCSData = null;}GUILayout.EndHorizontal();GUILayout.EndVertical();}EditorGUILayout.Space();}List<LineLocation> lineLocations = new List<LineLocation>();public class LineLocation{public Vector2 FirstRectOffset;public Vector2 FirstRect;public Vector2 LastRectOffset;public Vector2 LastRect;}public void ReadFile(){GetAllFilesAndDertorys(modelPath, searchCharacterData);}/// <summary>/// 获得拖拽文件/// </summary>void DragFolder(){//鼠标位于当前窗口if (mouseOverWindow == this){//拖入窗口未松开鼠标if (Event.current.type == EventType.DragUpdated){DragAndDrop.visualMode = DragAndDropVisualMode.Generic;//改变鼠标外观// 判断区域if (modelRect.Contains(Event.current.mousePosition))GUI.FocusControl("input1");}//拖入窗口并松开鼠标else if (Event.current.type == EventType.DragExited){string dragPath = string.Join("", DragAndDrop.paths);// 判断区域if (modelRect.Contains(Event.current.mousePosition))modelPath = dragPath;// 取消焦点(不然GUI不会刷新)GUI.FocusControl(null);}}}static string FindString(string str, string StartStr, string EndStr){if (str.Length < 3)return "";int index3 = str.IndexOf(StartStr);if (index3 != -1){int index4 = str.IndexOf(EndStr, index3);if (index4 != -1){return str.Substring(index3 + StartStr.Length, index4 - index3 - StartStr.Length);}}return "";}void GetAllFilesAndDertorys(string _path, SearchCharacterData searchCharacterData){//判断路径是否存在if (Directory.Exists(_path)){#region 找到Editor文件夹DirectoryInfo dir = new DirectoryInfo(_path);DirectoryInfo[] allDirs = dir.GetDirectories("*", SearchOption.AllDirectories);string EditorPath = "";foreach (var item in allDirs){//忽略.metaif (item.Name.EndsWith(".meta")) continue;string assetsName = item.FullName;assetsName = assetsName.Substring(assetsName.IndexOf("Assets"));if (item.Name == "Editor"){EditorPath = assetsName;break;}}#endregionif (EditorPath != ""){#region 得到Editor文件夹下所有的.cs文件DirectoryInfo editorDir = new DirectoryInfo(EditorPath);cslist.Clear();int ListIndex = 0;foreach (var item in editorDir.GetFiles("*", SearchOption.AllDirectories)){//忽略.metastring assetsName = item.FullName;assetsName = assetsName.Substring(assetsName.IndexOf("Assets"));if (item.Name.EndsWith(".meta")) continue;if (item.Name.EndsWith(".cs")){cslist.Add(new CSData(ListIndex, item, assetsName, searchCharacterData, this));}ListIndex++;}#endregionforeach (var item in cslist){item.ReadFileInfo();}}else{Debug.LogError("该目录没有Editor文件夹");}}}string ReplaceStr(string str, string oldStr, string newStr){if (str.Length < 3)return "";int strIndex = str.IndexOf(oldStr);if (strIndex != -1){string startStr = str.Substring(0, strIndex);string endStr = str.Substring(strIndex + oldStr.Length);return startStr + newStr + endStr;}return "";}public float LimitMax(float f, float maxValue = 690, float minValue = 0){//return f;return Math.Min(maxValue, Math.Max(f, minValue));}[System.Serializable]public class CSData{Editor_ChinesizationTool editor_ChinesizationTool = null;SearchCharacterData searchCharacterData = null;public string name;private FileInfo fileInfo;public int ListIndex = -1;public string path;/// <summary>/// 原始数据/// </summary>public List<string> Datas = new List<string>();/// <summary>/// 当前数据/// </summary>public List<string> CurrentDatas = new List<string>();/// <summary>/// 新数据/// </summary>public List<NewCSData> CurrentSplitDatas = new List<NewCSData>();public List<NewCSData> NewSplitDatas = new List<NewCSData>();public FileInfo FileInfo{get{if (fileInfo == null){editor_ChinesizationTool.ReadFile();fileInfo = editor_ChinesizationTool.cslist[ListIndex].FileInfo;}return fileInfo;}set => fileInfo = value;}public CSData(int mListIndex, FileInfo mfileInfo, string path, SearchCharacterData searchCharacterData, Editor_ChinesizationTool parent){FileInfo = mfileInfo;this.path = path;this.name = FileInfo.Name;this.searchCharacterData = searchCharacterData;this.ListIndex = mListIndex;this.editor_ChinesizationTool = parent;}/// <summary>/// 写入数据/// </summary>public void WriterData(){WriteFileInfo(Datas);}/// <summary>/// 读取数据/// </summary>/// <returns></returns>public List<string> ReadFileInfo(bool IsUpdateNewData = true){Datas.Clear();CurrentDatas.Clear();CurrentSplitDatas.Clear();if (IsUpdateNewData) NewSplitDatas.Clear();StreamReader sr = null;//读取string assetsName = FileInfo.FullName;sr = File.OpenText(assetsName.Substring(assetsName.IndexOf("Assets")));//读取文件//读取所有行int line = 0;string data = null;do{data = sr.ReadLine();if (data != null){Datas.Add(data);CurrentDatas.Add(data);foreach (var item in searchCharacterData.items){string csData = FindString(data, item.startStr, item.endStr);if (csData != ""){CurrentSplitDatas.Add(new NewCSData(line, csData));if (IsUpdateNewData) NewSplitDatas.Add(new NewCSData(line, csData));break;}}}line++;} while (data != null);sr.Close();//关闭流sr.Dispose();//销毁流return CurrentDatas;}void WriteFileInfo(List<string> datas){StreamWriter sw;//写入if (!FileInfo.Exists){Debug.LogError("无法写入,没有该文件");return;}ClearData(path);sw = FileInfo.AppendText();//打开文件foreach (string linedata in datas){sw.WriteLine(linedata);}sw.Flush();//清除缓冲区sw.Close();//关闭流sw.Dispose();//销毁流//ReadFileInfo();}void ClearData(string path){StreamWriter tmpWrite = new StreamWriter(path);tmpWrite.Write("");tmpWrite.Close();}}[System.Serializable]public class NewCSData{public int line = 0;public string data = "";public NewCSData(int line, string data){this.line = line;this.data = data;}}
}

最后的最后:

我自己反正没实践过,可以先拿这个玩玩看还是挺有意思的~
觉得有意思可以改巴改巴,也可以把建议放在评论区,有空我就更新一下~
Demo源码

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/69999.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SQL Server如何新建作业

作业&#xff1a; 在 SQL Server 中&#xff0c;作业&#xff08;Job&#xff09;是一组可以在预定时间自动执行的任务。可以将作业看作是一个可以在后台运行的程序或脚本。作业由一系列步骤组成&#xff0c;每个步骤都是一个独立的任务&#xff0c;可以执行诸如执行 SQL 查询…

架构师如何做好需求分析

架构师如何做好需求分析 目录概述需求&#xff1a; 设计思路实现思路分析1.主要步骤 2.主要步骤2操作步骤 参考资料和推荐阅读 Survive by day and develop by night. talk for import biz , show your perfect code,full busy&#xff0c;skip hardness,make a better result,…

【已解决】使用xshell来ssh到vmware的虚拟机,请求超时的问题

我的情况&#xff1a; 1.本地ping虚拟机请求超时&#xff0c;但是虚拟机ping本地成功 2.本地和虚拟机的防火墙都关了&#xff0c;ssh服务也开了 3.端口也是正确的 百思不得其解&#xff0c;不知道为什么就是连接不上 当出现这种情况的时候&#xff0c;可以考虑一下vmware的…

【Java】Jxls--轻松生成 Excel

1、介绍 Jxls 是一个小型 Java 库&#xff0c;可以轻松生成 Excel 报告。Jxls 在 Excel 模板中使用特殊标记来定义输出格式和数据布局。 Java 有一些用于创建 Excel 文件的库&#xff0c;例如Apache POI。这些库都很好&#xff0c;但都是一些较底层的库&#xff0c;因为它们要…

Vue3【Provide/Inject】

前言 自从使用了Provide/Inject代码的组织方式更加灵活了&#xff0c;但是这个灵活性的增加伴随着代码容错性的降低。我相信只要是真的在项目中引入Provide/Inject的同学&#xff0c;一定一定有过或者正在经历下面的状况&#xff1a; 注入名&#xff08;Injection key&#x…

C++中虚继承时的构造函数

在虚继承中,虚基类是由最终的派生类初始化的,换句话说,最终派生类的构造函数必须要调用虚基类的构造函数。对最终的派生类来说,虚基类是间接基类,而不是直接基类。这跟普通继承不同,在普通继承中,派生类构造函数中只能调用直接基类的构造函数,不能调用间接基类的。 下面…

VBA技术资料MF51:VBA_在Excel中突出显示唯一值

【分享成果&#xff0c;随喜正能量】世间万物&#xff0c;因果循环不休&#xff0c;你的善心善行&#xff0c;都可能成为你的善缘善果。每天忆佛念佛&#xff0c;每天都在佛菩萨的加持下生活&#xff0c;自然吉祥如意&#xff0c;法喜充满。 。 我给VBA的定义&#xff1a;VBA是…

重磅| Falcon 180B 正式在 Hugging Face Hub 上发布!

引言 我们很高兴地宣布由 Technology Innovation Institute (TII) 训练的开源大模型 Falcon 180B 登陆 Hugging Face&#xff01; Falcon 180B 为开源大模型树立了全新的标杆。作为当前最大的开源大模型&#xff0c;有180B 参数并且是在在 3.5 万亿 token 的 TII RefinedWeb 数…

3D点云处理:点云投影为2D图像 调平点云(附源码)

文章目录 0. 测试效果1. 基本内容1.1 计算点云位姿1.2 调平点云1.3 点云投影2. 代码实现文章目录:3D视觉个人学习目录微信:dhlddxB站: Non-Stop_0. 测试效果

如何远程访问Linux MeterSphere一站式开源持续测试平台

文章目录 前言1. 安装MeterSphere2. 本地访问MeterSphere3. 安装 cpolar内网穿透软件4. 配置MeterSphere公网访问地址5. 公网远程访问MeterSphere6. 固定MeterSphere公网地址 前言 MeterSphere 是一站式开源持续测试平台, 涵盖测试跟踪、接口测试、UI 测试和性能测试等功能&am…

使用 WebGL 为 HTML5 游戏创建逼真的地形

推荐&#xff1a;使用 NSDT场景编辑器快速搭建3D应用场景 建 模 和 3D 地形 大多数 3D 对象是 使用建模工具创建&#xff0c;这是有充分理由的。创建复杂对象 &#xff08;如飞机甚至建筑物&#xff09;很难在代码中完成。建模工具 几乎总是有意义的&#xff0c;但也有例外&am…

深入浅出Android同步屏障机制

原文链接 Android Sync Barrier机制 诡异的假死问题 前段时间&#xff0c;项目上遇到了一个假死问题&#xff0c;随机出现&#xff0c;无固定复现规律&#xff0c;大量频繁随机操作后&#xff0c;便会出现假死&#xff0c;整个应用无法操作&#xff0c;不会响应事件&#xff…

ES6中导入import导出export

ES6使用 export 和 import 来导出、导入模块 用法 /** 导出 export *///分别导出 export let name 孙悟空; export function sum(a, b) {return a b; } } //先定义再导出 let age 18 export {age}/** 默认导出 export default */const a 默认导出; export default a;/**…

手写apply方法

<script>/** 手写apply方法 * */Function.prototype.myApply function (context, args) {console.log(this, sss)//fnconst key Symbol()context[key] thiscontext[key](...args)delete context[key]return context[key]}const obj {name: zs,age: 18}function fn …

$ref属性的介绍与使用

在Vue.js中&#xff0c;$ref是一个特殊的属性&#xff0c;用于访问Vue组件中的DOM元素或子组件实例。它允许你直接访问组件内部的DOM元素或子组件&#xff0c;并且可以在需要时进行操作或修改。以下是有关$ref的详细介绍和示例演示&#xff0c;给大家做一个简单的介绍和概念区分…

Leetcode 1486.数组异或操作

给你两个整数&#xff0c;n 和 start 。 数组 nums 定义为&#xff1a;nums[i] start 2*i&#xff08;下标从 0 开始&#xff09;且 n nums.length 。 请返回 nums 中所有元素按位异或&#xff08;XOR&#xff09;后得到的结果。 示例 1&#xff1a; 输入&#xff1a;n 5, …

YOLOv5:对yolov5n模型进一步剪枝压缩

YOLOv5&#xff1a;对yolov5n模型进一步剪枝压缩 前言前提条件相关介绍具体步骤修改yolov5n.yaml配置文件单通道数据&#xff08;黑白图片&#xff09;修改models/yolo.py文件修改train.py文件 剪枝后模型大小 参考 前言 由于本人水平有限&#xff0c;难免出现错漏&#xff0c;…

小程序中如何查看会员的积分和变更记录

​积分是会员卡的一个重要功能&#xff0c;可以用于激励会员消费和提升用户粘性。在小程序中&#xff0c;商家可以方便地查看会员卡的积分和变更记录&#xff0c;以便更好地了解会员的消费行为和积分变动情况。下面将介绍如何在小程序中查看会员卡的积分和变更记录。 1. 找到指…

Nat. Communications Biology2022 | PepNN+: 用于识别多肽结合位点的深度关注模型

论文标题&#xff1a;PepNN: a deep attention model for the identification of peptide binding sites 论文链接&#xff1a;PepNN: a deep attention model for the identification of peptide binding sites | Communications Biology 代码地址&#xff1a;oabdin / PepN…

【管理运筹学】第 7 章 | 图与网络分析(1,图论背景以及基本概念、术语、矩阵表示)

文章目录 引言一、图与网络的基本知识1.1 图与网络的基本概念1.1.1 图的定义1.1.2 图中相关术语1.1.3 一些特殊图类1.1.4 图的运算 1.2 图的矩阵表示1.2.1 邻接矩阵1.2.2 可达矩阵1.2.3 关联矩阵1.2.4 权矩阵 写在最后 引言 按照正常进度应该学习动态规划了&#xff0c;但我想…