日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

unity之定制脚本模板

發(fā)布時(shí)間:2025/7/25 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 unity之定制脚本模板 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、unity的腳本模板

新版本unity中的C#腳本有三類(lèi),第一類(lèi)是我們平時(shí)開(kāi)發(fā)用的C# Script;第二類(lèi)是Testing,用來(lái)做單元測(cè)試;第三類(lèi)是Playables,用作TimeLine中管理時(shí)間線上每一幀的動(dòng)畫(huà)、聲音等。我們點(diǎn)擊創(chuàng)建腳本時(shí),會(huì)自動(dòng)生成unity內(nèi)置的一套模板: using System.Collections; using System.Collections.Generic; using UnityEngine;public class NewBehaviourScript : MonoBehaviour {// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {} } 如果我們開(kāi)發(fā)時(shí)使用的框架有明顯的一套基礎(chǔ)模板, 那為項(xiàng)目框架定制一套模板會(huì)很有意義,這樣可以為我們省去編寫(xiě)重復(fù)代碼的時(shí)間。這里介紹兩種方法。

2、修改默認(rèn)腳本模板

打開(kāi)unity安裝目錄,比如D:\unity2018\Editor\Data\Resources\ScriptTemplates,unity內(nèi)置的模板腳本都在這里,那么可以直接修改這里的cs文件,比如我們將81-C# Script-NewBehaviourScript.cs.txt文件修改為如下,那下次創(chuàng)建C# Script時(shí)模板就會(huì)變成這樣:

?

//// // _ooOoo_ // // o8888888o // // 88" . "88 // // (| ^_^ |) // // O\ = /O // // ____/`---'\____ // // .' \\| |// `. // // / \\||| : |||// \ // // / _||||| -:- |||||- \ // // | | \\\ - /// | | // // | \_| ''\---/'' | | // // \ .-\__ `-` ___/-. / // // ___`. .' /--.--\ `. . ___ // // ."" '< `.___\_<|>_/___.' >'"". // // | | : `- \`.;`\ _ /`;.`/ - ` : | | // // \ \ `-. \_ __\ /__ _/ .-` / / // // ========`-.____`-.___\_____/___.-`____.-'======== // // `=---=' // // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // // 佛祖保佑 永不宕機(jī) 永無(wú)BUG // ////using System.Collections; using System.Collections.Generic; using UnityEngine;public class #SCRIPTNAME# : MonoBehaviour {// Use this for initializationvoid Start () {#NOTRIM#}// Update is called once per framevoid Update () {#NOTRIM#} }

3、拓展腳本模板

上面講的第一種方法直接修改了unity的默認(rèn)配置,這并不適應(yīng)于所有項(xiàng)目,這里第二種方法會(huì)更有效,可以針對(duì)不同的項(xiàng)目和框架創(chuàng)建合適的腳本模板。 首先,先創(chuàng)建一個(gè)文本文件MyTemplateScript.cs.txt作為腳本模板,并將其放入unity project的Editor文件夾下,模板代碼為: using System.Collections; using System.Collections.Generic; using UnityEngine;public class MyNewBehaviourScript : MonoBase {//添加事件監(jiān)聽(tīng)protected override void AddMsgListener(){}//處理消息protected override void HandleMsg(MsgBase msg){switch (msg.id){default:break;}}} 我們使用時(shí),需要在Project視圖中右擊->Create->C# FrameScript 創(chuàng)建腳本模板,因此首先要?jiǎng)?chuàng)建路徑為Assets/Create/C# FrameScript的MenuItem,點(diǎn)擊創(chuàng)建腳本后,需要修改腳本名字,因此需要在拓展編輯器腳本中繼承EndNameEditAction來(lái)監(jiān)聽(tīng)回調(diào),最終實(shí)現(xiàn)輸入腳本名字后自動(dòng)創(chuàng)建相應(yīng)的腳本模板。

代碼如下,將這個(gè)腳本放入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";//菜單項(xiàng)[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中,點(diǎn)擊創(chuàng)建C# FrameScript,輸入腳本名字,對(duì)應(yīng)的腳本就已經(jīng)創(chuàng)建好了

4、總結(jié)

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

?

?

?

?

?

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/IAMTOM/p/10156148.html

總結(jié)

以上是生活随笔為你收集整理的unity之定制脚本模板的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。