首页 技术 正文
技术 2022年11月16日
0 收藏 324 点赞 4,649 浏览 2413 个字

Unity里能创建 c#脚本模板,但是如果我想创建Lua脚本模板怎么办呢?拓展一下编辑器吧。

设置一下Lua脚本的模板地址 :  Assets/Editor/Lua/Template/lua.lua

using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Text;
using UnityEditor.ProjectWindowCallback;
using System.Text.RegularExpressions;public class Test
{
[MenuItem("Assets/Create/Lua Script", false, )]
public static void CreatNewLua()
{
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(,
ScriptableObject.CreateInstance<MyDoCreateScriptAsset>(),
GetSelectedPathOrFallback() + "/New Lua.lua",
null,
"Assets/Editor/Lua/Template/lua.lua");
} public static string GetSelectedPathOrFallback()
{
string path = "Assets";
foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
{
path = AssetDatabase.GetAssetPath(obj);
if (!string.IsNullOrEmpty(path) && File.Exists(path))
{
path = Path.GetDirectoryName(path);
break;
}
}
return path;
}
} class MyDoCreateScriptAsset : EndNameEditAction
{ public override void Action(int instanceId, string pathName, string resourceFile)
{
UnityEngine.Object o = CreateScriptAssetFromTemplate(pathName, resourceFile);
ProjectWindowUtil.ShowCreatedAsset(o);
} internal static UnityEngine.Object CreateScriptAssetFromTemplate(string pathName, string resourceFile)
{
string fullPath = Path.GetFullPath(pathName);
StreamReader streamReader = new StreamReader(resourceFile);
string text = streamReader.ReadToEnd();
streamReader.Close();
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathName);
text = Regex.Replace(text, "#NAME#", fileNameWithoutExtension);
//string text2 = Regex.Replace(fileNameWithoutExtension, " ", string.Empty);
//text = Regex.Replace(text, "#SCRIPTNAME#", text2);
//if (char.IsUpper(text2, 0))
//{
// text2 = char.ToLower(text2[0]) + text2.Substring(1);
// text = Regex.Replace(text, "#SCRIPTNAME_LOWER#", text2);
//}
//else
//{
// text2 = "my" + char.ToUpper(text2[0]) + text2.Substring(1);
// text = Regex.Replace(text, "#SCRIPTNAME_LOWER#", text2);
//}
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(pathName);
return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object));
}}

因为是模板就可以添加一些预制代码在上面, 当填写完类名后可以来替换操作,  例如unity自带的 创建C#文件, 外面写了类名里面也跟这变。Creat -> Lua Script

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

然后可以输入Lua脚本的类名

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

然后就根据自己创建Lua的文件名 正则替换模板里的东西了。

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781