我们创建新的脚本的时候,为了方便 ,很多unity的程序员会去修改unity引擎的创建脚本的模版源码,这不失为一个非常方便的方法。但是弊端也是显而易见,每次升级unity版本的时候都要重新去改一次,太过麻烦,还有就是不利于自定义添加功能和修改。所以字考虑到这些因素之后 ,我决定自己写个拓展的编辑器小工具:
- 在Editor文件下创建个脚本AutoAddNamespace.cs,继承UnityEditor.AssetModificationProcessor ,并实现方法 public static void OnWillCreateAsset(string path)
- 其实 最重要的是获取到类名,这里使用正则匹配来获取到类名,这边正则匹配 为了防止出现误差,直接全部匹配整个unity创建的脚本类,来取得我们需要的类名。
- 我这边自己写了个脚本生成器类。
最后记得脚本一定要放在editor文件下具体代码如下:
//=======================================================
// 作者:Bowenk
// 描述:创建脚本自动添加命名空间
//=======================================================
using UnityEngine;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
namespace Babybus.Uno
{
public class AutoAddNamespace : UnityEditor.AssetModificationProcessor {
private static readonly string AuthorCode =
"//=======================================================\r\n"
+"// 作者:\r\n"
+"// 描述:\r\n"
+"//=======================================================\r\n";
public static readonly string headCode =
"using UnityEngine;\r\n"
+"using System.Collections;\r\n"
+"\r\n";
public static void OnWillCreateAsset(string path){
//只修改C#脚本
var scriptName = "";
path = path.Replace(".meta", "");
if(path.EndsWith(".cs")){
string allText = "";
allText += File.ReadAllText(path);
scriptName = GetClassName(allText);
if(scriptName != "")
{
CreateClass(path, scriptName);
}
}
}
//创建新的类
public static void CreateClass(string path, string className)
{
var sb = new ScriptBuilder();
//添加命名空间 UnoCfg.App.CodeName()->自己定义的项目代码字段
sb.WriteLine("namespace Bowenk." + FirstLetterUppercase(UnoCfg.App.CodeName()));
sb.WriteCurlyBrackets();
sb.Indent++;
sb.WriteLine("public class #SCRIPTNAME# : MonoBehaviour");
sb.WriteCurlyBrackets();
sb.Indent++;
var allText = AuthorCode + headCode + sb.ToString();
//替换脚本名字
allText = allText.Replace("#SCRIPTNAME#", className);
File.WriteAllText(path, allText);
}
//首字母改成大写
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 "";
}
}
}
还有就是使用到的一个脚本生成器
//=======================================================
// 作者:Snake
// 描述:自定义创建脚本生成器
//=======================================================
public class ScriptBuilder
{
private const string NEW_LINE = "\r\n";
public ScriptBuilder()
{
builder = new StringBuilder();
}
private StringBuilder builder;
public int Indent { get; set; }
private int currentCharIndex;
public void Write(string val, bool noAutoIndent = false)
{
if (!noAutoIndent)
val = GetIndents() + val;
if (currentCharIndex == builder.Length)
builder.Append(val);
else
builder.Insert(currentCharIndex, val);
currentCharIndex += val.Length;
}
public void WriteLine(string val, bool noAutoIndent = false)
{
Write(val + NEW_LINE);
}
public void WriteCurlyBrackets()
{
var openBracket = GetIndents() + "{" + NEW_LINE;
var closeBracket = GetIndents() + "}" + NEW_LINE;
Write(openBracket + closeBracket, true);
currentCharIndex -= closeBracket.Length;
}
public string GetIndents()
{
var str = "";
for (var i = 0; i < Indent; i++)
str += " ";
return str;
}
public override string ToString()
{
return builder.ToString();
}
}