先介绍一种常用的加载AssetBundle方法

  

using UnityEngine;
using System.Collections;
using System.IO;
public class LoadUnity3d : MonoBehaviour
{ // Use this for initialization
void Start()
{
StartCoroutine(LoadScene());
}
// Update is called once per frame
void Update()
   {    }
IEnumerator LoadScene() {
//文件路径,也就是我们打包的那个
WWW www = new WWW("file:///" + Application.dataPath + "/Bundles/My Prefab.unity3d");
yield return www;
Instantiate(www.assetBundle.mainAsset);
}
}

第二种,将www文件读成字节进行同步加载

 using UnityEngine;
using System.Collections; public class ExampleClass : MonoBehaviour
{
/// <summary>
/// 返回字节数组
/// </summary>
/// <param name="binary"></param>
/// <returns></returns>
byte[] MyDecription(byte[] binary)
{
byte[] decrypted;
return decrypted;
}
IEnumerator Start()
{
WWW www = new WWW("http://myserver/myBundle.unity3d"); //带有后缀的文件路径,可以是网络也可以是本地
yield return www;
byte[] decryptedBytes = MyDecription(www.bytes); //返回字节数组
AssetBundle assetBundle = AssetBundle.LoadFromMemory(decryptedBytes); //从内存中同步加载资源资源包
yield return assetBundle;
if (assetBundle != null) //如果资源包不为空
{
Instantiate(assetBundle.LoadAsset<GameObject>("myBundle"); //根据名字从资源包中加载对应资源
}
}
}

第三种 异步

 using UnityEngine;
using System.Collections; public class ExampleClass : MonoBehaviour
{
/// <summary>
/// 返回字节数组
/// </summary>
/// <param name="binary"></param>
/// <returns></returns>
byte[] MyDecription(byte[] binary)
{
byte[] decrypted;
return decrypted;
}
IEnumerator Start()
{
WWW www = new WWW("http://myserver/myBundle.unity3d"); //带有后缀的文件路径,可以是网络也可以是本地
yield return www;
byte[] decryptedBytes = MyDecription(www.bytes); //返回字节数组
AssetBundleCreateRequest bundle = AssetBundle.LoadFromMemoryAsync(decryptedBytes);//从内存中异步加载资源资源包
yield return bundle;
if (bundle != null && bundle.assetBundle != null)
{
Instantiate(bundle.assetBundle.LoadAsset<GameObject>("myBundle"));//根据名字从资源包中加载对应资源
}
}
}

下面贴上项目中使用的方法 和异步加载很像,只是有些简单的实现进行了封装

   private IEnumerator LoadBundle(ModelGouJian _model)
{
string filePath = string.Format("{0}/_lesson/ModelFbx/{1}/{2}.unity3d", PublicJs.Datapath, _model.JieDianID, _model.ModelFbx);
byte[] decryptedData = FileHelp.Instance.GetFileTextByteEsc(filePath); //返回字节数组
AssetBundleCreateRequest bundle = AssetBundle.LoadFromMemoryAsync(decryptedData); //从内存中加载资源包
yield return bundle;
if (bundle != null && bundle.assetBundle != null)
{
_model.ModelObj = Instantiate(bundle.assetBundle.LoadAsset<GameObject>(_model.ModelFbx)); //根据资源名加载资源包中资源。
_model.ModelObj.name = _model.ModelFbx;
_model.ModelObj.transform.localPosition = new Vector3(, , );
if (_model.ModelObj.name != gouJianData.CurSelectGouJian.ModelFbx) { _model.ModelObj.SetActive(false); }
bundle.assetBundle.Unload(false);
bundle = null;
}
else
{
LogHelp.Write("模型下载错误:{0}", filePath);
}
yield return new WaitForSeconds(0.5f);
iNum++;
//判断模型是否下载完成
if (IsLoadModel == true && iNum == iCount)
{
IsLoadModel = false;
this.LoadModelEnd();
}
}

对加载AssetBundle做一小结,以后补充。

欢迎广大Unity兴趣爱好者加群学习165628892(进群备注:博客)  随时提出问题解决问题!

树欲静而风不止,子欲养而亲不待!

2016年12月22日

加载AssetBundle方法的更多相关文章

  1. 加载 AssetBundle 的四种方法

    [加载 AssetBundle 的四种方法] 1.AssetBundle.LoadFromMemoryAsync(byte[] binary, uint crc = 0); 返回AssetBundle ...

  2. js jquery 页面加载初始化方法

    js jquery 页面加载初始化方法 一.js页面加载初始化方法 // 1.在body里面写初始化方法. <body onload='init()'> </body> < ...

  3. ARM架构下linux设备树加载的方法

    引入设备树后bootloader加载DTB方法: 1. 标准方法 将linux kernel放到内存地址为<kernel img addr>的内存中. 将DTB放到地址为<dtb a ...

  4. Android图片异步加载的方法

    很多时候,我们在加载大图片或者需要处理较多图像数据的时候,希望显示效果能好点,不至于因为图片解码耗时产生ANR等情况,不得不说异步加载是个不错的方法.说到异步加载,避免application出现ANR ...

  5. 动态加载css方法实现和深入解析

    一.方法引用来源和应用  此动态加载css方法 loadCss,剥离自Sea.js,并做了进一步的优化(优化代码后续会进行分析).  因为公司项目需要用到懒加载来提高网站加载速度,所以将非首屏渲染必需 ...

  6. AngularJS进阶(三十八)上拉加载问题解决方法

    AngularJS上拉加载问题解决方法 项目中始终存在一个问题:当在搜索栏输入关键词后(见图1),按照既定的业务逻辑应该是服务端接收到请求后,首先返回查询的前7条数据,待客户端出现上拉加载时,继续查找 ...

  7. Google官方网络框架-Volley的使用解析Json以及加载网络图片方法

    Google官方网络框架-Volley的使用解析Json以及加载网络图片方法 Volley是什么? Google I/O 大会上,Google 推出 Volley的一个网络框架 Volley适合什么场 ...

  8. 打包加载 AssetBundle

    1.先创建Asset序列化(单个文件夹所在文件夹路径,会遍历这个文件夹所有的Prefab,所有的Prefab名字不能重复,必须保证名字得唯一性),配置好ConfigAB表 /* ######### # ...

  9. jQuery:实现图片按需加载的方法,当要显示内容的高度超过了页面的高度,按需加载,根据滚动条的位置来判断页面显示的内容

    实现图片按需加载的方法,当要显示内容的高度超过了页面的高度,按需加载,根据滚动条的位置来判断页面显示的内容 这个类似于京东或淘宝页面,根绝页面的滚动,显示下面的内容 如下图所示,一开始并不是所有的图片 ...

随机推荐

  1. python PIL Image模块

    原地址:http://hi.baidu.com/drunkdream/item/9c9ac638dfc46ec6382ffac5 实验环境: windows7+python2.6+pycrust+PI ...

  2. 0x00linux32位汇编初入--前期准备

    0x00汇编初入--前期准备 一.汇编工具 在linux平台下常用的编译器为as,连接器为ld,使用的文本编辑器为vim,汇编语法为att 以下是一些工具: addr2line 把地址转换为文件名和行 ...

  3. IDEA使用maven中tomcat插件启动项目乱码问题

    今天用IDEA来启动项目,使用的是maven中的tomcat7插件,正常启动后,再页面操作新增或修改数据时,发生了诡异的事, 中文保存后全部乱码...顿时不淡定了,接着就开始排查原因 首先检查IDEA ...

  4. 简述Session

    Session的原理 1.session技术的概述 * session是服务器端技术 * 服务器在运行时可以为每一个用户的浏览器创建一个其独享的session对象 * 由于session为用户浏览器独 ...

  5. arcgis 10.2连接Oracle

  6. redis缓存

    参考: java对redis的基本操作 http://www.cnblogs.com/edisonfeng/p/3571870.html 一.支持类型: key:一般设计为标准的字符串, values ...

  7. 黄聪:C#Winform程序如何发布并自动升级(图解)

    有不少朋友问到C#Winform程序怎么样配置升级,怎么样打包,怎么样发布的,在这里我解释一下打包和发布关于打包的大家可以看我的文章C# winform程序怎么打包成安装项目(图解)其实打包是打包,发 ...

  8. 【学】jQuery的源码思路1——后代选择器

    jQuery的源码思路1--后代选择器 这里探讨一下jQuery中后代选择器的封装原理,并自己写一下 getEle('#div1 ul li .box');接受的参数就是个后代选择器,类似于这样: # ...

  9. (error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about t

    运行redis过程中,突然报错如下: (error) MISCONF Redis is configured to save RDB snapshots, but is currently not a ...

  10. Nhibernate对应关系参数介绍

    一.多对一关联映配置介绍 <many-to-one name="PropertyName" 属性名 column="column_name" 数据库字段名 ...