xLua icon indicating copy to clipboard operation
xLua copied to clipboard

CustomLoader该如何支持异步呢?小游戏平台WebGL需要使用异步加载

Open Zsnbda opened this issue 9 months ago • 1 comments

    public override void Initialize(GameObject eventSystem)
    {
        base.Initialize(eventSystem);
        luaEnv = new LuaEnv();

        LuaEnv.CustomLoader loader = CustomLoader;
        luaEnv.customLoaders.Clear();
        luaEnv.AddLoader(loader);
        luaEnv.AddBuildin("pb", XLua.LuaDLL.Lua.LoadLuaProfobuf);
        luaEnv.AddBuildin("rapidjson", XLua.LuaDLL.Lua.LoadRapidJson);
    }

    private byte[] CustomLoader(ref string filepath)
    {
        TextAsset asset = App.Loader.LoadAsset<TextAsset>(PATH + filepath + ".lua.txt");
        return asset.bytes;
    }

目前是先把所有的lua文件异步加载到内存中,CustomLoader从内存中获取, 有更好的方案支持CustomLoader直接使用async/await异步加载吗?或者是必须得修改xlua的源代码支持异步require呢?

Zsnbda avatar Jun 13 '25 09:06 Zsnbda

    public override void Initialize(GameObject eventSystem)
    {
        base.Initialize(eventSystem);
        luaEnv = new LuaEnv();

        LuaEnv.CustomLoader loader = CustomLoader;
        luaEnv.customLoaders.Clear();
        luaEnv.AddLoader(loader);
        luaEnv.AddBuildin("pb", XLua.LuaDLL.Lua.LoadLuaProfobuf);
        luaEnv.AddBuildin("rapidjson", XLua.LuaDLL.Lua.LoadRapidJson);
    }

    private byte[] CustomLoader(ref string filepath)
    {
        TextAsset asset = App.Loader.LoadAsset<TextAsset>(PATH + filepath + ".lua.txt");
        return asset.bytes;
    }

目前是先把所有的lua文件异步加载到内存中,CustomLoader从内存中获取, 有更好的方案支持CustomLoader直接使用async/await异步加载吗?或者是必须得修改xlua的源代码支持异步require呢?

可以参考lua-auto-require库的做法

Checkson avatar Aug 08 '25 02:08 Checkson