Unity3D LuaBundleLoader(基于cslua)

Posted MrBlue

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity3D LuaBundleLoader(基于cslua)相关的知识,希望对你有一定的参考价值。

说明:异步加载lua的bundle,会优先加载cache目录下bundle(一般更新的资源都在cache下)

 

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

public class LuaBundleLoader : MonoBehaviour {

    public delegate void DelegateLoading(int idx, int total, string bundleName, string path);
    public delegate void DelegateLoadOver();

    //正在加载中回掉
    public DelegateLoading OnLoading;

    //加载完成回掉
    public DelegateLoadOver OnLoadOver;

    //总共要加载的bundle个数
    private int mTotalBundleCount = 0;

    //当前已加载的bundle个数
    private int mBundleCount = 0;

#if UNITY_5
    public void LoadBundle(string dir, string bundleName)
#else
    public void LoadBundle(string dir, List<string> bundleList)
#endif
    {
        StartCoroutine(LoadBundles(dir, bundleName));
    }

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

            mBundleCount++;
            if (null != OnLoading)
            {
                try
                {
                    LuaFileUtils.Instance.AddSearchBundle(name, www.assetBundle);
                    OnLoading(mBundleCount, mTotalBundleCount, name, path);
                }
                catch (Exception e)
                {
                    Debug.LogError(e.Message);
                }
                
            }
            www.Dispose();
        }
    }


#if UNITY_5
    private IEnumerator LoadBundles(string dir,string bundleName)                
#else
    private IEnumerator LoadBundles(string dir,List<string> bundleList)
#endif
    {
        var cachePath = Application.temporaryCachePath.Replace(\\, /);
        var streamingPath = Application.streamingAssetsPath.Replace(\\, /);

        List<string> list = new List<string>();

#if UNITY_5
        
        var bundlePath = cachePath+"/"+dir+"/"+bundleName;
        if (!File.Exists(bundlePath))
        {
            bundlePath = streamingPath + "/" + dir + "/" + bundleName;
        }
        else
        {
#if UNITY_android && !UNITY_EDITOR
            bundlePath = "file:///" + bundlePath;
#endif
        }
#if UNITY_ANDROID && !UNITY_EDITOR
        
#else
        bundlePath = "file:///" + bundlePath;
#endif
        using (WWW www = new WWW(bundlePath))
        {
            yield return www;

            AssetBundleManifest manifest = (AssetBundleManifest)www.assetBundle.LoadAsset("AssetBundleManifest");
            list = new List<string>(manifest.GetAllAssetBundles());
            //www.assetBundle.Unload(true);
            www.Dispose();
        }
#else
        list = bundleList;
#endif
        mTotalBundleCount = list.Count;

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

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

        yield return StartCoroutine(CheckLoadFinish());
    }

    IEnumerator CheckLoadFinish()
    {
        while (mBundleCount < mTotalBundleCount)
        {
            yield return null;
        }

        if (null != OnLoadOver)
        {
            try
            {
                OnLoadOver();
            }
            catch (Exception e)
            {
                Debug.LogError(e.Message);
            }
            
        }
    }

}

 

使用代码

var loader = GetComponent<LuaBundleLoader>();
			if (null == loader)
			{
				loader = gameObject.AddComponent<LuaBundleLoader>();
			}

			loader.OnLoading = (idx, total, bundleName, path) =>
			{
				Debug.Log(path+"    ok");
			};

			loader.OnLoadOver = OnBundleLoadOver;

			loader.LoadBundle(LuaConst.osDir, LuaConst.osDir);

  

以上是关于Unity3D LuaBundleLoader(基于cslua)的主要内容,如果未能解决你的问题,请参考以下文章

使用 Unity3D 进行 Ninject

Unity3D 关于飞行类型集合

Unity3D 实现简单的语音聊天 [Android版本]

Unity3D多线程之页面和窗口管理

由于对象的当前状态,操作无效 - Linq on List

请问unity3d资源如何打包