Unity5.x版本AssetBundle加载研究
之前说了 “Unity5.x版本AssetBundle打包研究”,没看过的请先看一下:http://www.shihuanjue.com/?p=57
再来看本文,有一定的连接性。
先梳理一下思路:
要加载一个资源A,必须先去加载它的所有依赖资源
要知道这个资源A依赖了哪些资源,必须先去加载AssetBundleManifest
通过AssetBundleManifest对象的GetAllDependencies(A)方法,获取它依赖的所有资源。
依赖资源都加载了,就可以去真正加载资源A了。
注意点:
1.资源A加载完了后,要记得Unload(false),资源A的依赖资源要在 资源A加载完成后,才能Unload(false),否则无法正常加载资源A
2.不Unload的话也可以,那就自己做一个字典记录所有加载过的AssetBundle,还有它们的引用计数器。那样就可以先判断是否存在,然后再确定是否要去加载。
我下面的例子,采用1的方式。就是加载完了后就Unload的方式。
上代码
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System; public class AssetBundleLoaderMgr : Singleton<AssetBundleLoaderMgr>
{
public string m_assetPath = Application.streamingAssetsPath;
string assetTail = ".unity3d"; #region LoadAssetBundle /// <summary>
/// 加载目标资源
/// </summary>
/// <param name="name"></param>
/// <param name="callback"></param>
public void LoadAssetBundle(string name, Action<UnityEngine.Object> callback)
{
name = name + assetTail;//eg:ui/panel.unity3d Action<List<AssetBundle>> action = (depenceAssetBundles) =>
{ string realName = this.GetRuntimePlatform() + "/" + name;//eg:Windows/ui/panel.unity3d LoadResReturnWWW(realName, (www) =>
{
int index = realName.LastIndexOf("/");
string assetName = realName.Substring(index + );
assetName = assetName.Replace(assetTail, "");
AssetBundle assetBundle = www.assetBundle;
UnityEngine.Object obj = assetBundle.LoadAsset(assetName);//LoadAsset(name),这个name没有后缀,eg:panel //卸载资源内存
assetBundle.Unload(false);
for (int i = ; i < depenceAssetBundles.Count; i++)
{
depenceAssetBundles[i].Unload(false);
} //加载目标资源完成的回调
callback(obj);
}); }; LoadDependenceAssets(name, action);
} /// <summary>
/// 加载目标资源的依赖资源
/// </summary>
/// <param name="targetAssetName"></param>
/// <param name="action"></param>
private void LoadDependenceAssets(string targetAssetName, Action<List<AssetBundle>> action)
{
Debug.Log("要加载的目标资源:" + targetAssetName);//ui/panel.unity3d
Action<AssetBundleManifest> dependenceAction = (manifest) =>
{
List<AssetBundle> depenceAssetBundles = new List<AssetBundle>();//用来存放加载出来的依赖资源的AssetBundle string[] dependences = manifest.GetAllDependencies(targetAssetName);
Debug.Log("依赖文件个数:" + dependences.Length);
int length = dependences.Length;
int finishedCount = ;
if (length == )
{
//没有依赖
action(depenceAssetBundles);
}
else
{
//有依赖,加载所有依赖资源
for (int i = ; i < length; i++)
{
string dependenceAssetName = dependences[i];
dependenceAssetName = GetRuntimePlatform() + "/" + dependenceAssetName;//eg:Windows/altas/heroiconatlas.unity3d //加载,加到assetpool
LoadResReturnWWW(dependenceAssetName, (www) =>
{
int index = dependenceAssetName.LastIndexOf("/");
string assetName = dependenceAssetName.Substring(index + );
assetName = assetName.Replace(assetTail, "");
AssetBundle assetBundle = www.assetBundle;
UnityEngine.Object obj = assetBundle.LoadAsset(assetName);
//assetBundle.Unload(false);
depenceAssetBundles.Add(assetBundle); finishedCount++; if (finishedCount == length)
{
//依赖都加载完了
action(depenceAssetBundles);
}
});
}
}
};
LoadAssetBundleManifest(dependenceAction);
} /// <summary>
/// 加载AssetBundleManifest
/// </summary>
/// <param name="action"></param>
private void LoadAssetBundleManifest(Action<AssetBundleManifest> action)
{
string manifestName = this.GetRuntimePlatform();
manifestName = manifestName + "/" + manifestName;//eg:Windows/Windows
LoadResReturnWWW(manifestName, (www) =>
{
AssetBundle assetBundle = www.assetBundle;
UnityEngine.Object obj = assetBundle.LoadAsset("AssetBundleManifest");
assetBundle.Unload(false);
AssetBundleManifest manif = obj as AssetBundleManifest;
action(manif);
});
}
#endregion #region ExcuteLoader
public void LoadResReturnWWW(string name, Action<WWW> callback)
{
string path = "file://" + this.m_assetPath + "/" + name;
Debug.Log("加载:" + path);
StartCoroutine(LoaderRes(path, callback));
} IEnumerator LoaderRes(string path, Action<WWW> callback)
{
WWW www = new WWW(path);
yield return www;
if (www.isDone)
{
callback(www);
}
}
#endregion #region Util
/// <summary>
/// 平台对应文件夹
/// </summary>
/// <returns></returns>
private string GetRuntimePlatform()
{
string platform = "";
if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)
{
platform = "Windows";
}
else if (Application.platform == RuntimePlatform.Android)
{
platform = "Android";
}
else if (Application.platform == RuntimePlatform.IPhonePlayer)
{
platform = "IOS";
}
else if (Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXEditor)
{
platform = "OSX";
}
return platform;
}
}
#endregion
测试一下咯
using UnityEngine;
using System.Collections;
using System.Collections.Generic; public class TestAssetBundleLoader : MonoBehaviour
{
Dictionary<string, GameObject> GameObjectPool = new Dictionary<string, GameObject>(); void Start()
{
//要加载的资源的队列
Queue<string> needLoadQueue = new Queue<string>();
needLoadQueue.Enqueue("ui/plane");
needLoadQueue.Enqueue("ui/cube");
needLoadQueue.Enqueue("ui/sphere"); Load(needLoadQueue);
} void Load(Queue<string> needLoadQueue)
{
if (needLoadQueue.Count > )
{
string needLoadAssetName = needLoadQueue.Dequeue();
AssetBundleLoaderMgr.Instance.LoadAssetBundle(needLoadAssetName, (obj) =>
{
GameObject go = GameObject.Instantiate(obj) as GameObject;
int index = needLoadAssetName.LastIndexOf("/");
string assetName = needLoadAssetName.Substring(index + ); //加载出来的GameObject放到GameObjectPool存储
GameObjectPool.Add(assetName, go); Load(needLoadQueue);
});
}
else
{
Debug.Log("all finished");
}
}
}
有图有真相
可能代码写的还有很多优化的地方 ,毕竟100个人有100种写法,思路才是最重要的。你说呢?
其实我想说的是,代码都在了,自己尝试一下吧!
- 本文固定链接: http://www.shihuanjue.com/?p=83
- 转载请注明: 乔 2015年09月10日 于 是幻觉 发表
Unity5.x版本AssetBundle加载研究的更多相关文章
- Unity5系列资源管理AssetBundle——加载
上次我们进行了AssetBundle打包,现在我们还把打包的资源加载到我们的游戏中.在加载之前,我们需要把打包好的Bundle包裹放到服务器上,如果没有,也可以使用XAMPP搭建本地服务器. 加载的A ...
- AssetBundle加载API
AssetBundle加载API 在Unity 5当中,可以通过4个不同的API来加载AssetBundle,4个API可以用两个条件来区分: AssetBundle是 LZMA压缩. LZ4压缩还是 ...
- Unity5.x版本AssetBundle打包研究
Unity5的AssetBundle打包机制和以前版本不太一样.简单的说就是,只要给你要打包的资源设置一个AssetBundleName ,Unity自身会对这些设置了名字的资源进行打包,如果一个资源 ...
- Unity Lightmap动态加载研究
什么情况下需要Lightmap? 移动平台上目前暂时还不能开实时光影效果,会卡成幻灯片.所以就需要将光影烘焙到贴图上. 什么情况下需要动态加载Lightmap? 1.当项目抛弃了Unity的多场景模式 ...
- U3D assetbundle加载与卸载的深入理解
using UnityEngine; using System.Collections; using System; public class testLoadFromAB : MonoBehavio ...
- U3D assetbundle加载
using UnityEngine; using System.Collections; public class testLoadFromAB : MonoBehaviour { IEnumerat ...
- 加载MySQL、Oracle、SQL Server 2000、SQL Server 2005及以上版本 的加载数据库驱动程序
2018-11-04 20:00:59 开始 //getConnection(String url, String user, String password) //url:连接数据库的URL 3 ...
- Unity5-ABSystem(三):AssetBundle加载
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/lodypig/article/detai ...
- 关于springboot2.*版本无法加载静态资源
前言 在学习springboot的过程中,发现无法引用静态资源.我使用的是springboot2.2.1版本. 追溯源码,终于解决.并记录下解决思路. 默认加载路径 首先得知道springboot默认 ...
随机推荐
- Yii 同域名的单点登录 SSO实现
SSO (Single Sign-on) 顾名思义就是几个子项目共用一个登录点. 原理简单来说就是服务端session 共享, 客户端跨域cookies. 实现非常简单,protected/confi ...
- 《linux系统及其编程》实验课记录(六)
实验 6:Linux 文件系统 实验环境: 安装了 Red Hat Enterprise Linux 6.0 可运行系统,并且是成功验证系统.有另外一个无特权用户 student,密码 student ...
- POJ3321 Apple Tree(树状数组)
先做一次dfs求得每个节点为根的子树在树状数组中编号的起始值和结束值,再树状数组做区间查询 与单点更新. #include<cstdio> #include<iostream> ...
- MS SQL 合并结果集并求和 分类: SQL Server 数据库 2015-02-13 10:59 92人阅读 评论(0) 收藏
业务情景:有这样一张表:其中Id列为表主键,Name为用户名,State为记录的状态值,Note为状态的说明,方便阅读. 需求描述:需要查询出这样的结果:某个人某种状态的记录数,如:张三,待审核记录数 ...
- hdu 4034 2011成都赛区网络赛 逆向floyd **
给出一个最短路邻接矩阵,求出构图的最小边数 正常的floyd的k放在最外面是为了防止i到j的距离被提前确定,而逆向的floyd,i到j的距离已经确定,所以需要在i到j之间枚举k,注意需要break,否 ...
- 用.NET开发通用Windows App
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:随着Windows 10的正式发布,作为.NET开发人员应该开始或多或少了解一下通用( ...
- HDU 2242 考研路茫茫——空调教室 无向图缩环+树形DP
考研路茫茫——空调教室 Problem Description 众所周知,HDU的考研教室是没有空调的,于是就苦了不少不去图书馆的考研仔们.Lele也是其中一个.而某教室旁边又摆着两个未装上的空调,更 ...
- I/O复用模型之epoll学习
简介: epoll是linux下多路复用I/O接口select/poll的增强版,它能够显著提高程序在大量并发连接中只有少量活跃的情况下的系统cpu利用率,原因是它会复用文件描述符集合来传递结果而不用 ...
- 让Web API支持$format参数的方法
public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web AP ...
- 【转】Struts2中json插件的使用
配置注意点: 在原有Struts2框架jar包的引入下,需要额外多加一个Json的插件包(struts2-json-plugin-2.3.7.jar) 在struts.xml配置文件中,包需要继承js ...