您的位置:首页 > 移动开发 > Unity3D

Unity3d Assetbundle代码示例

2014-01-06 21:04 911 查看
最近看了比较多Assetbundle方面的东西,官网上有一段代码写的不错可以拿来参考一
using UnityEngine;
using System.Collections;

using System.Collections.Generic;
using System;

static public class AssetBundleManager
{
//定义一个字典来保持Assetbundle的引用
static private Dictionary<string, AssetBundleRef> dictAssetBundleRefs;
public static float progress;
public static WWW www;
static AssetBundleManager()
{
dictAssetBundleRefs = new Dictionary<string, AssetBundleRef>();
}

//AssetBundle的信息
private class AssetBundleRef
{
public AssetBundle assetBundle = null;
public int version;
public string url;
public AssetBundleRef(string ursStr, int VersionIn)
{
url=ursStr;
version = VersionIn;
}
}
//获得AssetBundle
public static AssetBundle getAssetBundle(string url, int version)
{
string keyName = url + version.ToString();
AssetBundleRef abRef;
if (dictAssetBundleRefs.TryGetValue(keyName, out abRef))
return abRef.assetBundle;
else
return null;
}
//下载AssetBudle
public static IEnumerator downloadAssetBundle(string url, int version)
{
string keyName = url + version.ToString();
Debug.Log(url);
if (dictAssetBundleRefs.ContainsKey(keyName))
yield return null;
else
{
using ( www = WWW.LoadFromCacheOrDownload(url, version))
{
yield return www;
progress = www.progress;
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);
}
}
}
//完成之后卸载AssetBundle
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);
}
}
}





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: