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. codevs 2102 石子归并2

    传送门 2102 石子归并 2  时间限制: 10 s  空间限制: 256000 KB  题目等级 : 黄金 Gold   题目描述 Description 在一个园形操场的四周摆放N堆石子,现要将 ...

  2. 安装和配置Rose HA

    安装和配置Rose HA 1.1.1 安装Rose HA 将roseha的三个安装文件拷贝到/data/install, 便于两台主机的安装. 在没有安装Rose HA 之前,分别修改hosts文件将 ...

  3. android 怎么实现跑马灯效果

    自定义控件 FocusedTextView, 使android系统误以为它拥有焦点 public class FocusedTextView extends TextView { public Foc ...

  4. final/finalize/finally的区别

    一.性质不同 (1)final为关键字: (2)finalize()为方法:---垃圾回收机制中的方法(GC) (3)finally为为区块标志,用于try语句中: 二.作用 (1)final为用于标 ...

  5. python 去停用词

    Try caching the stopwords object, as shown below. Constructing this each time you call the function ...

  6. js搜索相同类型的控件全选、取值(Checkbox)

    function selectAll(obj) { if (obj.checked) { $("input[type='checkbox']").each(function () ...

  7. 3.11-3.14 Hive 企业使用优化2

    一.查看HQL执行计划explain 1.explain hive在执行的时候会把所对应的SQL语句都会转换成mapreduce代码执行,但是具体的MR执行信息我们怎样才能看出来呢? 这里就用到了ex ...

  8. c++中的友元重载

    1 语法 返回值类型 operator 运算符名称(形参列表) { 重载实体 } --------->operator和运算符名称在一起构造成新的函数名 2 案例 #include <io ...

  9. Flutter实战视频-移动电商-53.购物车_商品列表UI框架布局

    53.购物车_商品列表UI框架布局 cart_page.dart 清空原来写的持久化的代码; 添加对应的引用,stless生成一个静态的类.建议始终静态的类,防止重复渲染 纠正个错误,上图的CartP ...

  10. 51nod - 1420 - 数袋鼠好有趣 - 贪心 - 二分

    https://www.51nod.com/Challenge/Problem.html#!#problemId=1420 一开始乱搞了一发,每个袋鼠二分找最小的能放它的,然后二分的范围从下一个开始保 ...