Unity 接入Luabn记录图解

发布于:2025-02-18 ⋅ 阅读:(98) ⋅ 点赞:(0)

文档及链接

官方文档

最新版本

项目目录

接入的方法有很多,我这里随便找了一种

https://gitee.com/focus-creative-games/luban_examples.git

如上图,git拉去后,只保留圈起来的2个文件夹。基础使用已经够了

每个项目工程目录不尽相同,这里及以下根据我自己的项目目录配置,

在Unity平级目录创建一个文件夹,用来存放导表工具Luban

将上面Git保留的2个文件夹放入这个文件夹下,如上图

这个路径下存放的是配置表,将配表填入_tables_.xlsx中,用以生成表数据及代码

这个是生成工具,需要用文档打开,修改为自己路径

set WORKSPACE=..
set LUBAN_DLL=%WORKSPACE%\Tools\Luban\Luban.dll
set CONF_ROOT=.

dotnet %LUBAN_DLL% ^
    -t client ^
    -c cs-simple-json ^
    -c cs-bin ^
    -d json ^
    -d bin ^
    --conf %CONF_ROOT%\luban.conf ^
    -x cs-simple-json.outputCodeDir=..\Code\Json ^
    -x cs-bin.outputCodeDir=..\..\ClientFrame\Assets\Game\Scripts\DataTable ^
    -x json.outputDataDir=..\..\ClientFrame\Assets\DataTable\Json ^
    -x bin.outputDataDir=..\..\ClientFrame\Assets\Res\Table

pause

这里同时导出了二进制和json,项目中用的是二进制,Json用于自己查表方便。可根据自己需求保留二进制和json
outputCodeDir是导出代码路径
outputDataDir是导出数据路径
可根据自己需求修改路径。

例如:只保留二进制

set WORKSPACE=..
set LUBAN_DLL=%WORKSPACE%\Tools\Luban\Luban.dll
set CONF_ROOT=.

dotnet %LUBAN_DLL% ^
    -t client ^
    -c cs-bin ^
    -d bin ^
    --conf %CONF_ROOT%\luban.conf ^
    -x cs-bin.outputCodeDir=..\..\ClientFrame\Assets\Game\Scripts\DataTable ^
    -x bin.outputDataDir=..\..\ClientFrame\Assets\Res\Table

pause

UnityEditor 导表工具

每次导表需要到项目找到对应文件夹,执行bat,很麻烦。
在UnityEditor 加个工具,直接执行bat导表


using System.Diagnostics;
using System.IO;
using UnityEngine;
using UnityEditor;

public class LubanExpand
{
   
    [MenuItem("Luban/Export Excel")]
    public static void ExecuteBat()
    {
       
        // 获取上一级目录
        var parentPath = Path.GetDirectoryName(Path.GetDirectoryName(Application.dataPath));
        // 确定 .bat 文件的绝对路径
        var batFilePath = Path.Combine(parentPath, "Luban/DataTables/gen.bat").Replace("\\", "/" );
        // 获取.bat文件所在目录
        var batDirectory = Path.GetDirectoryName(batFilePath);
        if (!File.Exists(batFilePath))
        {
            UnityEngine.Debug.LogError($"Bat file not found: {batFilePath}");
            return;
        }
        try
        {
            var processInfo = new ProcessStartInfo
            {
                FileName = batFilePath,
                UseShellExecute = false,          // 不使用系统Shell
                RedirectStandardOutput = true,    // 重定向输出
                RedirectStandardError = true,     // 重定向错误
                CreateNoWindow = false,            // 不创建新窗口
                WorkingDirectory = batDirectory   // 关键点:工作目录设为.bat所在文件夹
            };

            var process = new Process { StartInfo = processInfo };
            process.Start();

            // 读取输出(防止进程阻塞)
            var output = process.StandardOutput.ReadToEnd();
            var error = process.StandardError.ReadToEnd();
            process.WaitForExit();
            AssetDatabase.Refresh();
            if (!string.IsNullOrEmpty(output))
                UnityEngine.Debug.Log($"Bat Output:\n{output}");

            if (!string.IsNullOrEmpty(error))
                UnityEngine.Debug.LogError($"Bat Error:\n{error}");

            UnityEngine.Debug.Log("Bat executed successfully!");
        }
        catch (System.Exception e)
        {
            UnityEngine.Debug.LogError($"Failed to execute bat: {e.Message}");
        }
    }
}

坑点:上面代码中 WorkingDirectory 是指定工作目录。如果不设置,工作目录会默认是Unity项目的Assets平级目录,gen.bat 里的WORKSPACE 生效错误,无法导出


网站公告

今日签到

点亮在社区的每一天
去签到