Unity编辑器扩展之创建Lua模板文件

本文主要提供了创建.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;
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值