How Unity Loads the Assetbundle

Source: Internet
Author: User

Using system.collections;using system.collections.generic;using unityengine;using system.io;using Unityengine.networking;public class download:monobehaviour{IEnumerator Start () {//Resource pack path string Pat        H1 = "Assetbundles/cubewall.unity3d";        Shared dependent resource Bundle path string path2 = "Assetbundles/share.unity3d";  The first way to load AB, load loadfrommemory from memory #region//method One: Asynchronously load//load resource Assetbundlecreaterequest Request        = Assetbundle.loadfrommemoryasync (File.readallbytes (path1));        yield return request;        Load co-dependent resources such as decals, materials assetbundlecreaterequest request2 = Assetbundle.loadfrommemoryasync (File.readallbytes (path2));        Yield return request2;        Assetbundle ab = Request.assetbundle;        Assetbundle ab2 = Request2.assetbundle; Use the resources inside gameobject WALLPREFAB1 = (gameobject) ab.        Loadasset ("Cubewall");        Instantiate (WALLPREFAB1); Method Two: Synchronous loading (no co-use)//load resources assetbundle AB3 = ASSETBUNDLE.LOADFRommemory (File.readallbytes (path1));        Load co-dependent resources such as decals, materials assetbundle AB4 = Assetbundle.loadfrommemory (File.readallbytes (path2)); Use the resources inside gameobject wallPrefab2 = (gameobject) ab.        Loadasset ("Cubewall");        Instantiate (WALLPREFAB2); #endregion//second load AB, load LoadFromFile locally (no co-use) #region assetbundle ab5 = assetbundle.loadfromfile        (path1);        Assetbundle ab6 = Assetbundle.loadfromfile (path2); Gameobject wallPrefab3 = (gameobject) ab5.        Loadasset ("Cubewall");        Instantiate (WALLPREFAB3);        #endregion//The Third way to load AB, load WWW from local or server (will be deprecated) #region//is ready while (Caching.ready = = False)        {yield return null; }//Load from local//www www = www.        Loadfromcacheordownload (@ "File:/e:assetbundleproject\assetbundleproject\assetbundles", 1); Load www www = www from the server.        Loadfromcacheordownload (@ "Http://localhost/AssetBundles/cubewall.unity3d", 1); Yield RetuRN www; Whether to error if (string.            IsNullOrEmpty (www.error) = = False) {Debug.Log (www.error);        Yield break;        } assetbundle ab7 = Www.assetBundle; Use the resources inside gameobject WALLPREFAB4 = (gameobject) ab7.        Loadasset ("Cubewall");        Instantiate (WALLPREFAB4); #endregion//Fourth load AB mode download from server side unitywebrequest (new unity Use) #region//server path localhost for IP string        URI = @ "Http://localhost/AssetBundles/cubewall.unity3d";        Unitywebrequest request3 = Unitywebrequest.getassetbundle (URI); Yield return REQUEST3.        Send ();        Assetbundle Ab8 = ((downloadhandlerassetbundle) request3.downloadhandler). Assetbundle;        Assetbundle Ab8 = downloadhandlerassetbundle.getcontent (REQUEST3); Use the resources inside gameobject WALLPREFAB5 = (gameobject) ab8.        Loadasset ("Cubewall");        Instantiate (WALLPREFAB5); The resource bundle on which the Cubewall.unity3d resource bundle is loaded assetbundle Manifestab = Assetbundle.loadfromfile ("AssetBundles/assetbundles ");        Assetbundlemanifest manifest = (assetbundlemanifest) manifestab.loadasset ("Assetbundlemanifest"); foreach (string name in manifest.        Getallassetbundles ())//{//print (name); The//cubewall.unity3d resource bundle depends on the name of the resource bundle string[] STRs = manifest.        Getalldependencies ("Cubewall.unity3d");        foreach (string name in STRs) {assetbundle.loadfromfile ("assetbundles/" + name); } #endregion}}

  

How Unity Loads the Assetbundle

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.