1、unity的脚本模板
新版本unity中的c#脚本有三类,第一类是我们平时开发用的c# script;第二类是testing,用来做单元测试;第三类是playables,用作timeline中管理时间线上每一帧的动画、声音等。我们点击创建脚本时,会自动生成unity内置的一套模板:
using system.collections;
using system.collections.generic;
using unityengine;
public class newbehaviourscript : monobehaviour {
// use this for initialization
void start () {
}
// update is called once per frame
void update () {
}
}
如果我们开发时使用的框架有明显的一套基础模板, 那为项目框架定制一套模板会很有意义,这样可以为我们省去编写重复代码的时间。这里介绍两种方法。
2、修改默认脚本模板
打开unity安装目录,比如d:\unity2018\editor\data\resources\scripttemplates,unity内置的模板脚本都在这里,那么可以直接修改这里的cs文件,比如我们将81-c# script-newbehaviourscript.cs.txt文件修改为如下,那下次创建c# script时模板就会变成这样:
////////////////////////////////////////////////////////////////////
// _ooooo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// o\ = /o //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永不宕机 永无bug //
////////////////////////////////////////////////////////////////////
using system.collections;
using system.collections.generic;
using unityengine;
public class #scriptname# : monobehaviour {
// use this for initialization
void start () {
#notrim#
}
// update is called once per frame
void update () {
#notrim#
}
}
3、拓展脚本模板
上面讲的第一种方法直接修改了unity的默认配置,这并不适应于所有项目,这里第二种方法会更有效,可以针对不同的项目和框架创建合适的脚本模板。
首先,先创建一个文本文件mytemplatescript.cs.txt作为脚本模板,并将其放入unity project的editor文件夹下,模板代码为:
using system.collections;
using system.collections.generic;
using unityengine;
public class mynewbehaviourscript : monobase {
//添加事件监听
protected override void addmsglistener()
{
}
//处理消息
protected override void handlemsg(msgbase msg)
{
switch (msg.id)
{
default :
break ;
}
}
}
我们使用时,需要在project视图中右击->create->c# framescript 创建脚本模板,因此首先要创建路径为assets/create/c# framescript的menuitem,点击创建脚本后,需要修改脚本名字,因此需要在拓展编辑器脚本中继承endnameeditaction来监听回调,最终实现输入脚本名字后自动创建相应的脚本模板。
代码如下,将这个脚本放入editor文件夹中:
using unityeditor;
using unityengine;
using system;
using system.io;
using unityeditor.projectwindowcallback;
using system.text;
using system.text.regularexpressions;
public class createtemplatescript {
//脚本模板路径
private const string templatescriptpath = "assets/editor/mytemplatescript.cs.txt" ;
//菜单项
[menuitem( "assets/create/c# framescript" , false , 1)]
static void createscript()
{
string path = "assets" ;
foreach (unityengine. object item in selection.getfiltered( typeof (unityengine. object ),selectionmode.assets))
{
path = assetdatabase.getassetpath(item);
if (! string .isnullorempty(path) && file.exists(path))
{
path = path.getdirectoryname(path);
break ;
}
}
projectwindowutil.startnameeditingifprojectwindowexists(0, scriptableobject.createinstance<createscriptasset>(),
path + "/mynewbehaviourscript.cs" ,
null , templatescriptpath);
}
}
class createscriptasset : endnameeditaction
{
public override void action( int instanceid, string newscriptpath, string templatepath)
{
unityengine. object obj= createtemplatescriptasset(newscriptpath, templatepath);
projectwindowutil.showcreatedasset(obj);
}
public static unityengine. object createtemplatescriptasset( string newscriptpath, string templatepath)
{
string fullpath = path.getfullpath(newscriptpath);
streamreader streamreader = new streamreader(templatepath);
string text = streamreader.readtoend();
streamreader.close();
string filenamewithoutextension = path.getfilenamewithoutextension(newscriptpath);
//替换模板的文件名
text = regex.replace(text, "mytemplatescript" , filenamewithoutextension);
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(newscriptpath);
return assetdatabase.loadassetatpath(newscriptpath, typeof (unityengine. object ));
}
}
然后,在project中,点击创建c# framescript,输入脚本名字,对应的脚本就已经创建好了
4、总结
上面介绍了两种方案,第一种适合玩玩,第二种方法显然逼格高一些,为不同的项目和框架定制一套脚本模板,可以让我们少写一些重复代码。按照上面介绍的方法,我们同样可以修改和拓展testing、playables的脚本模板,甚至shader,我们也可以定制模板。
好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。
原文链接:http://HdhCmsTestcnblogs测试数据/IAMTOM/p/10156148.html
dy("nrwz");
查看更多关于unity学习教程之定制脚本模板示例代码的详细内容...