10.批量转换Lua脚本后缀工具

10.批量转换Lua脚本后缀工具


10.1 知识点

工具目标

在Unity编辑器中创建一个菜单项,然后执行一个操作,该操作将Lua脚本文件的内容复制到具有”.txt”扩展名的新文件中,并将这些新文件放入另一个文件夹中。

创建脚本,放在Editor文件夹下

创建方法,添加可以点击自定义菜单栏的特性

[MenuItem("XLua/自动生成txt后缀的Lua")]
public static void CopyLuaToTxt()
{
   
}

找到原来存放lua脚本的路径,用一个数组装所有路径

//首先要找到 我们的所有Lua文件
string path = Application.dataPath + "/LuaScripts/";

//判断路径是否存在
if (!Directory.Exists(path))
    return;

//得到每一个lua文件的路径 才能进行迁移拷贝 这个数组存储的是原路径下所有lua脚本的全路径
string[] strs = Directory.GetFiles(path, "*.lua");

生成新路径,并清空新路径下可能存在的文件

//然后把Lua文件拷贝到一个新的文件夹中
//首先定一个新路径
string newPath = Application.dataPath + "/LuaScriptsTxt/";

//为了避免一些被删除的lua文件 不再使用 我们应该先清空目标路径
//判断新路径文件夹 是否存在
if (!Directory.Exists(newPath))
    Directory.CreateDirectory(newPath);
else
{
    //得到该路径中 所有后缀.txt的文件 把它们全部删除了
    string[] oldFileStrs = Directory.GetFiles(newPath, "*.txt");
    for (int i = 0; i < oldFileStrs.Length; i++)
    {
        File.Delete(oldFileStrs[i]);
    }
}

拷贝原路径的文件到新路径,并添加txt后缀

//存储所有新Lua.txt后缀脚本的List 这个数组要存储的是新路径下所有lua脚本的全路径
List<string> newFileNames = new List<string>();

//临时变量存储路径
string fileName;

for (int i = 0; i < strs.Length; ++i)
{
    //得到新的文件路径 用于拷贝
    //用 Path.GetFileName 取文件名,避免 Windows 下路径分隔符不是 / 时 LastIndexOf 失效
    fileName = newPath + Path.GetFileName(strs[i]) + ".txt";

    newFileNames.Add(fileName);

    //指定要拷贝的路径和新路径 拷贝过去
    File.Copy(strs[i], fileName);
}

//刷新Project窗口
AssetDatabase.Refresh();

批量修改.txt后缀的Lua文件所在的AB包

//刷新过后 再来修改Lua.txt后缀脚本的所在AB包 因为 如果不刷新 第一次改变 会没用
for (int i = 0; i < newFileNames.Count; i++)
{
    //改AB包 传入的路径 必须是 相对Assets文件夹的 传入的路径要从Assets开始 Assets/..../.... 
    //newFileNames[i].Substring(newFileNames[i].IndexOf("Assets")) 是把路径截取 要Assets之后的路径
    Debug.Log("修改Lua.txt后缀脚本的所在AB包的路径:" + newFileNames[i].Substring(newFileNames[i].IndexOf("Assets")));
    AssetImporter importer = AssetImporter.GetAtPath(newFileNames[i].Substring(newFileNames[i].IndexOf("Assets")));

    //全部修改为luascripts
    if (importer != null)
        importer.assetBundleName = "luascripts";
}

打AB包,报错的话重新生成xLua代码,测试AB包重定向Lua


10.2 知识点代码

LuaScriptFormatConverterTxtAndCopy

using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class LuaScriptFormatConverterTxtAndCopy
{
    [MenuItem("XLua/自动生成txt后缀的Lua")]
    public static void CopyLuaToTxt()
    {
        //首先要找到 我们的所有Lua文件
        string path = Application.dataPath + "/LuaScripts/";

        //判断路径是否存在
        if (!Directory.Exists(path))
            return;

        //得到每一个lua文件的路径 才能进行迁移拷贝
        string[] strs = Directory.GetFiles(path, "*.lua");

        //然后把Lua文件拷贝到一个新的文件夹中
        //首先定一个新路径
        string newPath = Application.dataPath + "/LuaScriptsTxt/";

        //为了避免一些被删除的lua文件 不再使用 我们应该先清空目标路径
        //判断新路径文件夹 是否存在
        if (!Directory.Exists(newPath))
            Directory.CreateDirectory(newPath);
        else
        {
            //得到该路径中 所有后缀.txt的文件 把它们全部删除了
            string[] oldFileStrs = Directory.GetFiles(newPath, "*.txt");
            for (int i = 0; i < oldFileStrs.Length; i++)
            {
                File.Delete(oldFileStrs[i]);
            }
        }

        //存储所有新Lua.txt后缀脚本的List
        List<string> newFileNames = new List<string>();

        //临时变量存储路径
        string fileName;

        for (int i = 0; i < strs.Length; ++i)
        {
            //用 Path.GetFileName 取文件名,避免 Windows 下路径分隔符不是 / 时 LastIndexOf 失效
            fileName = newPath + Path.GetFileName(strs[i]) + ".txt";

            newFileNames.Add(fileName);

            //指定要拷贝的路径和新路径 拷贝过去
            File.Copy(strs[i], fileName);
        }

        //刷新Project窗口
        AssetDatabase.Refresh();

        //刷新过后 再来修改Lua.txt后缀脚本的所在AB包 因为 如果不刷新 第一次改变 会没用
        for (int i = 0; i < newFileNames.Count; i++)
        {
            //改AB包 传入的路径 必须是 相对Assets文件夹的 传入的路径要从Assets开始 Assets/..../.... 
            //newFileNames[i].Substring(newFileNames[i].IndexOf("Assets")) 是把路径截取 要Assets之后的路径
            Debug.Log("修改Lua.txt后缀脚本的所在AB包的路径:" + newFileNames[i].Substring(newFileNames[i].IndexOf("Assets")));
            AssetImporter importer = AssetImporter.GetAtPath(newFileNames[i].Substring(newFileNames[i].IndexOf("Assets")));

            //全部修改为luascripts
            if (importer != null)
                importer.assetBundleName = "luascripts";
        }
    }
}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com

×

喜欢就点赞,疼爱就打赏