In this blog post, We try to explain How to Get data from AssetBundle on The Server and Data From AssetBundle In Local.
What Is An AssetBundle?
An AssetBundle is a collection of assets from a project saved in a compact file with the purpose of being loaded separately to the application. AssetBundles can be loaded on demand by a game or application built in Unity. This allows streaming and asynchronous loading of content such as models, textures, audio clips, or even entire scenes. AssetBundles can be “pre-cached” and stored locally for immediate loading when first running an application. AssetBundles should be built for each target platform separately.
What Is The Use Of It?
AssetBundles can be used for downloadable content (DLC), reducing initial install size, loading assets optimized for the end-users platform, and reduce runtime memory pressure. The primary purpose of AssetBundles, however, is to stream content on demand from a remote location, to be loaded into the application as necessary. AssetBundles can contain any kind of asset type recognized by Unity, including custom binary data. The only exception is that script assets are not allowed.
In this tutorial, we will cover following topics for Android and iOS platform:
How to get data from asset bundle in server
How to get data from asset bundle in local
How To Get Data From AssetBundle On The Server?
Step-1: Build An AssetBundle
Now, right click on the SoundAsset and click on the Build AssetBundle. Choose the location to save the AssetBundle and save it. The asset bundle will be available on the location for Android and as well as iOS. BuildPipeline.BuildAssetBundle will build the selected AssetBundle for the platform specified in BuildTarget. Now upload the AssetBundle to the Server.
To make an AssetBundle, first, we need to build the asset of unity project.You can manually mark an asset as AssetBundle. Select any asset from project view and in the bottom, part from model view give AssetBundle name. You can also set version of AssetBundle.
In this tutorial, we will load an asset that contains sound files. The project contains a SoundAsset prefab inside Assets folder. We will use this to create an AssetBundle, Load it into the project and play the sound.
Create a script “ExportAssetBundles” inside Editor folder and add following lines of code in it:
Step-2: Load AssetBundle From The Server
We will load the SoundAsset from the server and play the Audio clip attached to it.
Create a new script name it “AssetBundleSample ”. It will manage all the loading operation of asset bundle. Add the following lines code in it:
Attach this script to an empty game object. create a Text to display loading status and assign to this script. Also, give the server URL of AssetBundle.
Now create 3 buttons and add LoadSoundFiles() method in the OnClick listener. Pass “Music1” as a parameter in button1, ”Music2” as a parameter in button2 and ”Music2” and “Music3 as a parameter in button3”. Now hit play. The loading will display as “%” format. when done with loading, it will first instantiate the AssetBundle and then will play the Audio Clip attached to it.
How To Get Data From AssetBundle In Local?
In this example, we will create a scene as Asset bundle and load it from local memory. Create a scene and name it “AssetScene”. Add some images and other game objects to this scene you want.
Step-1: Build A Scene As AssetBundle
To create a scene as asset bundle, update the following lines of code to the “ExportAssetBundles” script:
This script will allow us to create an asset bundle from the scene. Right click on AssetScene in project view and click Build Scene Bundle. It will create an asset Bundle inside Assets/AssetBundle folder.
Step-2: Load AssetBundle From The Local
To load any files from local memory in any platform, first, we need to save it somewhere in the local machine. Upload this AssetScene to the server.
Update the AssetBundleSample Script with following lines of code:
Create a button name it Loads scene and adds LoadFromLocal() method in the OnClick listener. LoadSceneBundle will load the Scene Asset from the server and store it in the local memory of the device. Call this coroutine in Start() method.
Now when you click on LoadScene button, you will be navigated to the SceneBundle. You do not need to add this scene to build setting.