Unity5 Assetbundle Series--loading and unloading of resources and use of assetbundlemanifest

Source: Internet
Author: User

The following code lists common operations for Assetbundle resources, with operations for bundles, asset, Gameobject, and three types of objects, which are guaranteed to be used in practice.

This piece of operation is more cumbersome, but as long as the use of the correct, can ensure that the resources are completely non-leaking.

usingUnityengine;usingSystem.Collections; Public classtestassetbundle:monobehaviour{ Public stringAssetbundlename ="Cube1.assetbundle"; Private stringDIR =""; PrivateAssetbundle bundle =NULL; PrivateUnityengine.object Asset =NULL; PrivateGameobject go =NULL; Private voidStart () {dir = Application.datapath +"/streamingassets/"; } Private voidOngui () {//Once these operations come down, the requested resources can be fully recovered.        if(Guilayout.button ("Loadassetbundle", Guilayout.width ( $), Guilayout.height ( -)) {loadbundle ();} if(Guilayout.button ("Loadasset", Guilayout.width ( $), Guilayout.height ( -)) {Loadasset ();} if(Guilayout.button ("Instantiate", Guilayout.width ( $), Guilayout.height ( -)) {instantiate ();} if(Guilayout.button ("Destroy", Guilayout.width ( $), Guilayout.height ( -)) {Destroy ();} if(Guilayout.button ("Unload", Guilayout.width ( $), Guilayout.height ( -)) {Unload ();} if(Guilayout.button ("Unloadforce", Guilayout.width ( $), Guilayout.height ( -)) {Unloadforce ();} if(Guilayout.button ("unloadunusedassets", Guilayout.width ( $), Guilayout.height ( -)) {unloadunusedassets ();} }    //Load Assetbundle    Private voidLoadbundle () {bundle=assetbundle.loadfromfile (System.IO.Path.Combine (dir, assetbundlename)); if(Bundle = =NULL) Debug.logerror ("Loadbundle Failed"); }    //load asset from Assetbundle    Private voidLoadasset () {if(Bundle = =NULL)return; Asset= bundle. Loadasset ("Cube1"); if(Asset = =NULL) Debug.logerror ("Loadasset Failed"); }    //instantiate Gameobject based on asset    Private voidInstantiate () {if(Asset = =NULL)return; Go= gameobject.instantiate (Asset) asGameobject; if(Go = =NULL) Debug.logerror ("Instantiate Failed"); }    //destroying Gameobject    Private voidDestroy () {if(Go = =NULL)return;        Gameobject.destroy (GO); Go=NULL; }    //weak offload, freeing the memory of Assetbundle itself    Private voidUnload () {if(Bundle = =NULL)return; //unload end, bundle can no longer use, remember to emptyBundle. Unload (false); Asset=NULL; Bundle=NULL; }    //Strong offload (ignoring the referenced offload), freeing the memory of Assetbundle itself while recovering asset extracted from Assetbundle    Private voidUnloadforce () {if(Bundle = =NULL)return; //unload end, bundle can no longer use, remember to emptyBundle. Unload (true); Asset=NULL; Bundle=NULL; }    //Global Weak offload, recycle unreferenced asset    Private voidunloadunusedassets () {resources.unloadunusedassets (); }}

Since bundles are dependent on each other before they are used, it is necessary to load all the bundle packages that they depend on before using a bundle package, a process that needs to be implemented with global manifest , with the following simple code:

usingUnityengine;usingSystem.Collections; Public classtestmanifest:monobehaviour{Private stringDIR =""; PrivateGameobject go =NULL; PrivateAssetbundlemanifest manifest =NULL; Private voidStart () {dir = Application.datapath +"/streamingassets/"; } Private voidOngui () {if(Guilayout.button ("loadassetbundlemanifest", Guilayout.width ( $), Guilayout.height ( -)) {loadassetbundlemanifest ();} if(Guilayout.button ("Loadbundle", Guilayout.width ( $), Guilayout.height ( -)) {loadbundle ();} }    //Load Manifest    Private voidloadassetbundlemanifest () {varBundle = Assetbundle.loadfromfile (System.IO.Path.Combine (dir,"streamingassets")); Manifest= bundle. Loadasset<assetbundlemanifest> ("assetbundlemanifest"); //Compress package releasedBundle. Unload (false); Bundle=NULL; }    //Load Assetbundle    Private voidLoadbundle () {stringBundlename ="Assets.resources.test.cube1.prefab.assetbundle"; //Note: The getalldependencies will return direct and indirect associated Assetbundle//load dependent packages do not have order requirements        string[] dependence =manifest.        Getalldependencies (Bundlename);  for(inti =0; I < dependence. Length; ++i) {assetbundle.loadfromfile (System.IO.Path.Combine (dir, dependence[i])); //Note: Manual loadasset is not required here//just load Assetbundle to//asset will load automatically when other associated asset are loaded        }        varbundle =assetbundle.loadfromfile (System.IO.Path.Combine (dir, bundlename)); varAsset = bundle. Loadasset ("Cube1"); Go= gameobject.instantiate (Asset) asGameobject; }}

Unity5 Assetbundle Series--loading and unloading of resources and use of assetbundlemanifest

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.