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

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#SQLite在C#中的安裝與操作技巧

    SQLite在C#中的安裝與操作技巧

    SQLite,是一款輕型的數據庫,用于本地的數據儲存。其優點有很多,下面通過本文給大家介紹SQLite在C#中的安裝與操作技巧,感興趣的的朋友參考下吧...

    藍曈魅11162022-01-20
  • C#如何使用C#將Tensorflow訓練的.pb文件用在生產環境詳解

    如何使用C#將Tensorflow訓練的.pb文件用在生產環境詳解

    這篇文章主要給大家介紹了關于如何使用C#將Tensorflow訓練的.pb文件用在生產環境的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒...

    bbird201811792022-03-05
  • C#VS2012 程序打包部署圖文詳解

    VS2012 程序打包部署圖文詳解

    VS2012雖然沒有集成打包工具,但它為我們提供了下載的端口,需要我們手動安裝一個插件InstallShield。網上有很多第三方的打包工具,但為什么偏要使用微軟...

    張信秀7712021-12-15
  • C#C#微信公眾號與訂閱號接口開發示例代碼

    C#微信公眾號與訂閱號接口開發示例代碼

    這篇文章主要介紹了C#微信公眾號與訂閱號接口開發示例代碼,結合實例形式簡單分析了C#針對微信接口的調用與處理技巧,需要的朋友可以參考下...

    smartsmile20127762021-11-25
  • C#利用C#實現網絡爬蟲

    利用C#實現網絡爬蟲

    這篇文章主要介紹了利用C#實現網絡爬蟲,完整的介紹了C#實現網絡爬蟲詳細過程,感興趣的小伙伴們可以參考一下...

    C#教程網11852021-11-16
  • C#深入理解C#的數組

    深入理解C#的數組

    本篇文章主要介紹了C#的數組,數組是一種數據結構,詳細的介紹了數組的聲明和訪問等,有興趣的可以了解一下。...

    佳園9492021-12-10
  • C#C#設計模式之Strategy策略模式解決007大破密碼危機問題示例

    C#設計模式之Strategy策略模式解決007大破密碼危機問題示例

    這篇文章主要介紹了C#設計模式之Strategy策略模式解決007大破密碼危機問題,簡單描述了策略模式的定義并結合加密解密算法實例分析了C#策略模式的具體使用...

    GhostRider10972022-01-21
  • C#三十分鐘快速掌握C# 6.0知識點

    三十分鐘快速掌握C# 6.0知識點

    這篇文章主要介紹了C# 6.0的相關知識點,文中介紹的非常詳細,通過這篇文字可以讓大家在三十分鐘內快速的掌握C# 6.0,需要的朋友可以參考借鑒,下面來...

    雨夜瀟湘8272021-12-28
欧美日韩色另类综合|亚洲中文字幕无码一区|99国产真实露脸精彩对白|d专干日本老太婆|欧美狂野可乐视频在线观看