本文主要提供了创建.lua模板以及xlua中的.lua.txt格式的模板文件;
主要参考自:简书-Unity技术 扩展脚本模板(Lua为例) erick_book
原博主的文章中只支持了创建lua文件的模板,由于最近需要使用功能到xlua,而xlua的后缀为.lua.txt,所以就加了个扩展;
正所谓不要重复造轮子嘛,自己简单看了一下代码就搬了过来,做了点简单修改,在这里分享一下;
主要包含了一下两个脚本,直接移动到自己的项目目录里即可,可以从github上获取,https://github.com/zhimo1997/CustomTools
使用方式:
1.lua模板文件
这个需要自己定义,然后将目录赋值给即可;
private static readonly string LuaScriptTemplatePath = "Assets/1-LuaTemplate/Editor/LuaTemplate.txt";
2.lua文件icon
这一个可用可不用,unity会为其提供默认的icon;
private static readonly string LuaScriptIconPath = "Assets/Tools/ScriptTemplates/lua_icon64.png";
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.ProjectWindowCallback;
using Microsoft.VisualBasic;
public static class LuaScriptCreater
{
private static readonly string LuaScriptIconPath = "Assets/Tools/ScriptTemplates/lua_icon64.png";
private static readonly string LuaScriptTemplatePath = "Assets/1-LuaTemplate/Editor/LuaTemplate.txt";
[MenuItem("Assets/Create/Lua Script", false, 81)]
private static void CreateLuaScript()
{
CreateLuaTemplate(".lua");
}
[MenuItem("Assets/Create/xLua Script", false, 81)]
private static void CreateXLuaScript()
{
CreateLuaTemplate(".txt");
}
private static void CreateLuaTemplate(string ext){
if (EditorApplication.isCompiling || EditorApplication.isPlaying)
{
EditorUtility.DisplayDialog("警告", "无法在游戏运行时或代码编译时创建lua脚本", "确定");
return;
}
Texture2D icon = AssetDatabase.LoadAssetAtPath<Texture2D>(LuaScriptIconPath);
string scriptDirPath = PathUtil.GetSelectionAssetDirPath();
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
ScriptableObject.CreateInstance<CreateLuaScriptAction>(),
scriptDirPath + "/NewLuaScript"+ext, icon,
LuaScriptTemplatePath);
}
}
internal class CreateLuaScriptAction : EndNameEditAction
{
public override void Action(int instanceId, string pathName, string resourceFile)
{
string content = File.ReadAllText(PathUtil.GetDiskPath(resourceFile));
string fileName = Path.GetFileNameWithoutExtension(pathName);
content = content.Replace("#NAME#", fileName);
string fullName = PathUtil.GetDiskPath(pathName);
File.WriteAllText(fullName, content);
AssetDatabase.ImportAsset(pathName);
string ext=Path.GetExtension(pathName);
if(ext==".txt"){
string oldPathName=pathName;
string newPathName=Path.ChangeExtension(pathName,".lua");
newPathName+=".txt";
if(File.Exists(PathUtil.GetDiskPath(oldPathName))){
File.Move(PathUtil.GetDiskPath(oldPathName),PathUtil.GetDiskPath(newPathName));
pathName=newPathName;
}
}
Object obj = AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object));
ProjectWindowUtil.ShowCreatedAsset(obj);
AssetDatabase.Refresh();
}
}
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
public static class PathUtil
{
public static string GetAssetPath(string dirPath)
{
dirPath = dirPath.Replace("\\", "/");
if (dirPath.StartsWith(Application.dataPath))
{
return "Assets" + dirPath.Replace(Application.dataPath, "");
}
return string.Empty;
}
public static string GetDiskPath(string assetPath)
{
if (string.IsNullOrEmpty(assetPath))
{
return string.Empty;
}
assetPath = assetPath.Replace("\\", "/");
if (!assetPath.StartsWith("Assets"))
{
return string.Empty;
}
return Application.dataPath + assetPath.Substring(assetPath.IndexOf("Assets") + 6);
}
public static string GetSelectionAssetDirPath()
{
string path = "Assets";
foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
{
if(obj == null)
{
continue;
}
path = AssetDatabase.GetAssetPath(obj);
if(Path.HasExtension(path))
{
path = Path.GetDirectoryName(path);
}
break;
}
return path;
}
}