一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C# - unity學習教程之定制腳本模板示例代碼

unity學習教程之定制腳本模板示例代碼

2022-03-07 13:26禹澤鵬鵬 C#

這篇文章主要給大家介紹了關于unity學習教程之定制腳本模板的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

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來監聽回調,最終實現輸入腳本名字后自動創建相應的腳本模板。

unity學習教程之定制腳本模板示例代碼

代碼如下,將這個腳本放入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,輸入腳本名字,對應的腳本就已經創建好了

unity學習教程之定制腳本模板示例代碼

4、總結

上面介紹了兩種方案,第一種適合玩玩,第二種方法顯然逼格高一些,為不同的項目和框架定制一套腳本模板,可以讓我們少寫一些重復代碼。按照上面介紹的方法,我們同樣可以修改和拓展testing、playables的腳本模板,甚至shader,我們也可以定制模板。

好了,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:http://www.cnblogs.com/IAMTOM/p/10156148.html

延伸 · 閱讀

精彩推薦
  • C#C#裁剪,縮放,清晰度,水印處理操作示例

    C#裁剪,縮放,清晰度,水印處理操作示例

    這篇文章主要為大家詳細介紹了C#裁剪,縮放,清晰度,水印處理操作示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    吳 劍8332021-12-08
  • C#C#實現XML文件讀取

    C#實現XML文件讀取

    這篇文章主要為大家詳細介紹了C#實現XML文件讀取的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Just_for_Myself6702022-02-22
  • C#C#通過KD樹進行距離最近點的查找

    C#通過KD樹進行距離最近點的查找

    這篇文章主要為大家詳細介紹了C#通過KD樹進行距離最近點的查找,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    帆帆帆6112022-01-22
  • C#C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    這篇文章主要介紹了C# 實現對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下...

    E-iceblue5012022-02-12
  • C#深入解析C#中的交錯數組與隱式類型的數組

    深入解析C#中的交錯數組與隱式類型的數組

    這篇文章主要介紹了深入解析C#中的交錯數組與隱式類型的數組,隱式類型的數組通常與匿名類型以及對象初始值設定項和集合初始值設定項一起使用,需要的...

    C#教程網6172021-11-09
  • C#C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長...

    GhostRider9502022-01-21
  • C#WPF 自定義雷達圖開發實例教程

    WPF 自定義雷達圖開發實例教程

    這篇文章主要介紹了WPF 自定義雷達圖開發實例教程,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下...

    WinterFish13112021-12-06
  • C#Unity3D實現虛擬按鈕控制人物移動效果

    Unity3D實現虛擬按鈕控制人物移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現虛擬按鈕控制人物移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一...

    shenqingyu060520232410972022-03-11
主站蜘蛛池模板: 日本aaaaa高清免费看 | 8x8x拔插| 亚洲性色永久网址 | 天天爱综合网 | 日韩欧美国产在线 | 色多多在线视频 | 日本伦理动漫在线观看 | 91寡妇天天综合久久影院 | 九九99热 | 欧美理论片手机在线观看片免费 | 糖心hd在线观看 | 无码国产成人777爽死在线观看 | 国内精品视频九九九九 | 欧美在线视频 一区二区 | 国产欧美日韩在线不卡第一页 | 美女又爽又黄免费 | 思思玖玖玖在线精品视频 | 香蕉国产精品偷在线播放 | 办公室操秘书 | 2020年最新国产精品视频免费 | 日韩一卡2卡3卡新区网站 | 日日摸日日碰夜夜爽97纠 | 亚洲天堂2013 | 5x视频在线观看 | 69日本人xxxxxxxx色| 青草福利在线 | 苍井空色欲迷墙 | 湖南美女被黑人4p到惨叫 | 久久囯产精品777蜜桃传媒 | 精品国产mmd在线观看 | 免费黄色网站视频 | 毛片免费在线视频 | 无套插入 | 亚洲天天做夜夜做天天欢 | 精品老司机在线视频香蕉 | 天天gan| 风间由美一区二区av101 | 亚洲风情无码免费视频 | 亚洲精品短视频 | 国产99页| yellow在线 |