实现Unity自定义创建C#脚本,和Unity自己的创建方式一样的,修改Unity的模板的方式,只能修改自己Unity,他人的PC相同的项目也不会生效。今天写一个在Unity工程里的适用的方法。随着工程,不魔改Unity编辑器安装目录下的模板。
例如 模板内容:
//****************************************************************************** // * Copyright © 2022-2023 这里自定义修改.All rights reserved. // *------------------------------------------------------------------------ // * 文件:ClassName.cs // * 作者:Author // * 日期:Created by XXXX on Time // * 项目:XXXX项目 // * 功能:Nothing //*******************************************************************************/ using System.Collections; using System.Collections.Generic; using UnityEngine; public class ClassName : MonoBehaviour { // "Start"在第一帧更新之前被调用。 void Start() { } // "Update"在每一帧更新时被调用。 void Update() { } }
脚本:
//**************************************************** // * Copyright © 2022-2023 这里自定义修改.All rights reserved. // *------------------------------------------------------------------------ // * 文件:CreateCSharpTemplate // * 作者:wangyin // * 日期:Created by 2023/3/7 // * 功能:C#脚本创建模板 // *****************************************************/ using UnityEngine; using UnityEditor.ProjectWindowCallback; using System.IO; using UnityEditor; using System.Diagnostics; public class CreateCSharpTemplate { //文件夹前用特殊符号 、空格 或者数字开头,显示在最上面 [MenuItem("Assets/ Create C# Script", false, 1)] public static void CreateCSharpScript() { //创建C# 类 ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance<CreateCSharpTemplateAction>(), "NewCSharpScript.cs", null, "Assets"); } //创建C# 类 private static void CreateNewCSharpScript(string path, string fileName) { string templatePath = "Assets/Debug/WangYin/Test/Editor/Templates/MonoBehaviourTemplate.cs.txt"; // 获取当前Git用户名 string userName = ExecuteGitCommand(); string currentTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string templateContent = File.ReadAllText(templatePath); //templateContent = string.Format(templateContent, fileName, userName, currentTime, fileName); templateContent = templateContent.Replace("ClassName", fileName); templateContent = templateContent.Replace("Author", userName); templateContent = templateContent.Replace("Time", currentTime); File.WriteAllText(path, templateContent); AssetDatabase.Refresh(); } //用CMD获取GIt里的用户名 static string ExecuteGitCommand() { Process process = new Process(); process.StartInfo.FileName = "git"; process.StartInfo.Arguments = "config user.name"; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.Start(); string output = process.StandardOutput.ReadToEnd(); process.WaitForExit(); return output.Trim(); } //创建C#类 private class CreateCSharpTemplateAction : EndNameEditAction { public override void Action(int instanceId, string pathName, string resourceFile) { string fileName = Path.GetFileNameWithoutExtension(pathName); CreateNewCSharpScript(pathName, fileName); } } }
脚本文件和模板文件放到工程里的Editor下。
比如 模板文件路径:
Assets/Debug/WangYin/Test/Editor/Templates/MonoBehaviourTemplate.cs.txt
代码文件路径:
Assets/Debug/WangYin/Test/Editor/CreateCSharpTemplate.cs
使用上述的方式创建即可,或者直接到代码里,如下:
//**************************************************** // * Copyright © 2022-2023 这里自定义修改.All rights reserved. // *------------------------------------------------------------------------ // * 文件:CreateLuaTemplate // * 作者:wangyin // * 日期:Created by 2023/3/28 // * 功能:Lua脚本创建模板 // *****************************************************/ using UnityEngine; using UnityEditor.ProjectWindowCallback; using System.IO; using UnityEditor; using System.Diagnostics; using System.Text; public class CreateLuaTemplate { //文件夹前用特殊符号 、空格 或者数字开头,显示在最上面 [MenuItem("Assets/ CreateLuaScript/ClassScript", false, 1)] public static void CreateNewLuaScript() { //创建Lua结构类 ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance<CreateLuaTemplateAction>(), "NewLuaScript.lua", null, "Assets"); } //创建Lua结构类 private static void CreateNewLuaScript(string path, string fileName) { // 获取当前Git用户名 string userName = ExecuteGitCommand(); string currentTime = System.DateTime.Now.ToString("yyyy/MM/dd"); string content = $"--{fileName}.lua\n" + $"--{userName}\n" + $"--{currentTime}\n" + $"--des: {fileName} 基础结构\n" + "----------------------------------------" + "\n" + "-- 定义类" + "\n" + $"local {fileName} = Class(\"{fileName}\")\n\n" + $"{fileName} = {{}}\n" + $"{fileName}.__index = {fileName} -- 指定元表中的__index元素,用于实现类的继承\n\n" + "-- 定义构造函数" + "\n" + "-- newinfo, target为模板数据,不使用删除即可" + "\n" + $"function {fileName}:New(newinfo, target)\n" + " local o = {Anchor = newinfo, FollowTarget = target}" + "\n" + " local instance = setmetatable(o, self) -- 使用setmetatable创建一个实例,并指定元表为self" + "\n" + " return instance" + "\n" + "end" + "\n\n" + $"return {fileName}"; File.WriteAllText(path, content); AssetDatabase.Refresh(); } //用CMD获取GIt里的用户名 static string ExecuteGitCommand() { Process process = new Process(); process.StartInfo.FileName = "git"; process.StartInfo.Arguments = "config user.name"; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.Start(); string output = process.StandardOutput.ReadToEnd(); process.WaitForExit(); return output.Trim(); } //创建Lua结构类 private class CreateLuaTemplateAction : EndNameEditAction { public override void Action(int instanceId, string pathName, string resourceFile) { string fileName = Path.GetFileNameWithoutExtension(pathName); CreateNewLuaScript(pathName, fileName); } } }
入口:在Project面板鼠标右击,点击CreateC#Script 或者CreateLuaScript 即可