找回密码
 立即注册
查看: 224|回复: 0

XLua中C#读取lua配置表数据

[复制链接]
发表于 2022-6-7 12:46 | 显示全部楼层 |阅读模式
1.声明interface,只声明lua配置表的访问函数,例如:
[CSharpCallLua]
public interface IMonsterConfig
{
    int modelId(int id);
}
对应的lua配置表访问函数(Excel插件自动生成)为:
function MonsterConfig:modelId(id)
        local data = MonsterConfig[id]
        if data == nil then
                print("MonsterConfig没有id:" .. id)
                return nil
        end
        return data.modelId
end2.在Editor/XLuaGenConfig.cs里的CSharpCallLua加上该interface:
//C#静态调用Lua的配置(包括事件的原型),仅可以配delegate,interface
[CSharpCallLua]
[LuaCallCSharp]
public static List<Type> CSharpCallLua = new List<Type>() {
    typeof(Action),
    typeof(Func<double, double, double>),
    typeof(Func<long, long>),
    typeof(Func<long, string>),
    typeof(Func<string, long>),
    typeof(Action<string>),
    typeof(Action<double>),
    typeof(Action<bool>),
    typeof(Action<float, float>),
    typeof(UnityEngine.Events.UnityAction),
    typeof(UnityEngine.Events.UnityAction<long>),
    typeof(UnityEngine.Events.UnityAction<long,long>),
    typeof(UnityEngine.Events.UnityAction<string>),
    typeof(UnityEngine.Events.UnityAction<UnityEngine.Vector2>),
    typeof(UnityEngine.Events.UnityEvent<UnityEngine.Vector2>),
    typeof(System.Collections.IEnumerator),
    typeof(LuaConfigReader.IMonsterConfig),
};
3.先后执行Unity编辑里的XLua菜单的“Clear Generated Code”和“Generate Code”,生成相关代码;
4.读取lua配置表的数据:
LuaEnv luaenv = XLuaManager.Instance.GetLuaEnv();
IMonsterConfig m = luaenv.Global.Get<IMonsterConfig>("MonsterConfig");
int modelId = m.modelId(tid);
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-26 19:29 , Processed in 0.063374 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表