SciptBuildHelp类
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace CustomTool
{
public class SciptBuildHelp
{
private StringBuilder _stringBuilder;
private string _lineBreak = "\r\n";
public int IndentTimes { get; set; }
private int currentIndex = 0;
public override string ToString()
{
return _stringBuilder.ToString();
}
public SciptBuildHelp()
{
_stringBuilder=new StringBuilder();
}
public void WriteUsing(string nameSpaceName)
{
WriteLine("using " + nameSpaceName + ";");
}
public void WriteNameSpace(string name)
{
Write("namespace " + name);
int length= WriteCurlyBrackets();
currentIndex -= length;
}
public void WriteClass(string name)
{
Write("public class " + name + " : MonoBehaviour ", true);
int length= WriteCurlyBrackets();
currentIndex -= length;
}
public void WriteFun(string name, params string[] paraName)
{
StringBuilder temp = new StringBuilder();
temp.Append("public void " + name + "()");
if (paraName.Length > 0)
{
foreach (string s in paraName)
{
temp.Insert(temp.Length - 1, s + ",");
}
temp.Remove(temp.Length - 2, 1);
}
Write(temp.ToString(), true);
int length= WriteCurlyBrackets();
currentIndex -= length;
}
public void Write(string context,bool needIndent=false)
{
if(needIndent)
{
context = GetIndent() + context;
}
if (currentIndex == _stringBuilder.Length)
_stringBuilder.Append(context);
else
{
_stringBuilder.Insert(currentIndex, context);
}
currentIndex += context.Length;
}
public void WriteLine(string context, bool needIndent=false)
{
Write(context+_lineBreak,needIndent);
}
public int WriteCurlyBrackets()
{
var start =_lineBreak+ GetIndent() + "{" + _lineBreak;
var end =_lineBreak+ GetIndent() + "}" + _lineBreak;
Write(start + end, true);
return end.Length;
}
public string GetIndent()
{
string indent = "";
for(int i=0;i<IndentTimes;i++)
{
indent += " ";
}
return indent;
}
}
}
AutoAddNameSpace
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEngine;
namespace CustomTool
{
public class AutoAddNameSpace : UnityEditor.AssetModificationProcessor
{
private static void OnWillCreateAsset (string path)
{
path = path.Replace(".meta", "");
if(path.EndsWith(".cs"))
{
string text = "";
text += File.ReadAllText(path);
var newText= GetNewScript(GetClassName(text));
File.WriteAllText(path, newText);
}
}
//更新脚本内容
private static string GetNewScript(string classname)
{
var script = new SciptBuildHelp();
script.WriteUsing("UnityEngine");
script.WriteNameSpace("UIFrame");
script.IndentTimes++;
script.WriteClass(classname);
script.IndentTimes++;
script.WriteFun("Start");
return script.ToString();
}
/*获取类名*/
private static string GetClassName(string text)
{
/* string[] data=text.Split(' ');
int index = 0;
for(int i=0; i<data.Length; i++)
{
if(data[i].Contains("class"))
{
index += 1;
break;
}
}
if(data[index].Contains(":"))
{
return data[index].Split(':')[0];
}
else
{
return data[index];
}*/
string pattern ="public class ([A-Za-z0-9_]+)\\s*:\\s*MonoBehaviour" ;
var match=Regex.Match(text, pattern);
if(match.Success)
{
return match.Groups[1].Value;
}
return "";
}
}
}