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)
	{
		StartCoroutine(LoadBundles(dir, bundleName));
	}
#else
	public void LoadBundle(string dir, List<string> bundleList)
	{
		StartCoroutine(LoadBundles(dir, bundleList));
	}
#endif
	 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++;
			LuaFileUtils.Instance.AddSearchBundle(name, www.assetBundle);

			try
			{
				if (null != OnLoading)
				{
					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
	 public 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);

  

posted @ 2016-06-08 18:30  MrBlue  阅读(352)  评论(0编辑  收藏  举报