Unity ScenarioTemplate脚本C#创建注释模板
2023-09-11 14:19:57 时间
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace Epitome
{
/**********************************************************************************************
** C# script template class. Customizable template style. When creating a new C# script, the
** original content is overwritten by a custom template.
*/
public class ScenarioTemplate : UnityEditor.AssetModificationProcessor
{
private static readonly string copyright = "E P I T O M E S T U D I O S";
private static readonly string authorName = "Ji Qiu .Yang";
private static readonly string nameSpace = "Epitome";
private static readonly string headerCode =
"/* $Header: #FILEPATH# #VERSION# #CREATETIME# #CREATORNAME# #UnityVersion# $ */\r\n";
private static readonly string authorCode =
"/***********************************************************************************************\r\n"
+ " *** C O N F I D E N T I A L --- #COPYRIGHT#***\r\n"
+ " ***********************************************************************************************\r\n"
+ " * *\r\n"
+ " * Project Name : #PROJECTNAME#*\r\n"
+ " * *\r\n"
+ " * File Name : #FILENAME#*\r\n"
+ " * *\r\n"
+ " * Programmer : #AUTHORNAME#*\r\n"
+ " * *\r\n"
+ " * Start Date : #CREATEDATE#*\r\n"
+ " * *\r\n"
+ " * Last Update : #CREATEDATE#*\r\n"
+ " * *\r\n"
+ " *---------------------------------------------------------------------------------------------*\r\n"
+ " * Functions: *\r\n"
+ " * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */\r\n";
public static readonly string usingCode =
"using UnityEngine;\r\n"
+ "using System.Collections;\r\n"
+ "using System.Collections.Generic;\r\n"
+ "\r\n";
public static void OnWillCreateAsset(string path)
{
path = path.Replace(".meta", "");
if (path.EndsWith(".cs"))
{
string allText = "";
allText += File.ReadAllText(path);
string scriptName = GetClassName(allText);
if (scriptName != "")
{
CreateClass(path, scriptName);
AssetDatabase.Refresh();
}
}
}
//创建新的类
public static void CreateClass(string path, string className)
{
var sb = new ScriptBuilder();
if (nameSpace != null)
{
sb.WriteLine("namespace #NAMESPACE#");
sb.WriteCurlyBrackets();
sb.Indent++;
}
sb.WriteLine("public class #SCRIPTNAME# : MonoBehaviour");
sb.WriteCurlyBrackets();
sb.Indent++;
var allText = headerCode + authorCode + usingCode + sb.ToString();
// 替换Header数据
allText = allText.Replace("#FILEPATH#", path);
allText = allText.Replace("#VERSION#", "1.0");
allText = allText.Replace("#CREATETIME#", DateTime.Now.ToString("yyyy/MM/dd dddd tt hh:mm:ss"));
allText = allText.Replace("#CREATORNAME#", authorName);
allText = allText.Replace("#UnityVersion#", Application.unityVersion);
// 替换文件注释
allText = allText.Replace("#COPYRIGHT#", string.Format("{0,-45}", copyright));
allText = allText.Replace("#PROJECTNAME#", string.Format("{0,-61}", PlayerSettings.productName));
allText = allText.Replace("#FILENAME#", string.Format("{0,-61}", className + path.Substring(path.LastIndexOf("."))));
allText = allText.Replace("#AUTHORNAME#", string.Format("{0,-61}", authorName));
allText = allText.Replace("#CREATEDATE#", string.Format("{0,-61}", DateTime.Now.ToString("yyyy/MM/dd")));
// 替换代码数据
allText = allText.Replace("#SCRIPTNAME#", className);
allText = allText.Replace("#NAMESPACE#", nameSpace);
File.WriteAllText(path, allText, Encoding.UTF8);
}
//首字母改成大写
public static string FirstLetterUppercase(string str)
{
if (string.IsNullOrEmpty(str))
return str;
if (str.Length == 1)
return str.ToUpper();
var first = str[0];
var rest = str.Substring(1);
return first.ToString().ToUpper() + rest;
}
//获取unity自动创建的脚本类名
public static string GetClassName(string allText)
{
var patterm = "public class ([A-Za-z0-9_]+)\\s*:\\s*MonoBehaviour {\\s*\\/\\/ Use this for initialization\\s*void Start \\(\\) {\\s*}\\s*\\/\\/ Update is called once per frame\\s*void Update \\(\\) {\\s*}\\s*}";
var match = Regex.Match(allText, patterm);
if (match.Success)
{
return match.Groups[1].Value;
}
return "";
}
}
}
相关文章
- c#中@标志的作用 C#通过序列化实现深表复制 细说并发编程-TPL 大数据量下DataTable To List效率对比 【转载】C#工具类:实现文件操作File的工具类 异步多线程 Async .net 多线程 Thread ThreadPool Task .Net 反射学习
- c# 中内部类的简单介绍 C#内部类
- C#中缓存的使用 ajax请求基于restFul的WebApi(post、get、delete、put) 让 .NET 更方便的导入导出 Excel .net core api +swagger(一个简单的入门demo 使用codefirst+mysql) C# 位运算详解 c# 交错数组 c# 数组协变 C# 添加Excel表单控件(Form Controls) C#串口通信程序
- C#7.2——编写安全高效的C#代码 c# 中模拟一个模式匹配及匹配值抽取 走进 LINQ 的世界 移除Excel工作表密码保护小工具含C#源代码 腾讯QQ会员中心g_tk32算法【C#版】
- 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq
- 腾讯云图片鉴黄集成到C# SQL Server 怎么在分页获取数据的同时获取到总记录数 sqlserver 操作数据表语句模板 .NET MVC后台发送post请求 百度api查询多个地址的经纬度的问题 try{}里有一个 return 语句,那么紧跟在这个 try 后的 finally {}里的 code 会 不会被执行,什么时候被执行,在 return 前还是后? js获取某个日期
- [C#基础]c#中的BeginInvoke和EndEndInvoke
- C# 字符串拼接性能探索 c#中+、string.Concat、string.Format、StringBuilder.Append四种方式进行字符串拼接时的性能
- Word控件Spire.Doc 【文档操作】教程(九):如何在 C# 中向 Word 文档添加内容控件
- Word控件Spire.Doc 转换教程(十六):在 C# 中将 Word 转换为 PDF/A
- Word处理控件Aspose.Words功能演示:使用 C# 将 DOC 转换为 HTML
- 【Bug】C# IQueryable里的元素更改不了值
- C#【时间操作类】使用TimeSpan计算时间差
- C#【基础篇】StringBuilder的使用
- C#,煎饼排序问题(Pancake Sorting Problem)算法与源代码
- C# List<T> 转 DataTable
- C# winform开发的考试系统
- C#使用Aspose.Words操作word文档(利用模板2)
- C#创建唯一的订单号, 考虑时间因素
- c# 类嵌套
- C# linq 使用Groupby lamda 获取非重复数据
- C# WinForm 修改DataGridView控件的行高
- C#.Net下的防抖-Debounce和节流阀-Throttle功能实现
- 【C#】AddHandler可重复多次添加事件处理器的问题
- C# 线程