先介绍一种常用的加载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. 10天学会phpWeChat——第七天:创建一个自适应PC网站+H5移动端的模块

    本教程基于phpWeChat核心框架1.1.0+版本.下载地址:http://s.phpwechat.com/app_38026ed22fc1a91d92b5d2ef93540f20 通过前面六讲的系 ...

  2. Eclipse启动tomcat时出现报错-拒绝访问

    今天新建项目,当选择项目的发布路径为tomcat的路径时 启动tomcat出现如下错误: Publishing the configuration... Error copying file to D ...

  3. C# 下载搜狗词库

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); string[] userAgent = new string[]{& ...

  4. centos 7安装部署docker

    1.centos 7 在windows下通过vm虚拟机安装centos 7: VMware-workstation-full-10.0.3-1895310 centos 7 2.要求 操作系统的内核版 ...

  5. Oracle 用户管理与权限控制

    Oracle 用户管理与权限控制 oracle数据库的权限系统分为系统权限与对象权限.系统权限( database system privilege )可以让用户执行特定的命令集.例如,create ...

  6. TIOBE Index for January 2016(转载)

    Java has won the TIOBE Index programming language award of the year. This is because Java has the la ...

  7. 【学】jQuery的源码思路6——增加each,animaion,ajax以及插件机制

    each() 插件机制 animation ajax //each() //这里第一个参数指定将this指向每次循环到的那个元素身上,而第三个参数element其实就是this本身所以和第一个参数是一 ...

  8. C# 自动部署之附加数据库

    转自心存善念 原文 C# 自动部署之附加数据库 看着别人的网站能够自动安装,数据库自动附加,觉得很神奇很向往,但是始终米有去手动实践. 网上找了下资料,发现实现起来其实很简单 直接code priva ...

  9. Percona Toolkit 使用

    安装 percona-toolkit perl Makefile.PL make make test make install 默认安装到 /usr/local/bin 目录下 可能需要 DBI-1. ...

  10. java异常笔记

    1:<java核心技术卷一>473页提到:如果在子类中覆盖了超类的一个方法,子类方法中声明的已检查异常不能超过超类方法中声明的异常范围. 显然,如果子类中抛出的异常范围比超类还大.多态将无 ...