上次我们进行了AssetBundle打包,现在我们还把打包的资源加载到我们的游戏中。
在加载之前,我们需要把打包好的Bundle包裹放到服务器上,如果没有,也可以使用XAMPP搭建本地服务器。

加载的AssetBundle文件是放在内存中的,所以如果没有很好的管理,会使游戏性能大打折扣,因此,我们在这里最好使用缓存策略。

我们的AssetBundle:

 using System;
using UnityEngine; namespace MrCAssetFramework
{
public sealed class MrCAssetBundle
{
internal AssetBundle m_AssetBundle;
internal string m_AssetBundleName;
internal MrCAssetBundle(AssetBundle assetBundle,string name)
{
this.m_AssetBundle = assetBundle;
this.m_AssetBundleName = name;
this.m_ReferencedCount = ;
} #region ReferenceCountManage
//yinyongjishujizhi
private int m_ReferencedCount;
public void Retain()
{
this.m_ReferencedCount++;
}
public void Release(){
this.m_ReferencedCount--;
//当引用计数为0时,卸载资源
if (this.m_ReferencedCount == )
{
this.m_AssetBundle.Unload (true);
MrCAssetCache.FreeBundle(this.m_AssetBundleName);
}
} public int RetainCount()
{
return this.m_ReferencedCount;
}
#endregion }
}

AssetBundle管理:

 using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic; namespace MrCAssetFramework
{
public class MrCAssetManager:MonoBehaviour
{ #region Singleton
private static GameObject s_go_MrCAssetManager = null;
private static MrCAssetManager s_MrCAssetManager = null; public static MrCAssetManager DefaultAssetManager{
get{
if (s_MrCAssetManager == null) {
s_go_MrCAssetManager = new GameObject ("LOAssetManager");
s_MrCAssetManager = s_go_MrCAssetManager.AddComponent<MrCAssetManager> ();
} return s_MrCAssetManager;
}
} #endregion public static string URI{ set; get;}
public static string ManifestName{ set; get;}
//无返回值泛型委托..
public static Action<bool> InitBlock; public AssetBundleManifest manifest{ set; get;}      ///<summary>
/// To get the asset..
/// </summary> /// <returns>The asset..</returns>
public T GetAsset<T>(string assetbundlename,string assetname) where T:UnityEngine.Object
{
MrCAssetBundle lab = MrCAssetCache.GetBundleCache(assetbundlename); if (lab == null) {
return null;
}
else
{
return lab.m_AssetBundle.LoadAsset<T>(assetname);
}
} IEnumerator LoadManifestBundle()
{
//缓存中已经存在请求的bundle,中止..
if (MrCAssetCache.InCache(MrCAssetManager.ManifestName)) {
yield break;
} // 通过网络下载AssetBundle
WWW www = IsLoadAssetBundleAtInternal(MrCAssetManager.ManifestName);
yield return www; this.manifest = this.GetAsset<AssetBundleManifest>(MrCAssetManager.ManifestName,"AssetBundleManifest");
MrCAssetManager.InitBlock (this.manifest != null);
}
void Start()
{
StartCoroutine ("LoadManifestBundle");
} #region 加载包裹系列函数      ///<summary>
/// 检查是否已经从网络下载
/// </summary> protected WWW IsLoadAssetBundleAtInternal (string assetBundleName)
{
//已经存在了呀
MrCAssetBundle bundle = MrCAssetCache.GetBundleCache(assetBundleName);
if (bundle != null)
{
//保留数加一次
bundle.Retain ();
return null;
} //如果WWW缓存策略中包含有对应的关键字,则返回true
if (MrCAssetCache.InCache (assetBundleName)) {
return null;
} //创建下载链接
WWW www = new WWW(MrCAssetManager.URI + assetBundleName);
// 按版本号,按需要通过网络下载AssetBundle,一般在正式游戏版本中,不使用上面的,因为它会每次打开游戏重新下载
//WWW www = WWW.LoadFromCacheOrDownload(LOAssetManager.URI + assetBundleName, nowVersion); //加入缓存策略
MrCAssetCache.SetWWWCache(assetBundleName,www); return www;
} IEnumerator LoadDependencies(string assetBundleName)
{
if (this.manifest == null) {
yield return null;
}
// 获取依赖包裹
string[] dependencies = this.manifest.GetAllDependencies(assetBundleName); Debug.Log(dependencies.Length); if (dependencies.Length == )
{
yield return null;
} // 记录并且加载所有的依赖包裹
MrCAssetCache.SetDependCache(assetBundleName, dependencies); for (int i = ; i < dependencies.Length; i++)
{
yield return IsLoadAssetBundleAtInternal (dependencies [i]);
}
} 136     /// <summary>
/// 加载资源包
/// </summary> IEnumerator LoadAssetBundle(string assetBundleName)
{
if (MrCAssetCache.InCache(assetBundleName)) {
yield break;
}
// 通过网络下载AssetBundle
WWW www = IsLoadAssetBundleAtInternal(assetBundleName);
yield return www; // 通过网络加载失败,下载依赖包裹
yield return StartCoroutine(LoadDependencies(assetBundleName));
}      ///<summary>
/// 异步加载资源
/// </summary> public IEnumerator LoadAssetAsync (string assetBundleName, string assetName, System.Type type)
{
//开始加载包裹
yield return StartCoroutine(LoadAssetBundle (assetBundleName));
}      ///<summary>
/// 异步加载场景
/// </summary> public IEnumerator LoadLevelAsync (string assetBundleName, string levelName, bool isAdditive)
{
//加载资源包
yield return StartCoroutine(LoadAssetBundle (assetBundleName)); }
#endregion #region Update void Update()
{
MrCAssetCache.Update();
}
#endregion
}
}

缓存管理:

 using System;
using System.Collections.Generic;
using UnityEngine; namespace MrCAssetFramework
{
internal sealed class MrCAssetCache
{
#region 包裹缓存机制
//创建缓存字典
private static Dictionary<string, MrCAssetBundle> assetBundleCache;
//缓存字典属性
private static Dictionary<string, MrCAssetBundle> BundleCache
{
get{
if (assetBundleCache == null) {
assetBundleCache = new Dictionary<string, MrCAssetBundle> ();
} return assetBundleCache;
}
} //创建缓存WWW对象
private static Dictionary<string, WWW> wwwCache;
//创建缓存WWW对象属性
private static Dictionary<string, WWW> WwwCache{
get{
if (wwwCache == null) {
wwwCache = new Dictionary<string, WWW> ();
} return wwwCache;
}
}
//创建依赖缓存对象
private static Dictionary<string, string[]> dependCache;
//创建依赖缓存属性
private static Dictionary<string, string[]> DependCache
{
get{
if (dependCache == null) {
dependCache = new Dictionary<string, string[]> ();
}
return dependCache;
}
} private static Dictionary<string, string> errorCache;
private static Dictionary<string,string> ErrorCache{
get{
if (errorCache == null) {
errorCache = new Dictionary<string, string> ();
}
return errorCache;
}
}      ///<summary>
/// Instantiate the cache.
/// </summary> /// <returns><c>true</c>, if cache was ined, <c>false</c> otherwise.</returns>
/// <param name="assetbundlename">Assetbundlename.</param>
internal static bool InCache(string assetbundlename)
{
return MrCAssetCache.BundleCache.ContainsKey(assetbundlename);
}
#endregion #region 卸载系列函数      ///<summary>
/// 卸载资源包和依赖包
/// </summary> /// <param name="assetBundleName">Asset bundle name.</param>
public static void UnloadAssetBundle(string assetBundleName)
{
UnloadAssetBundleInternal (assetBundleName);
UnloadDependencies (assetBundleName);
}
internal static void UnloadDependencies(string assetBundleName)
{
string[] dependencies = null;
//获取所有的依赖包名称
if (!MrCAssetCache.DependCache.TryGetValue(assetBundleName, out dependencies) )
return; //卸载依赖包
foreach(var dependency in dependencies)
{
UnloadAssetBundleInternal(dependency);
}
//删除依赖缓存策略
MrCAssetCache.DependCache.Remove(assetBundleName);
} internal static void UnloadAssetBundleInternal(string assetBundleName)
{
MrCAssetBundle bundle;
MrCAssetCache.BundleCache.TryGetValue(assetBundleName,out bundle); if (bundle == null)
{
return;
}
bundle.Release ();
}
#endregion #region GetFunction
internal static WWW GetWWWCache(string key)
{
WWW www; MrCAssetCache.WwwCache.TryGetValue(key,out www); return www;
}
internal static void SetWWWCache(string key,WWW value)
{
MrCAssetCache.WwwCache.Add(key,value);
} internal static MrCAssetBundle GetBundleCache(string key)
{
MrCAssetBundle ab; MrCAssetCache.BundleCache.TryGetValue(key,out ab); return ab;
}
internal static void SetBundleCache(string key,MrCAssetBundle value)
{
MrCAssetCache.BundleCache.Add(key,value);
} internal static string[] GetDependCache(string key)
{
string[] depends; MrCAssetCache.DependCache.TryGetValue(key,out depends); return depends;
}
internal static void SetDependCache(string key,string[] value)
{
MrCAssetCache.DependCache.Add(key,value);
} internal static string GetErrorCache(string key)
{
string error; MrCAssetCache.ErrorCache.TryGetValue(key,out error); return error;
}
internal static void SetErrorCache(string key,string value)
{
MrCAssetCache.ErrorCache.Add(key,value);
}
#endregion internal static void FreeBundle(string key)
{
MrCAssetCache.BundleCache.Remove(key);
} #region Update internal static void Update()
{
// Collect all the finished WWWs.
var keysToRemove = new List<string>();
foreach (var keyValue in MrCAssetCache.WwwCache)
{
WWW download = keyValue.Value;
string m_bundleName = keyValue.Key; // 下载失败
if (download.error != null)
{
MrCAssetCache.ErrorCache.Add(m_bundleName, download.error); keysToRemove.Add(m_bundleName); continue;
} // 下载成功
if(download.isDone)
{ MrCAssetCache.BundleCache.Add(m_bundleName, new MrCAssetBundle(download.assetBundle,m_bundleName)); keysToRemove.Add(m_bundleName);
}
} // 删除下载成功的WWW对象
foreach( var key in keysToRemove)
{
WWW download = MrCAssetCache.WwwCache[key]; MrCAssetCache.WwwCache.Remove(key); download.Dispose();
}
} #endregion
}
}

加载包裹中的场景:
新建一个场景,创建TestScript脚本,放到摄像机上。

 using UnityEngine;
using System.Collections;
//引入框架
using MrCAssetFramework; public class TestScript : MonoBehaviour { /// <summary>
/// 加载场景资源函数
/// </summary>
protected IEnumerator Load (string assetBundleName, string level)
{
IEnumerator b = da.LoadLevelAsync(assetBundleName, level, false);
yield return StartCoroutine(b);
Application.LoadLevel(level);
} MrCAssetManager da;
void Start ()
{
//指定统一资源标志符
MrCAssetManager.URI = "http://....../UnityFiles/AssetBundlesForBlog/";
//主配置文件
MrCAssetManager.ManifestName = "Others";
//加载成功后的操作代理
MrCAssetManager.InitBlock = ((bool bundleObj) => {
if (bundleObj) {
//协程操作加载的AssetBundle包裹
StartCoroutine (Load ("scenes/loaderscene.unity3d", "LoaderScene"));
}
});
//开始我们的一切
da = MrCAssetManager.DefaultAssetManager;
}
}

加载包裹中的游戏对象等资源到场景中:
在上面的测试脚本中,修改部分代码。

    /// <summary>
/// 加载游戏对象资源函数
/// </summary>
protected IEnumerator LoadObj(string assetBundleName, string obj)
{
IEnumerator b = da.LoadAssetAsync(assetBundleName, obj, typeof(GameObject));
yield return StartCoroutine(b);
GameObject go = da.GetAsset<GameObject>(assetBundleName, obj);
GameObject.Instantiate(go);
}
//-------------------------------------------------------------------------------
//加载成功后的操作代理
MrCAssetManager.InitBlock = ((bool bundleObj) => {
if (bundleObj) {
//协程操作加载的游戏对象包裹
StartCoroutine(LoadObj("prefabs/cube.prefab", "MyCube"));
}
});

Unity5系列资源管理AssetBundle——加载的更多相关文章

  1. Unity5.x版本AssetBundle加载研究

    之前说了 “Unity5.x版本AssetBundle打包研究”,没看过的请先看一下:http://www.shihuanjue.com/?p=57 再来看本文,有一定的连接性. 先梳理一下思路: 要 ...

  2. Unity5系列资源管理AssetBundle——更新实现

    前面我们研究了AssetBundle的打包与加载,现在我们来了解下如何在项目中根据版本号更新内容. 最最重要的一点,细心的朋友应该看到了在加载AssetBundle的MrcAssetManager类中 ...

  3. Unity5系列资源管理AssetBundle——打包

    资源管理是游戏开发的重要环节,Unity中使用AssetBundle可以非常方便地帮我们打包和更新游戏内容,在5系列中,AssetBundle更是方便好用,现在让我们先进行打包吧. 刚说了,5系列打包 ...

  4. AssetBundle加载API

    AssetBundle加载API 在Unity 5当中,可以通过4个不同的API来加载AssetBundle,4个API可以用两个条件来区分: AssetBundle是 LZMA压缩. LZ4压缩还是 ...

  5. 老调重弹:JDBC系列之<驱动加载原理全面解析) ----转

      最近在研究Mybatis框架,由于该框架基于JDBC,想要很好地理解和学习Mybatis,必须要对JDBC有较深入的了解.所以便把JDBC 这个东东翻出来,好好总结一番,作为自己的笔记,也是给读者 ...

  6. RX系列四 | RxAndroid | 加载图片 | 提交表单

    RX系列四 | RxAndroid | 加载图片 | 提交表单 说实话,学RxJava就是为了我们在Android中运用的更加顺手一点,也就是RxAndroid,我们还是先一步步来,学会怎么去用的比较 ...

  7. 【iOS系列】-UIWebView加载网页禁止左右滑动

    [iOS系列]-UIWebView加载网页禁止左右滑动 问题: 做项目时候,用UIWebView加载网页的时候,要求是和微信网页中打开的网页的效果一样,也即是只能上下滑动,不能左右滑动,也不能缩放. ...

  8. SpringBoot系列之配置文件加载位置

    SpringBoot系列之配置文件加载位置 SpringBoot启动会自动扫描如下位置的application.properties或者application.yml文件作为Springboot的默认 ...

  9. AssetBundle系列——资源的加载、简易的资源管理器

    每个需要进行资源管理的类都继承自IAssetManager,该类维护它所使用到的所有资源的一个资源列表.并且每个资源管理类可以重写其资源引用接口和解引用接口. 每个管理器有自己的管理策略,比如Scen ...

