AssetBundle bundle = www.assetBundle;
Cannot load cached AssetBundle. A file of the same name is already loaded from another AssetBundle
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
static public class AssetBundleManager
{
/// <summary>
/// 保存AssetBundle资源包引用的容器,其中key值是下载地址+版本号的组合,value值是AssetBundleRef类数据
/// </summary>
static private Dictionary<string, AssetBundleRef> dictAssetBundleRefs;
static AssetBundleManager()
{
dictAssetBundleRefs = new Dictionary<string, AssetBundleRef>();
}
/// <summary>
/// 包含AssetBundle引用的类,其中包括url地址和version版本号
/// </summary>
private class AssetBundleRef
{
public AssetBundle assetBundle = null; //资源包
public int version; //版本号
public string url; //下载地址
public AssetBundleRef(string strUrlIn, int intVersionIn)
{
url = strUrlIn;
version = intVersionIn;
}
};
/// <summary>
/// 从保存的容器中获取资源包
/// </summary>
/// <param name="url">下载地址</param>
/// <param name="version">版本号</param>
/// <returns>返回获取的资源包</returns>
public static AssetBundle getAssetBundle(string url, int version)
{
string keyName = url + version.ToString();
AssetBundleRef abRef;
if (dictAssetBundleRefs.TryGetValue(keyName, out abRef)) //将空的abRef传递进去
return abRef.assetBundle;
else
return null;
}
/// <summary>
/// 下载AssetBundle资源包
/// </summary>
/// <param name="url">下载地址</param>
/// <param name="version">版本号</param>
/// <returns>返回下载的资源包</returns>
public static IEnumerator downloadAssetBundle(string url, int version)
{
string keyName = url + version.ToString();
if (dictAssetBundleRefs.ContainsKey(keyName))
yield return null;
else
{
while (!Caching.ready)
yield return null;
using (WWW www = WWW.LoadFromCacheOrDownload(url, version)) //异步方式加载
{
yield return www;
if (www.error != null)
throw new Exception("WWW download:" + www.error);
AssetBundleRef abRef = new AssetBundleRef(url, version);
abRef.assetBundle = www.assetBundle;
dictAssetBundleRefs.Add(keyName, abRef);
}
}
}
/// <summary>
/// 删除容器中保存的资源数据
/// </summary>
/// <param name="url">路径地址</param>
/// <param name="version">版本号</param>
/// <param name="allObjects">表示是否删除资源包的所有资源</param>
public static void Unload(string url, int version, bool allObjects)
{
string keyName = url + version.ToString();
AssetBundleRef abRef;
if (dictAssetBundleRefs.TryGetValue(keyName, out abRef))
{
abRef.assetBundle.Unload(allObjects);
abRef.assetBundle = null;
dictAssetBundleRefs.Remove(keyName);
}
}
}
注意:上面例子中的AssetBundleManager类是静态的,当加载一个新的场景时,引用的类中任何资源包信息都不会被销毁。using UnityEditor;
class ManagedAssetBundleExample : MonoBehaviour {
public string url; //地址路径
public int version; //版本号
AssetBundle bundle;
void OnGUI (){
if (GUILayout.Label ("Download bundle"){
bundle = AssetBundleManager.getAssetBundle (url, version);
if(!bundle)
StartCoroutine (DownloadAB());
}
}
IEnumerator DownloadAB (){
yield return StartCoroutine(AssetBundleManager.downloadAssetBundle (url, version));
bundle = AssetBundleManager.getAssetBundle (url, version);
}
void OnDisable (){ //当脚本执行时调用
AssetBundleManager.Unload (url, version, true);
}
}