美文网首页
ToLua的Example示例学习笔记18_Bundle

ToLua的Example示例学习笔记18_Bundle

作者: 凌枫望星月 | 来源:发表于2020-05-13 20:59 被阅读0次

展示了如何使用Assetbundle从本地(或服务器)来获取需要的Lua文件并执行。

1」代码

c#代码如下,这次我们分段来讲讲:

    void Awake()
    {
        LuaFileUtils file = new LuaFileUtils();
        file.beZip = true;
        StartCoroutine(LoadBundles());
    }         

上面的代码让LuaFileUtils 的单例的beZip = true,在LuaFileUtils中,beZip = false 在search path 中查找读取lua文件。否则从外部设置过来bundle文件中读取lua文件。注意一般Assetbundle里的都是xxx.lua.bytes文件,只有这样才能打包进AB包中。

    public IEnumerator LoadBundles()
    {
        string streamingPath = Application.streamingAssetsPath.Replace('\\', '/');
        string main = streamingPath + "/" + LuaConst.osDir + "/" + LuaConst.osDir;
        WWW www = new WWW(main);
        yield return www;

        AssetBundleManifest manifest = (AssetBundleManifest)www.assetBundle.LoadAsset("AssetBundleManifest");
        List<string> list = new List<string>(manifest.GetAllAssetBundles());        
        bundleCount = list.Count;

        for (int i = 0; i < list.Count; i++)
        {
            string str = list[i];
            string path = streamingPath + "/" + LuaConst.osDir + "/" + str;
            string name = Path.GetFileNameWithoutExtension(str);
            StartCoroutine(CoLoadBundle(name, path));            
        }

        yield return StartCoroutine(LoadFinished());
    }

这一段将AB包的地址告诉www,获取并从中得到ab的目录文件manifest,遍历目录,通过CoLoadBundle让所有目录中的的name与ab自身加到AddSearchBundle的搜索路径中,其实就是一个ZipMap字典(name, bundle)。

    IEnumerator CoLoadBundle(string name, string path)
    {
        using (WWW www = new WWW(path))
        {
            if (www == null)
            {
                Debugger.LogError(name + " bundle not exists");
                yield break;
            }

            yield return www;

            if (www.error != null)
            {
                Debugger.LogError(string.Format("Read {0} failed: {1}", path, www.error));
                yield break;
            }

            --bundleCount;
            LuaFileUtils.Instance.AddSearchBundle(name, www.assetBundle);
            www.Dispose();
        }                     
    }

还有,连开多个协程CoLoadBundle和协程LoadFinished,而LoadFinished每一帧看一下CoLoadBundle全部走完没,没走完就等着。

    IEnumerator LoadFinished()
    {
        while (bundleCount > 0)
        {
            yield return null;
        }

        OnBundleLoad();
    }

等CoLoadBundle全部走完,LoadFinished开始走OnBundleLoad:

    void OnBundleLoad()
    {                
        LuaState state = new LuaState();
        state.Start();
        state.DoString("print('hello tolua#:'..tostring(Vector3.zero))");
        state.DoFile("Main.lua");
        LuaFunction func = state.GetFunction("Main");
        func.Call();
        func.Dispose();
        state.Dispose();
        state = null;
    }

由于已经按名字加到了zipMap,DoFile走bundle的路子也没什么问题,找到Main.lua并执行之。

2」需要了解的部分

  • 第一次运行不起来的同学,要去Unity界面运行菜单命令Lua/build bundle files not jit,先打包输出到/Assets/StreamingAssets目录。

相关文章

网友评论

      本文标题:ToLua的Example示例学习笔记18_Bundle

      本文链接:https://www.haomeiwen.com/subject/eoinnhtx.html