unity學(xué)習(xí)教程之定制腳本模板示例代碼
1、unity的腳本模板
新版本unity中的C#腳本有三類,第一類是我們平時開發(fā)用的C# Script;第二類是Testing,用來做單元測試;第三類是Playables,用作TimeLine中管理時間線上每一幀的動畫、聲音等。我們點擊創(chuàng)建腳本時,會自動生成unity內(nèi)置的一套模板:
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 () {
  
 }
}
如果我們開發(fā)時使用的框架有明顯的一套基礎(chǔ)模板, 那為項目框架定制一套模板會很有意義,這樣可以為我們省去編寫重復(fù)代碼的時間。這里介紹兩種方法。
2、修改默認(rèn)腳本模板
打開unity安裝目錄,比如D:\unity2018\Editor\Data\Resources\ScriptTemplates,unity內(nèi)置的模板腳本都在這里,那么可以直接修改這里的cs文件,比如我們將81-C# Script-NewBehaviourScript.cs.txt文件修改為如下,那下次創(chuàng)建C# Script時模板就會變成這樣:
////////////////////////////////////////////////////////////////////
//       _ooOoo_        //
//       o8888888o        //
//       88" . "88        //
//       (| ^_^ |)        //
//       O\ = /O        //
//      ____/`---'\____       //
//     .' \\|  |// `.       //
//     / \\||| : |||// \      //
//     / _||||| -:- |||||- \      //
//     | | \\\ - /// | |      //
//     | \_| ''\---/'' | |      //
//     \ .-\__ `-` ___/-. /      //
//    ___`. .' /--.--\ `. . ___      //
//    ."" '< `.___\_<|>_/___.' >'"".     //
//   | | : `- \`.;`\ _ /`;.`/ - ` : | |     //
//   \ \ `-. \_ __\ /__ _/ .-` / /     //
//  ========`-.____`-.___\_____/___.-`____.-'========   //
//       `=---='        //
//  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  //
//   佛祖保佑  永不宕機(jī)  永無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的默認(rèn)配置,這并不適應(yīng)于所有項目,這里第二種方法會更有效,可以針對不同的項目和框架創(chuàng)建合適的腳本模板。
首先,先創(chuàng)建一個文本文件MyTemplateScript.cs.txt作為腳本模板,并將其放入unity project的Editor文件夾下,模板代碼為:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyNewBehaviourScript : MonoBase {
 //添加事件監(jiān)聽
 protected override void AddMsgListener()
 {
 }
 
 //處理消息
 protected override void HandleMsg(MsgBase msg)
 {
  switch (msg.id)
  {
   default:
    break;
  }
 }
}
我們使用時,需要在Project視圖中右擊->Create->C# FrameScript 創(chuàng)建腳本模板,因此首先要創(chuàng)建路徑為Assets/Create/C# FrameScript的MenuItem,點擊創(chuàng)建腳本后,需要修改腳本名字,因此需要在拓展編輯器腳本中繼承EndNameEditAction來監(jiān)聽回調(diào),最終實現(xiàn)輸入腳本名字后自動創(chuàng)建相應(yīng)的腳本模板。
代碼如下,將這個腳本放入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中,點擊創(chuàng)建C# FrameScript,輸入腳本名字,對應(yīng)的腳本就已經(jīng)創(chuàng)建好了
4、總結(jié)
上面介紹了兩種方案,第一種適合玩玩,第二種方法顯然逼格高一些,為不同的項目和框架定制一套腳本模板,可以讓我們少寫一些重復(fù)代碼。按照上面介紹的方法,我們同樣可以修改和拓展Testing、Playables的腳本模板,甚至shader,我們也可以定制模板。
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對我們的支持。
上一篇:Ruby創(chuàng)建數(shù)組方法總結(jié)
欄 目:C#教程
下一篇:C#獲取所有進(jìn)程的方法
本文標(biāo)題:unity學(xué)習(xí)教程之定制腳本模板示例代碼
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4961.html
您可能感興趣的文章
- 01-10C#一個簡單的定時小程序?qū)崿F(xiàn)代碼
 - 01-10微信開放平臺之網(wǎng)站授權(quán)微信登錄功能
 - 01-10C#編程自學(xué)之?dāng)?shù)據(jù)類型和變量二
 - 01-10C#編程自學(xué)之開篇介紹
 - 01-10C#編程自學(xué)之?dāng)?shù)據(jù)類型和變量三
 - 01-10C#編程自學(xué)之運算符和表達(dá)式
 - 01-10C#編程自學(xué)之類和對象
 - 01-10C#編程自學(xué)之?dāng)?shù)據(jù)類型和變量一
 - 01-10C#編程自學(xué)之流程控制語句
 - 01-10C#基于委托實現(xiàn)多線程之間操作的方法
 


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
 - 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實現(xiàn)txt定位指定行完整實例
 - 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
 - 01-10C#停止線程的方法
 - 01-10C#實現(xiàn)清空回收站的方法
 - 01-10C#通過重寫Panel改變邊框顏色與寬度的
 - 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
 
隨機(jī)閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 08-05織夢dedecms什么時候用欄目交叉功能?
 - 04-02jquery與jsp,用jquery
 - 01-10delphi制作wav文件的方法
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-10C#中split用法實例總結(jié)
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 


