using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.Networking; public class DownLoad : MonoBehaviour
{
IEnumerator Start()
{
//资源包路径
string path1 = "AssetBundles/cubewall.unity3d";
//共享依赖资源包路径
string path2 = "AssetBundles/share.unity3d"; //第一种加载AB的方式 ,从内存中加载 LoadFromMemory #region //方法一:异步加载
//加载资源
AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path1));
yield return request;
//加载共同依赖资源,如贴图、材质
AssetBundleCreateRequest request2 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path2));
yield return request2;
AssetBundle ab = request.assetBundle;
AssetBundle ab2 = request2.assetBundle; //使用里面的资源
GameObject wallPrefab1 = (GameObject) ab.LoadAsset("CubeWall");
Instantiate(wallPrefab1); //方法二:同步加载(无需用协程)
//加载资源
AssetBundle ab3 = AssetBundle.LoadFromMemory(File.ReadAllBytes(path1));
//加载共同依赖资源,如贴图、材质
AssetBundle ab4 = AssetBundle.LoadFromMemory(File.ReadAllBytes(path2)); //使用里面的资源
GameObject wallPrefab2 = (GameObject) ab.LoadAsset("CubeWall");
Instantiate(wallPrefab2); #endregion //第二种加载AB的方式 ,从本地加载 LoadFromFile(无需用协程) #region AssetBundle ab5 = AssetBundle.LoadFromFile(path1);
AssetBundle ab6 = AssetBundle.LoadFromFile(path2); GameObject wallPrefab3 = (GameObject) ab5.LoadAsset("CubeWall");
Instantiate(wallPrefab3); #endregion //第三种加载AB的方式 ,从本地或服务器加载 WWW(将被弃用) #region //是否准备好
while (Caching.ready == false)
{
yield return null;
}
//从本地加载
//WWW www = WWW.LoadFromCacheOrDownload(@"file:/E:AssetBundleProject\AssetBundleProject\AssetBundles", 1);
//从服务器加载
WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundles/cubewall.unity3d", 1);
yield return www;
//是否报错
if (string.IsNullOrEmpty(www.error) == false)
{
Debug.Log(www.error);
yield break;
}
AssetBundle ab7 = www.assetBundle; //使用里面的资源
GameObject wallPrefab4 = (GameObject) ab7.LoadAsset("CubeWall");
Instantiate(wallPrefab4); #endregion //第四种加载AB方式 从服务器端下载 UnityWebRequest(新版Unity使用) #region //服务器路径 localhost为IP
string uri = @"http://localhost/AssetBundles/cubewall.unity3d";
UnityWebRequest request3 = UnityWebRequest.GetAssetBundle(uri);
yield return request3.Send(); //AssetBundle ab8 = ((DownloadHandlerAssetBundle)request3.downloadHandler).assetBundle;
AssetBundle ab8 = DownloadHandlerAssetBundle.GetContent(request3); //使用里面的资源
GameObject wallPrefab5 = (GameObject) ab8.LoadAsset("CubeWall");
Instantiate(wallPrefab5); //加载cubewall.unity3d资源包所依赖的资源包
AssetBundle manifestAB = AssetBundle.LoadFromFile("AssetBundles/AssetBundles");
AssetBundleManifest manifest = (AssetBundleManifest) manifestAB.LoadAsset("AssetBundleManifest"); //foreach(string name in manifest.GetAllAssetBundles())
//{
// print(name);
//} //cubewall.unity3d资源包所依赖的资源包的名字
string[] strs = manifest.GetAllDependencies("cubewall.unity3d");
foreach (string name in strs)
{
AssetBundle.LoadFromFile("AssetBundles/" + name);
} #endregion
}
}

  

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

  1. 加载AssetBundle方法

    先介绍一种常用的加载AssetBundle方法 using UnityEngine; using System.Collections; using System.IO; public class L ...

  2. 加载 AssetBundle 的四种方法

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

  3. [转]全面理解Unity加载和内存管理

    [转]全面理解Unity加载和内存管理 最近一直在和这些内容纠缠,把心得和大家共享一下: Unity里有两种动态加载机制:一是Resources.Load,一是通过AssetBundle,其实两者本质 ...

  4. 全面理解Unity加载和内存管理

     全面理解Unity加载和内存管理http://game.ceeger.com/forum/read.php?tid=4394&fid=2&uid=6507 1.用简单的“for”循环 ...

  5. Unity加载二进制数据

    [Unity加载二进制数据] The first step is to save your binary data file with the ".bytes" extension ...

  6. 优化加载jQuery的方法

    请看下面的一段代码: <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js" ...

  7. jquery加载页面的方法

    jquery加载页面的方法(页面加载完成就执行),建议大家看下windows.onload与$(document).ready之间的区别.   1.$(function(){ $("#a&q ...

  8. jquery加载页面的方法(页面加载完成就执行)

    jquery加载页面的方法(页面加载完成就执行),建议大家看下windows.onload与$(document).ready之间的区别. 1.$(function(){  $("#a&qu ...

  9. (转载) jQuery 页面加载初始化的方法有3种

    jQuery 页面加载初始化的方法有3种 ,页面在加载的时候都会执行脚本,应该没什么区别,主要看习惯吧,本人觉得第二种方法最好,比较简洁. 第一种: $(document).ready(functio ...

随机推荐

  1. linux命令学习笔记 : install 命令

    install .作用 install命令的作用是安装或升级软件或备份数据,它的使用权限是所有用户. .格式 ()install [选项]... 来源 目的地 ()install [选项]... 来源 ...

  2. Material Design 之 定义状态栏(Status Bar)的颜色

    Hey,好久不见.今天遇到一个问题,想要把Status Bar 和 Tool Bar的颜色弄成一样的,或者是类似的,例如Material Design: 图中Status Bar颜色比Tool Bar ...

  3. codevs 3095 黑心的市长

    3095 黑心的市长  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 钻石 Diamond   题目描述 Description A市有一条长Nkm的高速公路.有M个人各自想承包 ...

  4. Intellij IDEA 弹窗License activation 报 this license BIG3CLIK6F has been cancelled 错误的解决。

    this license BIG3CLIK6F has been cancelled 具体如下: 对,没错,这个激活码本来可以使用到2018年的,但是,忽然间就不能用了.经查询吧. 还得修改个系统配置 ...

  5. POJ1742(多重部分和问题:模板题)

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 32776   Accepted: 11131 Descripti ...

  6. vuex本地存储

    vuex与localstorage 区别:vuex数据存储的内存,localstorage的数据存储在本地 应用场景:vuex用于组件之间的传值,localstorage用于不同页面之间的传值 永久性 ...

  7. hibernate学习二 基本用法

    一  映射文件User.hbm.xml 定义了持久化类实例是如何存储和加载的,这个文件定义了持久化类和表的映射. 根据映射文件,Hibernate可以生成足够的信息以产生所有的SQL语句,也就是类的实 ...

  8. 计时器 vb

    十分钟 我们寒假就是这么长!! vb 执行cmd   :              shell "cmd /c DOS命令" vb 取系统日期: Print Date; " ...

  9. Flexpaper二次开发入门教程》(十) Flexpaper简单使用-第一个Flexpaper例子

    4. Flexpaper简单使用 通过上面三章的内容,大家对Flexpaper.SWFTools应该有大概的了解了,SWF文件也已经生成了,我们开始进入Flexpaper的使用的介绍. 本章中只演示F ...

  10. PhpStorm之操作数据库

    对数据库进行基本的操作 还不清楚如何使用PhpStorm连接本地数据库的朋友看一下我的上一篇博客配置数据库连接 点击已经连接好的数据库,找到下图中的 Consoles,然后点击 console(def ...