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. html5--3.6 input元素(5)

    html5--3.6 input元素(5) 学习要点 input元素及其属性 input元素 用来设置表单中的内容项,比如输入内容的文本框,按钮等 不仅可以布置在表单中,也可以在表单之外的元素使用 i ...

  2. Understand JavaScript Callback Functions and Use Them

    In JavaScript, functions are first-class objects; that is, functions are of the type Object and they ...

  3. java学习之super关键字

    对于具有public或者protected属性的父类,其子类若想继承父亲的属性或者方法,那么需要用到super 实例:

  4. 【UVA12779占位】Largest Circle

    几何题,希望有时间回来解决掉.

  5. ubuntu 使用命令行清空回收站

    sudo rm -rf ~/.local/share/Trash/*

  6. 五、mysql中sql语句分类及常用操作

    1.sql语句分类: DQL语句 数据查询语言 select DML语句 数据操作语言 insert delete update DDL语句 数据定义语言 create drop alter TCL语 ...

  7. Python_两种导入模块的方法异同

    Python中有两种导入模块的方法 1:import module 2:from module import * 使用from module import *方法可以导入独立的项,也可以用from m ...

  8. RetHad6.7离线通过.rpm安装

    必须有RetHad6.7系统的.ios镜像文件,我们需要的.rpm都在镜像的Packages里面,针对不能联网的,并且也适用与CentOS系统 1. 查看版本号 参考我的博客 https://www. ...

  9. Eigen中的noalias(): 解决矩阵运算的混淆问题

    作者:@houkai本文为作者原创,转载请注明出处:http://www.cnblogs.com/houkai/p/6349990.html 目录 混淆例子解决混淆问题混淆和component级的操作 ...

  10. error: declaration of 'cv::Mat R ' shadows a parameter

    变量被覆盖. 例: void pose_estimation_2d2d::_pose_estimation_2d2d(const vector<KeyPoint> &v_keypo ...