1、unity的腳本模板
新版本unity中的c#腳本有三類,第一類是我們平時開發用的c# script;第二類是testing,用來做單元測試;第三類是playables,用作timeline中管理時間線上每一幀的動畫、聲音等。我們點擊創建腳本時,會自動生成unity內置的一套模板:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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時模板就會變成這樣:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
//////////////////////////////////////////////////////////////////// // _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文件夾下,模板代碼為:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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文件夾中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
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://www.cnblogs.com/IAMTOM/p/10156148.html