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); } } }
相关文章推荐
- 绘制准心
- Unity3d游戏开发经验
- Unity3D NGUI制作的Button放到场景中,按钮从2D变到3D
- unity 引入 ios 第三方sdk
- 高通AR Unity播放器特性
- Unity3d:地铁跑酷操控解析
- Unity3d模型动画回归原位---处理
- 关于Unity 2d动画实现
- 使用SVN对Unity进行版本控制
- 當Unity遇上SVN!版本控管的二、三事
- Unity3d面试题积累
- Unity中鼠标拾取的检测代码
- 基于Unity的多线程之间的事件派发
- unity 搭建聊天室
- unity
- Unity3D LitJson 使用方法
- 一些有用的unity3d 、webgl网址
- unity3d shader中RenderType的所有类型
- Unity3d的场景音效静音处理
- Unity3D学习笔记-编写脚本常常出现的错误