100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Unity编辑器 - 自定义脚本模板

Unity编辑器 - 自定义脚本模板

时间:2023-09-19 02:32:47

相关推荐

Unity编辑器 - 自定义脚本模板

Unity编辑器 - 自定义脚本模板

Unity编辑器的脚本模板

在Unity编辑器的脚本模板文件夹中添加自己的模板,重启编辑器后会自动识别并将可用模板显示在Assets/Create中(在Project中的鼠标右键菜单)

注:添加自定义模板后必须重启编辑器才可生效

模板文件位置: 编辑器安装位置\Editor\Data\Resources\ScriptTemplates

模板文件命名格式说明:(以 81-Script__MonoBehaviour-NewBehaviour.cs.txt 为例)

模板中常见关键字说明:

若将脚本模板直接放置在编辑器安装位置\Editor\Data\Resources\ScriptTemplates,在升级编辑器时就需要对新安装的编辑器重复上述操作,对此可直接将脚本模板(以同样的命名规则)保存在 Assets/ScriptTemplates 文件夹中,重启Unity编辑器后编辑器也会自动识别并在右键菜单中显示;再将Assets/ScriptTemplates 文件夹导出成.unitypackage 即可在其他项目中间进行使用。

MenuItem

先准备好脚本模板ScriptTemplate.cs.txt,放置在Assets/ScriptTemplates文件夹中

/*┌────────────────────────────┐│Description:│Author:│Remark:可在自定义的脚本模板中按自身需求添加版权信息、脚本说明等└────────────────────────────┘┌──────────────┐│ClassName:#SCRIPTNAME#└──────────────┘*/using System.Collections;using System.Collections.Generic;using UnityEngine;#ROOTNAMESPACEBEGIN#public class #SCRIPTNAME# : MonoBehaviour{}#ROOTNAMESPACEEND#

在Assets/Scripts/Editor文件夹中添加编辑器脚本 CreateMyScript.cs

using System.IO;using System.Text.RegularExpressions;using System.Text;using UnityEditor;using UnityEditor.ProjectWindowCallback;using UnityEngine;public static class CreateScript{[MenuItem("Assets/Create/My Script", false, 80)]public static void CreatNewLua(){ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,ScriptableObject.CreateInstance<CreateScriptAsset>(),GetSelectedPathOrFallback() + "/NewScript.cs",null,"Assets/ScriptTemplates/ScriptTemplate.cs.txt");}public static string GetSelectedPathOrFallback(){string path = "Assets";foreach (Object obj in Selection.GetFiltered(typeof(Object), SelectionMode.Assets)){path = AssetDatabase.GetAssetPath(obj);if (!string.IsNullOrEmpty(path) && File.Exists(path)){path = Path.GetDirectoryName(path);break;}}return path;}class CreateScriptAsset : EndNameEditAction{public override void Action(int instanceId, string pathName, string resourceFile){UnityEngine.Object o = CreateScriptAssetFromTemplate(pathName, resourceFile);ProjectWindowUtil.ShowCreatedAsset(o);}internal static Object CreateScriptAssetFromTemplate(string pathName, string resourceFile){string fullPath = Path.GetFullPath(pathName);StreamReader streamReader = new StreamReader(resourceFile);string text = streamReader.ReadToEnd();streamReader.Close();string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathName);text = Regex.Replace(text, "#SCRIPTNAME#", fileNameWithoutExtension);text = Regex.Replace(text, "#ROOTNAMESPACEBEGIN#", string.Empty);text = Regex.Replace(text, "#ROOTNAMESPACEEND#", string.Empty);bool encoderShouldEmitUTF8Identifier = true;bool throwOnInvalidBytes = false;UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes);bool append = false;StreamWriter streamWriter = new StreamWriter(fullPath, append, encoding);streamWriter.Write(text);streamWriter.Close();AssetDatabase.ImportAsset(pathName);return AssetDatabase.LoadAssetAtPath(pathName, typeof(Object));}}}

参考资料

/archives/3732

/hubertgdev/unity-script-templates

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。