zoukankan      html  css  js  c++  java
  • 加载 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);
    
        }
    View Code

     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;
        }
    }
    View Code

    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);
        }
    View Code

      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.

    参考:

  • 相关阅读:
    HDU 1548 A strange lift (Dijkstra)
    HDU 1217 Arbitrage (Floyd)
    HDU 1385 Minimum Transport Cost (Dijstra 最短路)
    考研总结 2016-12-31 20:10 219人阅读 评论(21) 收藏
    归并排序 2016-12-30 20:17 208人阅读 评论(21) 收藏
    docker安装 2016-11-06 19:14 299人阅读 评论(31) 收藏
    Docker初步了解 2016-10-30 20:46 279人阅读 评论(31) 收藏
    [自考]感想 2016-10-23 20:28 261人阅读 评论(32) 收藏
    [自考]C++中一些特殊用法 2016-10-16 22:12 318人阅读 评论(30) 收藏
    Fitnesse批量读取变量信息,并保存到用例执行上下文中
  • 原文地址:https://www.cnblogs.com/tekkaman/p/7616131.html
Copyright © 2011-2022 走看看