麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > C# > 正文

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

2019-10-29 19:32:17
字體:
來源:轉載
供稿:網友

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

unity,腳本,模板,代碼

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

unity,腳本,模板,代碼

4、總結

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

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


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天堂精品在线 | 在线免费日韩 | 日韩精品dvd | 欧美在线黄色 | 色网站综合 | 日本不卡一区二区在线观看 | 成人短视频在线观看免费 | 羞羞的小视频 | 欧美日本国产精品 | 午夜神马福利视频 | 成人午夜网址 | 国产精品久久久久无码av | 中文字幕欧美日韩 | 成人午夜在线播放 | 久久艹精品 | 免费一区二区三区 | 色屁屁xxxxⅹ在线视频 | www国产成人免费观看视频,深夜成人网 | 欧美偷拍一区二区 | 国产在线精品一区二区不卡 | 亚洲成人在线免费观看 | 国产毛片网 | 99视频网| 色综合视频 | 精精国产xxxx视频在线野外 | 99sesese| 国产91成人| 久久精品99国产国产精 | 欧美精品亚洲人成在线观看 | qyl在线视频精品免费观看 | 久久17| 中文字幕在线视频网站 | av手机免费在线观看 | 国产成人aⅴ | 免费看a级片 | 最新一区二区三区 | 国产精品9191 | 国产精品美女久久久久久网站 | 中文字幕涩涩久久乱小说 | 免费视频a | 亚洲午夜影院在线观看 |