首页 > 编程语言 > 详细

Unity3D LuaBundleLoader(基于cslua)

时间:2016-06-08 19:09:07      阅读:270      评论:0      收藏:0      [点我收藏+]

说明:异步加载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)

原文:http://www.cnblogs.com/mrblue/p/5571135.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!