stringurl="http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d";IEnumeratorStart(){// Start a download of the given URLWWWwww=WWW.LoadFromCacheOrDownload(url,1);// Wait for download to completeyieldreturnwww;// Load and retrieve the AssetBundleAssetBundlebundle=www.assetBundle;// Load the TextAsset objectTextAssettxt=bundle.Load("myBinaryAsText",typeof(TextAsset))asTextAsset;// Retrieve the binary data as an array of bytesbyte[]bytes=txt.bytes;}
stringurl="http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d";IEnumeratorStart(){// Start a download of the encrypted assetbundleWWWwww=newWWW.LoadFromCacheOrDownload(url,1);// Wait for download to completeyieldreturnwww;// Load the TextAsset from the AssetBundleTextAssettextAsset=www.assetBundle.Load("EncryptedData",typeof(TextAsset));// Get the byte databyte[]encryptedData=textAsset.bytes;// Decrypt the AssetBundle databyte[]decryptedData=YourDecryptionMethod(encryptedData);// Use your byte array. The AssetBundle will be cached}
stringurl="http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d";IEnumeratorStart(){// Start a download of the encrypted assetbundleWWWwww=newWWW(url);// Wait for download to completeyieldreturnwww;// Get the byte databyte[]encryptedData=www.bytes;// Decrypt the AssetBundle databyte[]decryptedData=YourDecryptionMethod(encryptedData);// Create an AssetBundle from the bytes arrayAssetBundleCreateRequestacr=AssetBundle.CreateFromMemory(decryptedData);yieldreturnacr;AssetBundlebundle=acr.assetBundle;// You can now use your AssetBundle. The AssetBundle is not cached.}
stringurl="http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d";IEnumeratorStart(){// Start a download of the encrypted assetbundleWWWwww=newWWW.LoadFromCacheOrDownload(url,1);// Wait for download to completeyieldreturnwww;// Load the TextAsset from the AssetBundleTextAssettextAsset=www.assetBundle.Load("EncryptedData",typeof(TextAsset));// Get the byte databyte[]encryptedData=textAsset.bytes;// Decrypt the AssetBundle databyte[]decryptedData=YourDecryptionMethod(encryptedData);// Create an AssetBundle from the bytes arrayAssetBundleCreateRequestacr=AssetBundle.CreateFromMemory(decryptedData);yieldreturnacr;AssetBundlebundle=acr.assetBundle;// You can now use your AssetBundle. The wrapper AssetBundle is cached}
注意:从AssetBundle加载代码在 Windows Store Apps 和 Windows Phone 是不支持的。
//c# examplestringurl="http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d";IEnumeratorStart(){// Start a download of the given URLWWWwww=WWW.LoadFromCacheOrDownload(url,1);// Wait for download to completeyieldreturnwww;// Load and retrieve the AssetBundleAssetBundlebundle=www.assetBundle;// Load the TextAsset objectTextAssettxt=bundle.Load("myBinaryAsText",typeof(TextAsset))asTextAsset;// Load the assembly and get a type (class) from itvarassembly=System.Reflection.Assembly.Load(txt.bytes);vartype=assembly.GetType("MyClassDerivedFromMonoBehaviour");// Instantiate a GameObject and add a component with the loaded classGameObjectgo=newGameObject();go.AddComponent(type);}
usingSystem;usingUnityEngine;usingSystem.Collections;classNonCachingLoadExample:MonoBehaviour{publicstringBundleURL;publicstringAssetName;IEnumeratorStart(){// Download the file from the URL. It will not be saved in the Cacheusing(WWWwww=newWWW(BundleURL)){yieldreturnwww;if(www.error!=null)thrownewException("WWW download had an error:"+www.error);AssetBundlebundle=www.assetBundle;if(AssetName=="")Instantiate(bundle.mainAsset);elseInstantiate(bundle.LoadAsset(AssetName));// Unload the AssetBundles compressed contents to conserve memorybundle.Unload(false);}// memory is freed from the web stream (www.Dispose() gets called implicitly)}}
一个使用 WWW.LoadFromCacheOrDownload 的缓存的例子:
usingSystem;usingUnityEngine;usingSystem.Collections;publicclassCachingLoadExample:MonoBehaviour{publicstringBundleURL;publicstringAssetName;publicintversion;voidStart(){StartCoroutine(DownloadAndCache());}IEnumeratorDownloadAndCache(){// Wait for the Caching system to be readywhile(!Caching.ready)yieldreturnnull;// Load the AssetBundle file from Cache if it exists with the same version // or download and store it in the cacheusing(WWWwww=WWW.LoadFromCacheOrDownload(BundleURL,version)){yieldreturnwww;if(www.error!=null)thrownewException("WWW download had an error:"+www.error);AssetBundlebundle=www.assetBundle;if(AssetName=="")Instantiate(bundle.mainAsset);elseInstantiate(bundle.LoadAsset(AssetName));// Unload the AssetBundles compressed contents to conserve memorybundle.Unload(false);}// memory is freed from the web stream (www.Dispose() gets called implicitly)}}
// C# Example// Loading an Asset from disk instead of loading from an AssetBundle// when running in the EditorusingSystem.Collections;usingUnityEngine;classLoadAssetFromAssetBundle:MonoBehaviour{publicObjectObj;publicIEnumeratorDownloadAssetBundle<T>(stringasset,stringurl,intversion)whereT:Object{Obj=null;#if UNITY_EDITOR
Obj=Resources.LoadAssetAtPath("Assets/"+asset,typeof(T));yieldreturnnull;#else
// Wait for the Caching system to be readywhile(!Caching.ready)yieldreturnnull;// Start the downloadusing(WWWwww=WWW.LoadFromCacheOrDownload(url,version)){yieldreturnwww;if(www.error!=null)thrownewException("WWW download:"+www.error);AssetBundleassetBundle=www.assetBundle;Obj=assetBundle.LoadAsset(asset,typeof(T));// Unload the AssetBundles compressed contents to conserve memorybundle.Unload(false);}// memory is freed from the web stream (www.Dispose() gets called implicitly)#endif
}}
usingUnityEngine;// Note: This example does not check for errors. Please look at the example in the DownloadingAssetBundles section for more informationIEnumeratorStart(){// Start a download of the given URLWWWwww=WWW.LoadFromCacheOrDownload(url,1);// Wait for download to completeyieldreturnwww;// Load and retrieve the AssetBundleAssetBundlebundle=www.assetBundle;// Load the object asynchronouslyAssetBundleRequestrequest=bundle.LoadAssetAsync("myObject",typeof(GameObject));// Wait for completionyieldreturnrequest;// Get the reference to the loaded objectGameObjectobj=request.assetasGameObject;// Unload the AssetBundles compressed contents to conserve memorybundle.Unload(false);// Frees the memory from the web streamwww.Dispose();}
这会报错:Cannot load cached AssetBundle. A file of the same name is already loaded from another AssetBundle。
而且assetBundle会返回null。如果第一个资源还在加载中时,你是不能在第二个下载时获得AssetBundle的,因此你要做的要么就是在不需要时卸载AssetBundle,要么对它保持引用以避免当它已经存在于内存中时还下载它。
usingUnityEngine;usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;staticpublicclassAssetBundleManager{// A dictionary to hold the AssetBundle referencesstaticprivateDictionary<string,AssetBundleRef>dictAssetBundleRefs;staticAssetBundleManager(){dictAssetBundleRefs=newDictionary<string,AssetBundleRef>();}// Class with the AssetBundle reference, url and versionprivateclassAssetBundleRef{publicAssetBundleassetBundle=null;publicintversion;publicstringurl;publicAssetBundleRef(stringstrUrlIn,intintVersionIn){url=strUrlIn;version=intVersionIn;}};// Get an AssetBundlepublicstaticAssetBundlegetAssetBundle(stringurl,intversion){stringkeyName=url+version.ToString();AssetBundleRefabRef;if(dictAssetBundleRefs.TryGetValue(keyName,outabRef))returnabRef.assetBundle;elsereturnnull;}// Download an AssetBundlepublicstaticIEnumeratordownloadAssetBundle(stringurl,intversion){stringkeyName=url+version.ToString();if(dictAssetBundleRefs.ContainsKey(keyName))yieldreturnnull;else{using(WWWwww=WWW.LoadFromCacheOrDownload(url,version)){yieldreturnwww;if(www.error!=null)thrownewException("WWW download:"+www.error);AssetBundleRefabRef=newAssetBundleRef(url,version);abRef.assetBundle=www.assetBundle;dictAssetBundleRefs.Add(keyName,abRef);}}}// Unload an AssetBundlepublicstaticvoidUnload(stringurl,intversion,boolallObjects){stringkeyName=url+version.ToString();AssetBundleRefabRef;if(dictAssetBundleRefs.TryGetValue(keyName,outabRef)){abRef.assetBundle.Unload(allObjects);abRef.assetBundle=null;dictAssetBundleRefs.Remove(keyName);}}}