Host addressable assets on the server

Addressable asset system

In the previous part, we discussed how we can create an addressable asset and load it locally using the direct asset reference. But nowadays most games are loading their data from their server at runtime when needed. An addressable asset system is the best way to make your build size low and attract more users.

Unity addressable asset system provides as a way to build and update content without releasing new build on the store. Isn’t it amazing?. Consider a scenario that you have an online shooting multiplayer game and you have to add new characters in your game without giving an update to the store or you have to add new maps, vehicles, themes, etc. This all you can do easily using a unity addressable system. All you need is to have a server to which you deploy the content built by the addressable system.

Before Starting makes sure you set up your unity project as shown in part 1. Firstly setup an addressable asset system to work with the server and then we can build and deploy our assets into the server.

How to set up the addressable asset system to use with the server?

  • Open the Addressable groups using Windows->Asset Management->Addressable->Groups
  • You find the profile in the group window. Goto Profile->Manage or Windows->Asset Management->Addressable->Profiles
  • Now create a new profile by Create->Profile
  • After new profile added, rename it to the server by right click on new profile -> Rename Profile
  • Now you see some profile variables like BuildTarget, LoadBuildPath, etc.
  • Don’t Change the variable BuildTarget.
  • Change LoadBuildPath variable to “ServerData/[BuildTarget]”.

Change LocalLoadPath variable to “http://localhost/[BuildTarget]”. To start checking your assets using the localhost. If you don’t know how to test on the localhost then download and install xampp.

After installing xampp go to xampp->htdocs and copy your folder. Then Start the xampp control panel and start the apache server. If you are targeting Android then your path to the localhost is “http://localhost/Android”. All your assets reside in the Android folder.

  • Change RemotebuildPath variable to “ServerData/[BuildTarget]”. If you wish to build your asset somewhere else then change the path appropriately.
  • Change RemoteLoadPath variable to “http:///[BuildTarget]”. i.e “http://Unity3dtuts.com/Android” or you can change the “Android” to your own in the server. But make sure it clearly reflected in the server profile in the unity editor.
  • Close the profile window.

Build asset data for deployment on a server

Now select the server profile as default to get staring woking on it by simply selecting the server profile from the Addressable Groups window.

If your game has remote and local both assets then it will be more beneficial to add new Group using the Create->Group->Packed Assets and rename this group to Remote Asset to anything you like.

Here we want all assets to be in the server so I make the default group as a remote group. To do this click on the Default Local Group (Default). You can see various things on the inspector window.

Now make some changes to work with the server asset. Choose Update Restriction to “Can Change Post Release” as we want our asset to be changeable after releasing our game. You can even make static by selecting “Can not Change Post Release”.

Now, the most important part is Content packing & loading. Change BuildPath and LoadPath to “RemoteBuildPath” and “RemoteLoadPath”. We don’t change anything in Advanced options for now. You can change as per your requirement.

Remote Server settings
Remote Server settings

Now Goto Play mode script and select the Use Existing Build(requires build groups) to build and release your asset to the server.

After completing all these things, Build the assets in our default group to make ready for publication to the server. Go to Build-> New Build->Default Build Script. This will create data at your RemoteBuildPath. You can find it under your Project.

File Location
File Location

Copy the Android folder under the ServerData and Publish in your server.

How to modify the current data and Deploy it again?

  • Modify the all the asset prefab you want
  • After Goto Build->Update a previous build
  • Select the content under the Assets->AddressableAssetsData->Android->addressables_content_state
  • Now to see the newly generated data in the ServerData folder.
  • Again copy the Android folder under the ServerData and Publish in your server.
  • Repeat this step whenever you want some modification in your assets.

How to Load Assets from the server?

The question remains is to load our data from the server at runtime.

To do this follow the sample script below.

using System.Collections;
using System.Collections.Generic;
using UnityEngine.AddressableAssets;
using UnityEngine;

public class AddressablesExample: MonoBehaviour 
{
   GameObject myGameObject;

   void Start()
   {
      Addressables.LoadAssetAsync<GameObject>("Cube").Completed += OnLoadDone;
   }

    private void OnLoadDone(UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle<GameObject> obj)
    {
        // In a production environment, you should add exception handling to catch scenarios such as a null result.
        myGameObject = obj.Result;
    }
}

After completion of the async loading of Cube, it will be store in the myGameObject. You can then perform any operation on myGameObject. When you try to load this object second time, it will be directly loaded from the cache memory.

Conclusion

Server-side hosting give us a more flexible environment to handle our assets at runtime effectively. An addressable asset system provides a better way to deal with this server-side stuff in a way that it optimizes our build and manage memory effectively.

Leave a Reply