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来监听回调,最终实现输入脚本名字后自动创建相应的脚本模板。

代码如下,将这个脚本放入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, )]
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(, 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,输入脚本名字,对应的脚本就已经创建好了

4、总结

     上面介绍了两种方案,第一种适合玩玩,第二种方法显然逼格高一些,为不同的项目和框架定制一套脚本模板,可以让我们少写一些重复代码。按照上面介绍的方法,我们同样可以修改和拓展Testing、Playables的脚本模板,甚至shader,我们也可以定制模板。

unity之定制脚本模板的更多相关文章

  1. 定制自己的Unity脚本模板

    有时候想给脚本添加符合自己编程习惯的内容,或是一些个性化信息.而作为一个多多少少有点强迫症的人,这种东西要加就得每个脚本都加上,不然看着多不爽! 于是就得每添加一个脚本就去修改一下,很麻烦. 但是,在 ...

  2. 修改Unity脚本模板的方法合计

    作为一个习惯于偷懒的程序,重复性的无聊内容是最让人无奈的事,就比如我们创建Unity脚本之后,需要手动调整生成的新脚本的格式.编码.内容:如果我们要编写的是编辑器或者服务器端脚本,需要修改的内容就会更 ...

  3. 【Cocos谁学谁会】定制属于自己的脚本模板

    版权申明: 本文原创首发于以下网站,您可以自由转载,但必须加入完整的版权声明 博客园:https://www.cnblogs.com/MogooStudio/ csdn博客:https://blog. ...

  4. Unity 添加,修改默认创建脚本模板

    Unity 默认创建的脚本可以添加也可以修改,不需要修改Editor. 一.找到模板目录 \Editor\Data\Resources\ScriptTemplates 二.如果要修改模板,直接打开修改 ...

  5. Tips12: 私人定制 专属的Unity3D 脚本模板

    在使用U3D的过程中,新建一个C#脚本,它包含着空的Start()和Update()函数.  根据个人习惯的不同,可能有些人有着自己的脚本风格,每次进去都增删改很麻烦,这里介绍一个更改新建脚本模板的方 ...

  6. 《转》Unity3D研究院编辑器之创建Lua脚本模板

    Unity里能创建 c#脚本模板,但是如果我想创建Lua脚本模板怎么办呢?拓展一下编辑器吧. 设置一下Lua脚本的模板地址 :  Assets/Editor/Lua/Template/lua.lua ...

  7. Unity3d自定义脚本模板

    这是一个小技巧,打开Unity安装目录,如: C:\Program Files (x86)\Unity\Editor\Data\Resources\ScriptTemplates /* * * Tit ...

  8. 如何修改新建脚本模板-ScriptTemplates(Unity3D开发之十五)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/44957631 ...

  9. unity中js脚本与c#脚本互相调用

    unity中js脚本与c#脚本互相调用   test1.js function OnGUI() { if(GUI.Button(Rect(25,25,100,30),"JS Call CS& ...

随机推荐

  1. python的数据存储

    Python存储数据 使用json.dump()和json.load() 不管专注的是什么,程序都把用户提供的信息存储在列表和字典等数据结构中.用户关闭程序时,你几乎总是要保存他们提供的信息:一种简单 ...

  2. Microsoft SQL Server 2012 管理 (1): 安装配置SQL Server 重点

    SQL Server 可以在实例,数据库,列,查询分别指定排序规则 /* Module 1 - working with Clollations */ -- 1.1 Obtain the Instan ...

  3. 实现EventHandler的监测

    的监测", "category":"", "tags":"", "publish":&qu ...

  4. Unity Shader 阶段性反思与总结(一)

    Unity Shader 阶段性反思与总结(一) 最近在写Shader的时候,总是感觉力不从心,感觉自己已经看了蛮久的书了,也有一定的积累了,但是一想写什么效果,完完全全就是脑袋一团空白.典型的例子就 ...

  5. Linux(Debian)网卡设置

    debian IP地址配置 vim /etc/network/interface   配置网卡eth0的IP地址 auto eth0 表示网卡随系统自动请 iface eth0 inet static ...

  6. H - The LCIS on the Tree HDU - 4718

    The LCIS on the Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Oth ...

  7. A Tool To Plot Mathematical Function

    Plot.cs using Microsoft.ClearScript; using Microsoft.ClearScript.V8; using Microsoft.Win32; using Sy ...

  8. window7下Python安装

    去python官网下载软件,https://www.python.org. 选择dowloads-->windows,选择稳定版python3.5.2,x86表示32位操作系统,x86-64表示 ...

  9. django -orm操作总结

    前言 Django框架功能齐全自带数据库操作功能,本文主要介绍Django的ORM框架 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MyS ...

  10. PostgreSQL 数据库错误代码解释

    PostgreSQL 服务器发出的所有消息都赋予了五个字符 的错误代码, 这些代码遵循 SQL 的 "SQLSTATE" 代码的习惯.需要知道发生了什么错误条件的应用通常应该测试错 ...