加载 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", 5);
        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, 0);
        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.

参考:

时间: 2024-10-06 12:43:23

加载 AssetBundle 的四种方法的相关文章

JavaScript实现判断图片是否加载完成的3种方法整理

JavaScript实现判断图片是否加载完成的3种方法整理 有时候我们在前端开发工作中为了获取图片的信息,需要在图片加载完成后才可以正确的获取到图片的大小尺寸,并且执行相应的回调函数使图片产生某种显示效果.本文主要整理了几种常见的javascipt判断图片加载完成时的方法,并通过代码与实际应用相结合进行解释与说明. onload方法 通过向img标签添加onload属性,并填入相应的函数来执行后续的javascipt代码.如下代码例子中img元素默认是不显示的,通过onload判断加载完成后再将

js 动态加载事件的几种方法总结

本篇文章主要是对js 动态加载事件的几种方法进行了详细的总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助 有些时候需要动态加载javascript事件的一些方法往往我们需要在 JS 中动态添加事件,这就涉及到浏览器兼容性问题了,以下谈及的几种方法,我们也常常混合使用. 方法一.setAttributevar obj = document.getElementById("obj");obj.setAttribute("onclick", "javasc

PHP自动加载SPL的四种处理方式

libs目录下有3个类文件: Test.class.php <?php class Test { public function __construct() { echo "Loading Test.class.php <br>"; } } Test.php <?php class Test { public function __construct() { echo "Loading Test.php <br>"; } } Us

Java加载资源文件几种方法

from: http://andyzhu.blog.51cto.com/4386758/775836/ import java.net.URL; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestMain { public static void main

jQuery页面加载初始化的几种方法

在新的公司里工作,最近一直在做关于js和jQuery的开发,下面三种是最常见的jquery页面加载初始化的方法 第一种,去年实习时的公司用的是这样 $(function(){ //定义局部变量 //var a; ... init(); }); function init(){ alert(123); } 第二种,和上面的写法类似 jQuery(function($){ //定义局部变量 //var a; ... init(); }); function init(){ alert(123); }

UIImage加载图片的两种方法区别

Apple官方的文档为生成一个UIImage对象提供了两种方法加载图片: 1. imageNamed,其参数为图片的名字: 2. imageWithContentsOfFile,其参数也是图片文件的路径. 那么两种有什么区别吗? 肯定是有的.根据Apple的官方文档: imageNamed: 这 个方法用一个指定的名字在系统缓存中查找并返回一个图片对象如果它存在的话.如果缓存中没有找到相应的图片,这个方法从指定的文档中加载然后缓存并返回这 个对象.因此imageNamed的优点是当加载时会缓存图

jquery easyui tab加载内容的几种方法

转:http://my.oschina.net/u/2331760/blog/391937?fromerr=saqeoxxB jQuery Easyui 的tabs插件有两种方式加载某个tab(标签页)上的内容:“href远程请求”和“content本地内容”,本文就两种方式的优缺点进行简单分析和思考. 两者特点: href方式加载数据的特点: content方式加载数据的特点: 简单总结: href常见问题: 1.href只加载目标URL的html片段 2.短暂的页面混乱: 3.html片段的

Spring加载配置文件的几种方法(org.springframework.beans.factory.BeanDefinitionStoreException)

一:Spring中的几种容器都支持使用xml装配bean,包括:XmlBeanFactory ,ClassPathXmlApplicationContext ,FileSystemXmlApplicationContext ,XmlWebApplicationContext 加载这些容器的配置文件的xml有一下几种常见的方法: 1:引用资源用XmlBeanFactory(不能实现多个文件相互引用) Resource resource = new ClassPathResource("appcon

JS 动态加载脚本的4种方法

有时候我们需要动态的加入适合的js,因为有时候不需要将所有的js都加载进来,以来提高效率,但这种方法比较适合单个js文件比较大的情况 如果js文件都比较小,还是一个js好,这样可以减少连接数.下面是4种比较常用的方法,大家可以根据情况选择,最后脚本之家 将会给推荐一个. 1.直接document.write  <script language="javascript"> document.write("<script src='test.js'><