Deliver content to an application
To deliver content updates for your application, you create custom content archives and load them into the application from a content delivery service, or from the local device. The ContentDeliveryService APIs provide a unified workflow for loading both local and online content on demand. You can use it to load content, check the delivery status, and conditionally run code depending on the content available.
Set up content delivery
To enable content delivery for your project, set the ENABLE_CONTENT_DELIVERY scripting symbol. For information on how to do this, refer to Custom scripting symbols.
Load content
The ContentDeliveryService APIs load content by URL. You can pass the URL of content stored on an online content delivery service or the local URL of a content archive on the device. After you begin to download the content, you must wait until Unity finishes installing and caching the content. Then, you can load objects from the installed content in the same way you access local content, by weak reference ID. For more information, refer to Load a weakly-referenced object at runtime.
The following code sample shows how to load content by URL, wait for content delivery to complete, then continue with the application logic. It can use a MonoBehaviour rather than a system because the example code performs a full content update once before the application starts. You can also use a system's update method to do this, but the content delivery system APIs aren't Burst-compiled, and you can't use the APIs from jobs, so there is no performance benefit.
using Unity.Entities;
using Unity.Entities.Content;
using UnityEngine;
// Loads remote content catalogs, caches them locally, and makes weakly-referenced
// assets available for use by resolving their references to the downloaded content.
public partial struct LoadingRemoteCatalogSystem : ISystem
{
private bool initialized;
private string remoteUrlRoot;
private string cachePath;
private string contentNameSet;
private int maxLoadAttempts; // Max number of times to try loading the catalog
private int loadAttempts; // Current number of attempts
public void OnCreate(ref SystemState state)
{
initialized = false;
contentNameSet = "all";
remoteUrlRoot = "https://127.0.0.1/content/";
cachePath = Application.persistentDataPath + "/content-cache/";
maxLoadAttempts = 3;
loadAttempts = 0;
// Register a callback that runs when content delivery completes
ContentDeliveryGlobalState.RegisterForContentUpdateCompletion(UpdateStateCallback);
}
private void UpdateStateCallback(ContentDeliveryGlobalState.ContentUpdateState
contentUpdateState)
{
// Use this condition to implement logic for cases when content update failed and
// there is no data in the cache, or for an early exit.
if (contentUpdateState == ContentDeliveryGlobalState.ContentUpdateState
.NoContentAvailable)
{
// The system attempts again until it reaches the maximum number of attempts.
return;
}
// Track the state of the content and define when it's ready to use
if (contentUpdateState >= ContentDeliveryGlobalState.ContentUpdateState
.ContentReady)
{
// Only mark initialized when catalog loading actually succeeded.
initialized = true;
LoadMainScene();
}
}
public void OnUpdate(ref SystemState state)
{
// Attempt to load content a content catalog until it is initialized or the maximum
// number of attempts is reached.
if (!initialized && loadAttempts < maxLoadAttempts)
{
loadAttempts++;
// Calling the LoadContentCatalog method initializes the content catalog.
RuntimeContentSystem.LoadContentCatalog(remoteUrlRoot, cachePath,
contentNameSet, true);
}
}
private void LoadMainScene()
{
// Content is ready - load your main scene or enable gameplay systems here
}
}