Unity加载AssetBundle的方法
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的方法的更多相关文章
- 加载AssetBundle方法
先介绍一种常用的加载AssetBundle方法 using UnityEngine; using System.Collections; using System.IO; public class L ...
- 加载 AssetBundle 的四种方法
[加载 AssetBundle 的四种方法] 1.AssetBundle.LoadFromMemoryAsync(byte[] binary, uint crc = 0); 返回AssetBundle ...
- [转]全面理解Unity加载和内存管理
[转]全面理解Unity加载和内存管理 最近一直在和这些内容纠缠,把心得和大家共享一下: Unity里有两种动态加载机制:一是Resources.Load,一是通过AssetBundle,其实两者本质 ...
- 全面理解Unity加载和内存管理
全面理解Unity加载和内存管理http://game.ceeger.com/forum/read.php?tid=4394&fid=2&uid=6507 1.用简单的“for”循环 ...
- Unity加载二进制数据
[Unity加载二进制数据] The first step is to save your binary data file with the ".bytes" extension ...
- 优化加载jQuery的方法
请看下面的一段代码: <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js" ...
- jquery加载页面的方法
jquery加载页面的方法(页面加载完成就执行),建议大家看下windows.onload与$(document).ready之间的区别. 1.$(function(){ $("#a&q ...
- jquery加载页面的方法(页面加载完成就执行)
jquery加载页面的方法(页面加载完成就执行),建议大家看下windows.onload与$(document).ready之间的区别. 1.$(function(){ $("#a&qu ...
- (转载) jQuery 页面加载初始化的方法有3种
jQuery 页面加载初始化的方法有3种 ,页面在加载的时候都会执行脚本,应该没什么区别,主要看习惯吧,本人觉得第二种方法最好,比较简洁. 第一种: $(document).ready(functio ...
随机推荐
- SVG-Android开源库——SVG生成Vector资源文件的编辑预览工具
Vector矢量图在Android项目中的应用越来越广泛,但是如果你想用Android Studio自带的工具将SVG图片转化成Vector资源文件却是相当麻烦,首先能支持的SVG规范较少,其次操作流 ...
- git比较两个版本,获取所有代码有差别的文件,并拷贝到一个文件夹中
git diff 3b3855d a024af5 --name-only | xargs -i cp '{}' ./update/ --parents 解释:通过xargs 命令,把git diff ...
- im资源
https://github.com/oikomi/FishChatServer https://github.com/subrosa-io https://github.com/WhisperSys ...
- Spring MVC 和 Struts2 的区别?
1.请求拦截级别 struts2框架是类级别的拦截,每次来了请求就创建一个Action,然后调用setter getter方法把request中的数据注入 struts2实际上是通过setter ge ...
- [TJOI2016 & HEOI2016] 字符串
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4556 [算法] 不难发现 , 对于每个询问 ans = max{ mi ...
- 关于yolov3 训练输出值
Region xx: cfg文件中yolo-layer的索引: Avg IOU:当前迭代中,预测的box与标注的box的平均交并比,越大越好,期望数值为1: Class: 标注物体的分类准确率,越大越 ...
- starUML安装与破解
安装包百度云: 链接:https://pan.baidu.com/s/1oF_DH7Xh6yun6fFUDB2H3w 密码:1z7e 破解步骤:1. 首先打开你的starUML安装目录,并找到Lice ...
- c++中的友元重载
1 语法 返回值类型 operator 运算符名称(形参列表) { 重载实体 } --------->operator和运算符名称在一起构造成新的函数名 2 案例 #include <io ...
- Linux 静态库 & 动态库
转自:http://blog.chinaunix.net/uid-26833883-id-3219335.html 一.什么是库 本质上来说库是一种可执行代码的二进制形式,可以被操作系统载入内存执 ...
- CodeForces Gym 100685J Just Another Disney Problem (STL,排序)
题意:给定你大小未知的n个数,你允许有不超过一万次的询问,每次询问两个数,第i个数是否比第j个数小?然后后台会返回给你一个结果YES或者NO(即一行输入), 然后经过多次询问后,你需要给出一个正确的原 ...