随机推荐

  1. java list<string>集合 传递值给js的数组

    转载地址:http://blog.sina.com.cn/s/blog_611f65fd0100msc6.html. 1.Action 中代码              List result = n ...

  2. 关于FileSystemXmlApplicationContext和ClassPathXmlApplicationContext路径问题

    在码代码的时候发现使用这两个方法写路径的时候总是存在问题,所以百度了一下解决了这个问题. 关于FileSystemXmlApplicationContext这个路径有2总写法 有盘符的代表的是绝对路径 ...

  3. JTable

    final Table table = new Table(parent, SWT.NONE | SWT.FULL_SELECTION); final GridData gd = new GridDa ...

  4. PHP检测文件能否下载

    用php代码检测一个文件是否可以下载,网上没有找到合适的代码,自己实现了一个还挺好用的,分享给有需要的朋友. 基本原理:使用http的HEAD方法,检测报文的头里httpcode是否为200. pub ...

  5. 个性化推荐系统中的BadCase分析

    针对内测用户反馈,由于前一天点击了几个动画,导致第二天推荐的动画屏占比较高,于是开始对此badcase进行分析. 首先分析了该用户的历史观看纪录,由于系统升级,日志缺陷问题,导致该用户10.15-11 ...

  6. 杂谈3之English

    1.面向过程(OPP):Orient Procedure Program (C语言) 2.面向对象(OOP):Orient ObjectProgram(Java) 3.面向对象的三大特征:继承Inhe ...

  7. EasyUI Messager 消息框

    通过 $.messager.defaults 重写默认的 defaults. 消息框(messager)提供不同样式的消息框,包括警示(alert).确认(confirm).提示(prompt).进展 ...

  8. umount: /home: device is busy

    转自:umount: /home: device is busy 取消挂载/home时出现umount: /home: device is busy,原因是因为有程序在使用/home目录,我们可以使用 ...

  9. Xcode使用小结2

    刷新时间慢的时候用timer定时器 以下内容为借用,作者:FlyElephant出处:http://www.cnblogs.com/xiaofeixiang iOS开发-NSOperation与GCD ...

  10. Python学习笔记——基础篇【第六周】——hashlib模块

    常用模块之hashlib模块 用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法 import ...