ToLua# Example 18 TestABLoader

目录

 

 

代码全部

怎么运行

容我猜猜


 

代码全部:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using LuaInterface;
using System;

//click Lua/Build lua bundle
public class TestABLoader : MonoBehaviour 
{
    int bundleCount = int.MaxValue;
    string tips = null;

    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);//加入zipMap
            www.Dispose();
        }                     
    }

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

        OnBundleLoad();//所有的bundle加载完之后执行这个
    }

    public IEnumerator LoadBundles()
    {
        string streamingPath = Application.streamingAssetsPath.Replace('\\', '/');

#if UNITY_5 || UNITY_2017 || UNITY_2018
#if UNITY_ANDROID && !UNITY_EDITOR
        string main = streamingPath + "/" + LuaConst.osDir + "/" + LuaConst.osDir;
#else
        string main = "file:///" + streamingPath + "/" + LuaConst.osDir + "/" + LuaConst.osDir;
#endif
        WWW www = new WWW(main);
        yield return www;

        AssetBundleManifest manifest = (AssetBundleManifest)www.assetBundle.LoadAsset("AssetBundleManifest");
        List list = new List(manifest.GetAllAssetBundles());        
#else
        //此处应该配表获取
        List list = new List() { "lua.unity3d", "lua_cjson.unity3d", "lua_system.unity3d", "lua_unityengine.unity3d", "lua_protobuf.unity3d", "lua_misc.unity3d", "lua_socket.unity3d", "lua_system_reflection.unity3d" };
#endif
        bundleCount = list.Count;

        for (int i = 0; i < list.Count; i++)
        {
            string str = list[i];

#if UNITY_ANDROID && !UNITY_EDITOR
            string path = streamingPath + "/" + LuaConst.osDir + "/" + str;
#else
            string path = "file:///" + streamingPath + "/" + LuaConst.osDir + "/" + str;
#endif
            string name = Path.GetFileNameWithoutExtension(str);
            StartCoroutine(CoLoadBundle(name, path));            
        }

        yield return StartCoroutine(LoadFinished());//所有下载bundle的指令发出之后就进这里等着了
    }

    void Awake()
    {
#if UNITY_5 || UNITY_2017 || UNITY_2018
        Application.logMessageReceived += ShowTips;
#else
        Application.RegisterLogCallback(ShowTips);
#endif
        LuaFileUtils file = new LuaFileUtils();
        file.beZip = true;
#if UNITY_ANDROID && UNITY_EDITOR
        if (IntPtr.Size == 8)
        {
            throw new Exception("can't run this in unity5.x process for 64 bits, switch to pc platform, or run it in android mobile");
        }
#endif
        StartCoroutine(LoadBundles());  //awake的时候就开始请求www
    }

    void ShowTips(string msg, string stackTrace, LogType type)
    {
        tips += msg;
        tips += "\r\n";
    }

    void OnGUI()
    {
        GUI.Label(new Rect(Screen.width / 2 - 200, Screen.height / 2 - 150, 400, 300), tips);
    }

    void OnApplicationQuit()
    {
#if UNITY_5 || UNITY_2017 || UNITY_2018
        Application.logMessageReceived -= ShowTips;
#else
        Application.RegisterLogCallback(null);
#endif
    }

    
    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;
    }    
    
}

怎么运行:

lua -> build bundle files no jit

 

容我猜猜:

首先清空当前GetOS()下的streamAssets下面的文件夹

这个是build出来的bundle的目标路径

然后创建一个temp文件夹

assets/luaassets/luaframework/tolua/luacopytemp下面

获得temp下面的所有dir

然后对temp/lua下面遍历进行BuildLuaBundle

TODO:这个函数具体实现再看看

 

最后还是用BuildPipeline.BuildAssetBundles打包到streamingAssets/getOs()下面

 

代码节选:

ToLua# Example 18 TestABLoader_第1张图片

 

 

打出的包在StreamingAssets/GetOs()下面

ToLua# Example 18 TestABLoader_第2张图片

lua.unity3d这样的文件就是打出来的包

lua.unity3d.manifest:

ToLua# Example 18 TestABLoader_第3张图片

我们执行Main.Lua就是在这里执行了

怎么知道DoFile的时候在哪儿找lua?

是通过AddSearchPath接口加的搜索路径

然后DoFile的时候zipMap.TryGetValue(zipName, out zipFile);

 

 

首先是WWW win文件

通过win文件读出menifest

都有哪些bundle

对每一个bundle执行StartCoroutine(CoLoadBundle(name, path));

每一个bundle下载完之后执行:

LuaFileUtils.Instance.AddSearchBundle(name, www.assetBundle);

你可能感兴趣的:(打包,lua,unity)