Unity editor to create lua extensions of the template file

This paper provides a template file templates and create .lua xlua in .lua.txt format;

Since the main reference: Jane books -Unity Technology Extended script template (Lua for example) erick_book

The original bloggers article only supports a template to create lua files, due to the recent need to use the function to xlua, and xlua suffix .lua.txt, so we added a expansion;

Do not re-create the wheel so-called Well he simply looked at the code to move over, made a point of simple modifications, here to share it;

Mainly it includes two scripts for a moment, to move directly to your project directory to be available on GitHub, https://github.com/zhimo1997/CustomTools

Use:

1.lua template file

The need to define themselves, and then the directory can be assigned to;

private static readonly string LuaScriptTemplatePath = "Assets/1-LuaTemplate/Editor/LuaTemplate.txt";

2.lua file icon

This may or may not, unity will provide a default 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;
    }
}

 

 

Mo
Published 147 original articles · won praise 46 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_36383623/article/details/104023350