unity学习教程之定制脚本模板示例代码
作者:禹泽鹏鹏 发布时间:2022-02-18 05:07:08
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, 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,输入脚本名字,对应的脚本就已经创建好了
4、总结
上面介绍了两种方案,第一种适合玩玩,第二种方法显然逼格高一些,为不同的项目和框架定制一套脚本模板,可以让我们少写一些重复代码。按照上面介绍的方法,我们同样可以修改和拓展Testing、Playables的脚本模板,甚至shader,我们也可以定制模板。
好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。
来源:http://www.cnblogs.com/IAMTOM/p/10156148.html


猜你喜欢
- 前言fragment 可认为是一个轻量级的Activity,但不同与Activity,它是要嵌到Activity中来使用的,它用来解决设备屏
- 事务处理基本原理 事务是将一系列操作作为一个单元执行,要么成功,要么失败,回滚到
- CDMA猫真是!@#¥#%(*,连PDU都不支持,只能发文本短信。而且发中文短信居然是UNICODE,无法在超级终端里输入。只能写程序。 网
- 一、什么是 javabean ?在jsp页面中,包含html代码、css代码、java代码、以及业务逻辑处理代码等。javabean的作用就
- 小编在之前给大家介绍过很多android项目打包的经验,本篇内容我们通过一个项目实例来给大家讲解android每一步打包和签名的过程。and
- 这篇文章主要介绍了Java利用读写的方式实现音频播放代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- 本文实例为大家分享了Android实现签名涂鸦手写板的具体代码,供大家参考,具体内容如下布局文件<?xml version="
- 通过@Query注解支持JPA语句和原生SQL语句在SpringData中们可是使用继承接口直接按照规则写方法名即可完成查询的方法,不需要写
- 做项目的时候需要对拿到的数据进行“清洗”,比如剔除一些不可能存在的身份证号码。查阅了网上的身份证号码验证算法,自己也总结一下。(一)18身份
- 话不多说,请看代码:<!DOCTYPE html><html><head> <meta
- JAVA中反射机制(JavaBean的内省与BeanUtils库)内省(Introspector) 是Java 语言对JavaBean类属性
- 如下所示:using System.Linq;List<string> ListA = new List<string&g
- 一、概述项目中经常用到倒计时的功能,比如说限时抢购,手机获取验证码等等。而google官方也帮我们封装好了一个类:CountDownTime
- 前言前阵子有同学反馈Flutter中的http请求无法通过fiddler抓包,作者喜欢使用Charles抓包工具,于是抽时间写了个小demo
- 前言我昨天做了个梦,我梦见我在一条路走,走的时候经过一个房间,里面关着一条边牧和鸡和猪,后来我醒了,我知道那只边牧就是小叶子(哈仔十一的边牧
- 1.概述1、Spring 是轻量级的开源的 JavaEE 框架2、 Spring 可以解决企业应用开发的复杂性3、Spring 有两个核心部
- 本文以一个C#的SQL数据库字串操作函数为例,说明如何实现对SQL字符串过滤、检测SQL是否有危险字符、修正sql语句中的转义字符,确保SQ
- using System;using System.Collections.Generic;using System.Text;using
- 一、背景项目中新建module之后,要在该目录下新增java Class文件,右键——》New发现无Java Class选项。二、办法Fil
- 前面文章讲述了Android手机与BLE终端之间的通信,而最常见的BLE终端应该是苹果公司倡导的iBeacon基站。iBeacon技术基于B