先介绍一种常用的加载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. 基本的HTML标签

    html的基本标签: <html><body> <h1>My First Heading</h1><h2>this second headi ...

  2. js 循环切换图片

    function changeLot(){ var minIndex = 1; var maxIndex = 100; var curIndex = 10; var src = $("ul ...

  3. WPF 打印

    1. System.Windows.Controls.PrintDialog printDialog = new System.Windows.Controls.PrintDialog(); if ( ...

  4. Linux-Rsync服务器/客户端搭建实战

    一.需求 每晚汇总各机器的操作日志,同步到主服务器进行日志分析. 二.基础知识 rsync 分为服务器端.客户端,服务器端搭建比客户端辛苦一些(也是很简单). rsync 服务器是指以 deamon ...

  5. ASP.NET Web 应用程序及页面生命周期

    以客户端浏览器向 ASP.NET Web 应用程序页面发送请求(Request)为起点,以浏览器收到 Web 服务器的响应(Response)为终点,这一完整的过程被称为"应用程序及页面的生 ...

  6. python海龟图制作

    海龟画图很好看,先上图形: 依据代码注释随意打印出来就行: #!/usr/bin/python3.4 # -*- coding: utf-8 -*- import turtle # 拿起一支笔 t = ...

  7. 0.AutoMapper核心

    AutoMapper是基于约定的对象 - 对象映射器.AutoMapper使用流畅的配置API来定义对象 - 对象映射策略.AutoMapper使用基于约定的匹配算法来匹配源到目标值.AutoMapp ...

  8. 【转】JSch - Java实现的SFTP(文件上传详解篇)

    JSch是Java Secure Channel的缩写.JSch是一个SSH2的纯Java实现.它允许你连接到一个SSH服务器,并且可以使用端口转发,X11转发,文件传输等,当然你也可以集成它的功能到 ...

  9. MyBatis学习(四)、MyBatis配置文件

    四.MyBatis主配置文件 在定义sqlSessionFactory时需要指定MyBatis主配置文件: <bean id="sqlSessionFactory" clas ...

  10. JAVA NIO简介-- Buffer、Channel、Charset 、直接缓冲区、分散和聚集、文件锁

    IO  是主存和外部设备 ( 硬盘.终端和网络等 ) 拷贝数据的过程. IO 是操作系统的底层功能实现,底层通过 I/O 指令进行完成. Java标准io回顾 在Java1.4之前的I/O系统中,提供 ...