加载 AssetBundle 的四种方法
【加载 AssetBundle 的四种方法】
1、AssetBundle.LoadFromMemoryAsync(byte[] binary, uint crc = 0);
返回AssetBundleCreateRequest.Use assetBundle property to get an AssetBundle once it is loaded.
Compared to LoadFromMemory, this version will perform AssetBundle decompression on a background thread, and will not create the AssetBundle object immediately.
IEnumerator LoadFromMemoryAsync(string path)
{
AssetBundleCreateRequest createRequest = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
yield return createRequest;
AssetBundle bundle = createRequest.assetBundle;
var prefab = bundle.LoadAsset.<GameObject>("MyObject");
Instantiate(prefab);
}
2、AssetBundle.LoadFromFile(string path, uint crc = 0, ulong offset = 0);
返回 AssetBundle
This API is highly-efficient when loading uncompressed bundles from local storage. LoadFromFile will load the bundle directly from disk if the bundle is uncompressed or chunk (LZ4) compressed.
Loading a fully compressed (LZMA) bundle with this method will first decompress the bundle before loading it into memory.
Compared to LoadFromFileAsync, this version is synchronous and will not return until it is done creating the AssetBundle object.
public class LoadFromFileExample extends MonoBehaviour {
function Start() {
var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "myassetBundle"));
if (myLoadedAssetBundle == null) {
Debug.Log("Failed to load AssetBundle!");
return;
}
var prefab = myLoadedAssetBundle.LoadAsset.<GameObject>("MyObject");
Instantiate(prefab);
}
}
3、WWW.LoadFromCacheOrDownload
Loading an AssetBundle from a remote location will automatically cache the AssetBundle. If the AssetBundle is compressed, a worker thread will spin up to decompress the bundle and write it to the cache. Once a bundle has been decompressed and cached, it will load exactly like AssetBundle.LoadFromFile.
此方法会缓存,所以1:assetbundle尽量小。2:一次仅下载1个asset bundle。
If the cache folder does not have any space for caching additional files, LoadFromCacheOrDownload will iteratively delete the least-recently-used AssetBundles from the Cache until sufficient space is available to store the new AssetBundle. If making space is not possible (because the hard disk is full, or all files in the cache are currently in use), LoadFromCacheOrDownload() will bypass Caching and stream the file into memory
In order to force LoadFromCacheOrDownload the version parameter (the second parameter) will need to change. The AssetBundle will only be loaded from cache if the version passed to the function matches the version of the currently cached AssetBundle.
using UnityEngine;
using System.Collections; public class LoadFromCacheOrDownloadExample : MonoBehaviour
{
IEnumerator Start ()
{
while (!Caching.ready)
yield return null; var www = WWW.LoadFromCacheOrDownload("http://myserver.com/myassetBundle", );
yield return www;
if(!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
yield return;
}
var myLoadedAssetBundle = www.assetBundle; var asset = myLoadedAssetBundle.mainAsset;
}
}
4、public static Networking.UnityWebRequest GetAssetBundle(string uri, uint version, uint crc);
After returning the request, pass the request object intoDownloadHandlerAssetBundle.GetContent(UnityWebRequest).
IEnumerator InstantiateObject()
{
string uri = "file:///" + Application.dataPath + "/AssetBundles/" + assetBundleName; UnityEngine.Networking.UnityWebRequest request = UnityEngine.Networking.UnityWebRequest.GetAssetBundle(uri, );
yield return request.Send();
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
GameObject cube = bundle.LoadAsset<GameObject>("Cube");
GameObject sprite = bundle.LoadAsset<GameObject>("Sprite");
Instantiate(cube);
Instantiate(sprite);
}
The advantages of using UnityWebRequest is that it allows developers to handle the downloaded data in a more flexible manner and potentially eliminate unnecessary memory usage. This is the more current and preferred API over the UnityEngine.WWW class.
参考:
加载 AssetBundle 的四种方法的更多相关文章
- (一)JQuery动态加载js的三种方法
Jquery动态加载js的三种方法如下: 第一种: $.getscript("test.js"); 例如: <script type="text/javascrip ...
- JavaScript实现判断图片是否加载完成的3种方法整理
JavaScript实现判断图片是否加载完成的3种方法整理 有时候我们在前端开发工作中为了获取图片的信息,需要在图片加载完成后才可以正确的获取到图片的大小尺寸,并且执行相应的回调函数使图片产生某种显示 ...
- js 动态加载事件的几种方法总结
本篇文章主要是对js 动态加载事件的几种方法进行了详细的总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助 有些时候需要动态加载javascript事件的一些方法往往我们需要在 JS 中动态添 ...
- VUE 动态加载组件的四种方式
动态加载组件的四种方式: 1.使用import导入组件,可以获取到组件 var name = 'system'; var myComponent =() => import('../compon ...
- PHP自动加载SPL的四种处理方式
libs目录下有3个类文件: Test.class.php <?php class Test { public function __construct() { echo "Loadi ...
- 异步加载js的三种方法
js加载时间线 : 它是根据js出生的那一刻开始记录的一系列浏览器按照顺序做的事,形容的就是加载顺序,可以用来优化什么东西,理论基础,背下来. 1.创建Document对象,开始解析web页面.解析H ...
- Java加载资源文件几种方法
from: http://andyzhu.blog.51cto.com/4386758/775836/ import java.net.URL; import org.springframework. ...
- flask开启debug模式的两种方法、加载配置文件的两种方法、URL传参的四种方法
from flask import Flask app = Flask(__name__) # app.config.update(DEBUG=True)#开启debug模式 #加载配置文件方法一 # ...
- jQuery页面加载初始化的3种方法
jQuery 页面加载初始化的方法有3种 ,页面在加载的时候都会执行脚本,应该没什么区别,主要看习惯吧,本人觉得第二种方法最好,比较简洁. 第一种: $(document).ready(functio ...
随机推荐
- PHP中json_encode()问题
PHP 生成JSON的时候,必须将汉字不转义为 \u开头的UNICODE数据. 要想不转义,在后面加个参数即可 json_encode($data, JSON_UNESCAPED_UNICODE); ...
- 使用Hexo + Github Pages搭建个人独立博客
使用Hexo + Github Pages搭建个人独立博客 https://linghucong.js.org/2016/04/15/2016-04-15-hexo-github-pages-blog ...
- win10更改hosts文件
管理员身份运行notepad,打开hosts文件即可.
- windows python读取grib2数据
一.环境准备 (1).python3环境 (2).wgirb工具(用于读取grib1文件),下载地址: ftp://ftp.cpc.ncep.noaa.gov/wd51we/wgrib (3).wg ...
- 《算法》第六章部分程序 part 8
▶ 书中第六章部分程序,加上自己补充的代码,包括单纯形法求解线性规划问题 ● 单纯形法求解线性规划问题 // 表上作业法,I 为单位阵,y 为对偶变量,z 为目标函数值 // n m 1 // ┌── ...
- ADO.net之综合演练
using ConsoleApplication4.App_Code; using System; using System.Collections.Generic; using System.Lin ...
- springboot 整合 redis 共享Session-spring-session-data-redis
参考:https://www.cnblogs.com/ityouknow/p/5748830.html 如何使用 1.引入 spring-boot-starter-redis <dependen ...
- Mysql索引,有哪几种索引,什么时候该(不该)建索引;SQL怎么进行优化以及SQL关键字的执行顺序
索引(Index)是帮助MySQL高效获取数据的数据结构.提取句子主干,就可以得到索引的本质:索引是数据结构. 1.按照索引列值的唯一性,索引可分为唯一索引和非唯一索引 非唯一索引:B树索引 crea ...
- Tomcat的相关配置问题
Tomcat的目录结构bin --- 存放启动和关闭tomcat的脚本文件 conf --- 存放tomcat的各种配置文件 (主要有server.xml,context.xml,web.xml) ...
- day07-while,for循环
1.循环语句 Python提供了for循环和while循环while在给定的判断条件为 true 时执行循环体,否则退出循环体.for重复执行语句嵌套循环可以在while循环体中嵌套for.while ...