Today in the group asked friends there is no synchronous load Assetbundle method, colleagues give me pointing unity3d don't know in which version added a Assetbundle.createfromfile interface, this interface is synchronous read Assetbundle. The help document is then queried for relevant information.
Http://docs.unity3d.com/ScriptReference/AssetBundle.CreateFromFile.html
article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn
Assetbundle.createfromfilepublic static Assetbundle CreateFromFile (string path); Parameterspathpath of the file on Disksee Also:WWW.assetBundle, WWW. Loadfromcacheordownload.descriptionloads an asset bundle from a disk. Only uncompressed asset bundles is supported by the This function. This is the fastest-to-load an asset bundle.
article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn
Compared with the previous use of the WWW mode, the use of CreateFromFile read Assetbundle mode has the following differences:
1, www is asynchronous, CreateFromFile is synchronous.
2. The CreateFromFile interface must read the Assetbundle file without compression.
To compare two ways of packaging and reading
Article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn
Packaged Assetbundle compression and non-compression
If you use CreateFromFile this API to read the Assetbundle, then packaging assetbundle should be careful to package the non-compressed, that is, to add this setting.
Buildassetbundleoptions.uncompressedassetbundle
The code is as follows:
Using unityengine;using system.collections;using unityeditor;using System.io;public class Newbehaviourscript:editor { [MenuItem ("Cp/build Girl Nolzma")]static void Buildgirlnolzma () {string targetdir = Application.datapath + "/ Bundlenolzma "; uint CRC = 0;//public resources into the stack; Buildpipeline.pushassetdependencies (); string filePath = "Assets/res/01.png"; Object obj = Assetdatabase.loadmainassetatpath (FilePath);//build;if (buildpipeline.buildassetbundle (obj, NULL, targetdir+ "/01.) Png.unity3d ", Out Crc,buildassetbundleoptions.uncompressedassetbundle | buildassetbundleoptions.collectdependencies | Buildassetbundleoptions.deterministicassetbundle, Buildtarget.standalonewindows)) {AssetDatabase.Refresh ();} FilePath = "Assets/res/aoki Shizuku.mp3"; obj = Assetdatabase.loadmainassetatpath (FilePath);//build;if ( Buildpipeline.buildassetbundle (obj, null, targetdir+ "/aoki Shizuku.mp3.unity3d", out CRC, Buildassetbundleoptions.uncompressedassetbundle | buildassetbundleoptions.collectdependencies | BuildassetbundleoptIons. Deterministicassetbundle, Buildtarget.standalonewindows)) {Assetdatabase.refresh ();} Ordinary resources into the stack; Buildpipeline.pushassetdependencies (); FilePath = "Assets/resources/girl.prefab"; obj = Assetdatabase.loadmainassetatpath (FilePath);//build;if (buildpipeline.buildassetbundle (obj, NULL, targetdir+ "/ Girl.prefab.unity3d ", Out Crc,buildassetbundleoptions.uncompressedassetbundle | buildassetbundleoptions.collectdependencies | Buildassetbundleoptions.deterministicassetbundle, Buildtarget.standalonewindows)) {AssetDatabase.Refresh ();} Buildpipeline.popassetdependencies (); Buildpipeline.popassetdependencies ();} [MenuItem ("Cp/build Girl")]static void Buildgirl () {string targetdir = Application.datapath + "/bundle"; uint CRC = 0;//Public Capital source into the stack; Buildpipeline.pushassetdependencies (); string filePath = "Assets/res/01.png"; Object obj = Assetdatabase.loadmainassetatpath (FilePath);//build;if (buildpipeline.buildassetbundle (obj, NULL, targetdir+ "/01.) Png.unity3d ", out CRC, Buildassetbundleoptions.collectdependencies | Buildassetbundleoptions.deterministicassetbundle, Buildtarget.standalonewindows)) {AssetDatabase.Refresh ();} FilePath = "Assets/res/aoki Shizuku.mp3"; obj = Assetdatabase.loadmainassetatpath (FilePath);//build;if ( Buildpipeline.buildassetbundle (obj, null, targetdir+ "/aoki Shizuku.mp3.unity3d", out CRC, buildassetbundleoptions.collectdependencies | Buildassetbundleoptions.deterministicassetbundle, Buildtarget.standalonewindows)) {AssetDatabase.Refresh ();} Ordinary resources into the stack; Buildpipeline.pushassetdependencies (); FilePath = "Assets/resources/girl.prefab"; obj = Assetdatabase.loadmainassetatpath (FilePath);//build;if (buildpipeline.buildassetbundle (obj, NULL, targetdir+ "/ Girl.prefab.unity3d ", out CRC, Buildassetbundleoptions.collectdependencies | Buildassetbundleoptions.deterministicassetbundle, Buildtarget.standalonewindows)) {AssetDatabase.Refresh ();} Buildpipeline.popassetdependencies (); Buildpipeline.popassetdependencies ();}}
With an uncompressed setting, the packaging assetbundle speed is significantly faster.
Compare the Assetbundle files packaged in two ways. article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn
Article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn
WWW and CreateFromFile mode read Assetbundle
CreateFromFile is a synchronous operation, and WWW is an asynchronous operation.
Using unityengine;using System.collections;public class Newbehaviourscript:monobehaviour {//Use this for Initializatio Nvoid Start () {}//Update is called once per framevoid update () {//debug.log ("Update");} void Ongui () {if (Guilayout.button ("Load Girl")) {Startcoroutine (Loaddepends ("file://" +application.datapath+ "/ Bundle/01.png.unity3d ")); Startcoroutine (Loaddepends ("file://" +application.datapath+ "/bundle/aoki Shizuku.mp3.unity3d")); Startcoroutine (Load ("file://" +application.datapath+ "/bundle/girl.prefab.unity3d")); if (Guilayout.button ("Load Girl Nolzma")) {Assetbundle Assetbundle01=assetbundle.createfromfile ( application.datapath+ "/bundlenolzma/01.png.unity3d"); Assetbundle assetbundlemp3=assetbundle.createfromfile (application.datapath+ "/bundlenolzma/aoki Shizuku.mp3.unity3d "); Assetbundle assetbundle=assetbundle.createfromfile (application.datapath+ "/bundlenolzma/girl.prefab.unity3d"); Nstantiate (Assetbundle.mainasset);}} Private IEnumerator Loaddepends (string path) {www www = new www (path);D Ebug. Log ("Loaddepends 1"); yield return www;debug. Log ("Loaddepends 2"), Object obj= Instantiate (www.assetBundle.mainAsset);D ebug. Log ("Loaddepends 3"); yield return new waitforseconds (3.0f);} Private IEnumerator Load (string path) {yield return new waitforseconds (3f); www www = new www (path);D ebug. Log ("Load 1"); yield return www;debug. Log ("Load 2"), Object obj= Instantiate (www.assetBundle.mainAsset);D ebug. Log ("Load 3"); Www.assetBundle.Unload (false); Resources.unloadunusedassets ();}}
Article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn
Attention:
Using uncompressed packaging Assetbundle, we recommend that you use the 7ZIP LZMA compression algorithm for overall compression, at the beginning of the game to extract.
Sample Project Download:
Http://pan.baidu.com/s/1o63fOz4
article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn
Unity3d Synchronous Load Assetbundle method Assetbundle.createfromfile