标签: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); }
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.
参考:
标签:this store ssi bre mat tsp property src offset
原文地址:http://www.cnblogs.com/tekkaman/p/7616131.html