码迷,mamicode.com
首页 > 其他好文 > 详细

加载 AssetBundle 的四种方法

时间:2017-09-30 21:54:12      阅读:1880      评论:0      收藏:0      [点我收藏+]

标签:this   store   ssi   bre   mat   tsp   property   src   offset   

加载 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.

参考:

加载 AssetBundle 的四种方法

标签:this   store   ssi   bre   mat   tsp   property   src   offset   

原文地址:http://www.cnblogs.com/tekkaman/p/7616131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!