Files
Fishing2Server/Fantasy/Fantasy.Packages/Fantasy.ConfigTable
2025-06-30 10:51:37 +08:00
..
2025-06-30 10:51:37 +08:00
2025-06-30 10:51:37 +08:00
2025-06-30 10:51:37 +08:00

Fantasy.ConfigTable

Fantasy配置表扩展包配合Tools里面的配置导出工具使用注意该包依赖Fantasy包。

  • Unity : 需要先安装Fantasy.Unity
  • Net : 需要先安装Fantasy.Net

Unity

在Unity中使用Package Manager点击左上角选择add package from disk选择package.json安装。

Unity端采用assetbundle方式加载配置文件需要把配置文件放到一个包中例如config的ab包。

可以在导出工具中设置导出到前端二进制文件的路径设置到这个config包中。

用代码使用一个IConfigTableAssetBundle接口

public sealed class AsserBundleManager : IConfigTableAssetBundle
    {
        public string Combine(string assetBundleDirectoryPath, string dataConfig)
        {
            // 该方法用于拼装ab包的路径因为不同的ab包资源管理工具路径都不一样
            // 所以这里提供该方法自定义路径
            return Path.Combine(assetBundleDirectoryPath, $"{dataConfig}.bytes");
        }

        public byte[] LoadConfigTable(string assetBundlePath)
        {
            // 这里是加载包里的二进制文件的逻辑。
            // 正常的情况下需要分编辑器和打包环境下。
            // 这里只做了编辑器下拿取ab包里的某个配置文件的逻辑。
            // 正常情况下要把非编辑器环境的逻辑也要写好。
            if (File.Exists(assetBundlePath))
            {
                return AssetDatabase.LoadAssetAtPath<TextAsset>(assetBundlePath).bytes;
            }
            UnityEngine.Debug.LogError($"assetBundlePath:{assetBundlePath} not exist");
            return null;
        }
    }

上述工作完成够,可以在项目入口点执行初始化代码

// 第一个参数是指定ab包的路径但不包含配置名字因为配置文件会在上面的AsserBundleManager里Combine方法拼接出的路径
// 同样这里因为是编辑环境下,所以我输入的是一个路径。
// 但如果打包后这个路径其实并不能使用要把这个路径改成ab包发布都得正常路径。
ConfigTableHelper.Initialize("Assets/Bundles/Config/", new AsserBundleManager());

Net

// 设置配置表的路径 
// 导出工具会把配置表的二进制数据导出到一个目录中。
// 服务器启动的时候需要传入这个目录的路径。
// 我这里使用了相对路径,大家可以根据自己的环境更改目录
ConfigTableHelper.Initialize("../../../Config/Binary");

使用

初始化完成后,就可以在项目中使用配置文件了

// 例如我配置了一个表名为UnitConfig那在导出在代码中使用只需要再这个表名后面加上Data
// 然后在使用下面的Instance就可以访问表里的数据了

// 获得表里的所有数据
var instanceList = UnitConfigData.Instance.List;
// 根据表的主键Id来获得数据这里的1就是表对应的Id
var unitConfig = UnitConfigData.Instance.Get(1);