02 June, 2022

How to Load Asset Bundle in unity3d

Original Source: http://gyanendushekhar.com/2016/06/25/load-asset-bundle-in-unity3d/

You can load asset bundle either locally or from an URL. First download the asset bundle using WWW class then you can load objects from the bundle.

You can download asset bundle using WWW class.

  • Non-caching: Use object of WWW class to download data and it will be not cached in local storage of the device.

  • Caching: WWW.LoadFromCacheOrDownload method allows you to load asset bundle either from cache memory or by downloading it. If cache will be clean then it will download asset bundle otherwise it will load asset bundle from cache memory.
    The asset bundles are cached to the unity’s cache folder in local storage device. Size of caching is different for different platforms (web player, android, mac); unity web player allows to cache 50 MB of data.

You can also load asset bundle from file using AssetBundle.LoadFromFile(“Your path”) method. Usually you can store asset in applications streaming asset folder and can load from there.

Load Game object from asset bundle

First create an object of AssetBundle class from the downloaded data and then you can load objects from that asset bundle. You can use below mentioned methods of AssetBundle class to load objects from your asset bundles.

  • LoadAsset: This method will load object by their name, pass the name of the object that you want to load.
  • LoadAssetAsync: Same as above method, but loads object asynchronously; so doesn’t blocks application’s main thread.
  • LoadAllAssets: This method will load all the objects in your assets bundle.

Once you load the game object from asset bundle, instantiate it to use in the scene.

Load scene from asset bundle

First load all the assets from associated scene asset bundle then load scene using SceneManager.LoadScene method.

Unload Asset bundle

Once you load the objects from asset bundle then unload asset bundle using AssetBundle.Unload method. This helps to conserve the memory and improves performance.

No comments:

Post a Comment