Complex Example
A real world example from a game that requires all the bundles to be present for the user to play. But allows the player to play offline if he got all the bundles from the last known version.
Can we reach the backend ?
Before knowing what we should do, we first have to know if we can reach our backend. ConnectToMaster
is the entry point for the connection sequence and will handle any exception thrown by the different connection sequences.
async Task ConnectToMaster() {
try {
// Get a connection to our backend
var socket = await MyClient.GetSocket();
// Check if we are connected
if (socket.IsConnected) {
// We are, start our connected scenario
await ConnectedSequence();
} else {
// We are not, start our not connected scenario
await NotConnectedSequence();
}
} catch (Exception e) {
// An error occured, we assume it is linked to the network
// More error handling can be added later if it is needed
Debug.LogException(e);
ShowNetworkErrorPopup();
}
}
The user has an internet connection
async Task ConnectedSequence() {
// CheckVersion retrieves the version from backend, stores it on the device and returns true if the major version is different
var needsHardUpdate = await MoonMSClient.CheckVersion();
// A hard means that the user needs to use the store from wich they downloaded the game to update it
// because scripts have changed.
if (needsHardUpdate) {
ShowHardUpdatePopup();
return;
}
// We give our remote url, the most recent version number and force the download of the manifest to make sure that we get the latest informations for when we check for the need of a patch
await AssetBundles.Init(MoonMSClient.AssetBundlesSourceURL, App.Version.UniqueNumber, true);
// Check if any of the bundles needs to be patched (that's when the forceDownloadManifest comes handy !)
if (AssetBundles.CheckIfBundlesOutdated()) {
// Patch all the bundles that needs to be updated and give an UI delegate to show the advancement of the patch to the user
await AssetBundles.Patch(null, null, PatchGUI.SetAdvancement);
}
// Everything went fine and the game is up to date, load the Authentication prefab
// AuthPrefab is a BundledAsset that has the bundle and name information for the auth popup prefab.
Instantiate(await AssetBundles.LoadAsset<GameObject>(AuthPrefab));
}
The user does not have an internet connection
Here, we check if we can initialize from the last known version which is a number that we got from our backend and that we stored in a file or PlayerPrefs. If we can, we check if we have all the bundles for that version in the cache, if we do we allow the user to play offline, if we don't we ask for a patch.
/// <summary>
/// Checks the integrity of the current cache and allows the player to play offline if he is able to.
/// </summary>
async Task NotConnectedSequence() {
// Init from cache using the last known version
// App.Version.UniqueNumber is the last version number we got from our backend
await AssetBundles.Init("", App.Version.UniqueNumber);
// Check if any bundle is outdated
if (AssetBundles.CheckIfBundlesOutdated()) {
ShowPatchPopup();
} else {
// We are good to go, ask the user if he wants to play offline
ShowPlayOfflinePopup();
}
}