Tag:Unity AssetBundle
Article From:https://www.cnblogs.com/luguoshuai/p/9124211.html
using UnityEngine;
using System.Collections;
using System.IO;

public class LoadResource : MonoBehaviour
    //assetbundle The suffix can actually be customized at randomString PathAssetBundle = "D:/LgsTest/ResourcePath/Cube.assetbundle";Void StaRT (){//StartCoroutine (LoadFromMemoryAsync ());//LoadFromMemorySync ();//LoadFromLocalFile ();StartCoroutine (LoadFromWWW ());}#region the first way to load AB, loading from memory./ / asynchronously create AssetBundle objects through binary data from Bundle. After completion, a larger WebStream will be created in memory./ / when invoking, the decompression of Bundle is asynchronous, so for uncompressed BunDle file, which is equivalent to CreateFromMemoryImmediate.IEnumerator LoadFromMemoryAsync (){AssetBundLeCreateRequest request = AssetBundle.CreateFromMemory (File.ReadAllBytes (PathAssetBundle));Yield return request;AssetBundle bundle = request.assetBundle;Instantiate (bundle.LOad ("Cube"));}Void LoadFromMemorySync (){AssetBundle bundle = AssetBundle.CreateFromMemoryImmediate (File.ReadAllBytes (PathAssetBundle));Instantiate (bundle.Load ("CubE "))}#endregion#region second ways to load AB, loading locally./ / loaded resources can only be uncompressed (that is to say, when you pack resources, you need to add BuildAsse.TBundleOptions.UncompressedAssetBundle) compressed resources will fail to load./ / through the uncompressed Bundle file, create the AssetBundle object synchronously. This is the fastest way to create.. After creation, only a smaller SerializedFile is created in memory, and subsequent AssetBundle.Load needs to be obtained from disk by IO.Void LoadFromLocalFile (){AssetBundle bundle = AssetBundle.CreateFromFile (PathAssetBundle);Instantiate (bundlE.Load ("Cube"));}#endregion#region third ways to load AB through WWW loadingIEnumerator LoadFromWWW (){While (! Caching.ready)Yield return null;WWW www = GetWWWInstanCE (false, PathAssetBundle);Yield return WWW;If (! String.IsNullOrEmpty (www.error)){Debug.Log (www.error);Yield break;}AssetBundleBundle = www.assetBundle;Instantiate (bundle.Load ("Cube"));}There are two ways to obtain //WWW objects/ / mode 1: load the Bundle file and get the WWW object, and then create a larger WebStream in memory./ / mode two: load the Bundle file and get the WWW object, and store the Bundle content of the decompression form to disk.For the cache, if the Bundle is already in the cache, this step is omitted./ / after completion, only a smaller SerializedFile will be created in memory, and subsequent AssetBundle.Load needs to be diskette from IO.Cache acquisition.WWW GetWWWInstance (bool isNew, string URL){WWW www = null;If (isNeW)Www = new WWW (URL);ElseWww = WWW.LoadFromCacheOrDownload (URL, 0));Return WWW;}#endregion}


Link of this Article: Unity 4.x resource loading

Leave a Reply

Your email address will not be published. Required fields are marked *