[Bootstrap] First go at Addressables bootsrapped objects

This commit is contained in:
Michal Pikulski
2025-09-07 12:36:35 +02:00
parent d3c6b838b4
commit d20004238d
65 changed files with 1748 additions and 3 deletions

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 72153f2c39ad40deacd00c5caed75019
timeCreated: 1757237591

View File

@@ -0,0 +1,181 @@
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace Bootstrap
{
/// <summary>
/// Entrypoint for the Custom Boot initialisation
/// </summary>
public static class CustomBoot
{
/// <summary>
/// Current initialisation status
/// </summary>
public static bool Initialised { get; private set; }
/// <summary>
// Called as soon as the game begins
/// </summary>
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
private static void Initialise()
{
//We should always clean up after Addressables, so let's take care of that immediately
Application.quitting += ApplicationOnUnloading;
PerformInitialisation();
}
/// <summary>
/// Initialise the bootstrapper
/// </summary>
public static void PerformInitialisation()
{
//In editor, perform initialisation synchronously
if (Application.isEditor)
{
InitialiseBootSettingsSync();
}
else
{
//In builds, just run things asynchronously, since we can add any checks we need early on
_ = InitialiseBootSettings();
}
}
/// <summary>
/// Called as the game is quitting, allowing for cleanup
/// </summary>
private static void ApplicationOnUnloading()
{
Application.quitting -= ApplicationOnUnloading;
PerformDeInitialisation();
}
/// <summary>
/// De-Initialise the bootstrapper
/// </summary>
public static void PerformDeInitialisation()
{
Cleanup(runtimeBootSettingsHandle);
Cleanup(editorBootSettingsHandle);
Initialised = false;
}
/// <summary>
/// Initialise the boot settings asynchronously
/// </summary>
private static async Task InitialiseBootSettings()
{
await LoadCustomBootSettings();
Initialised = true;
}
/// <summary>
/// Initialise the boot settings synchronously
/// </summary>
private static void InitialiseBootSettingsSync()
{
LoadCustomBootSettingsSync();
Initialised = true;
}
/// <summary>
/// Clean up the boot settings
/// </summary>
/// <param name="handle"></param>
private static void Cleanup(AsyncOperationHandle<CustomBootSettings> handle)
{
if (handle.IsValid())
{
handle.Result.Cleanup();
Addressables.Release(handle);
}
}
/// <summary>
/// Async handle for the runtime custom boot settings scriptable object
/// </summary>
private static AsyncOperationHandle<CustomBootSettings> runtimeBootSettingsHandle;
/// <summary>
/// Async handle for the editor custom boot settings object
/// </summary>
private static AsyncOperationHandle<CustomBootSettings> editorBootSettingsHandle;
/// <summary>
/// Runtime addressable key
/// </summary>
private static string RuntimeAsset = $"{nameof(CustomBootSettings)}_Runtime";
/// <summary>
/// Editor addressable key
/// </summary>
private static string EditorAsset = $"{nameof(CustomBootSettings)}_Editor";
/// <summary>
/// Load the custom boot settings asynchronously and run the initialisation method
/// </summary>
private static async Task LoadCustomBootSettings()
{
if (Application.isEditor)
{
editorBootSettingsHandle = await InitialiseBootSettingsAsset(EditorAsset);
}
runtimeBootSettingsHandle = await InitialiseBootSettingsAsset(RuntimeAsset);
}
/// <summary>
/// Load the custom boot settings synchronously and run the initialisation method
/// </summary>
private static void LoadCustomBootSettingsSync()
{
if (Application.isEditor)
{
editorBootSettingsHandle = InitialiseBootSettingsAssetSync(EditorAsset);
}
runtimeBootSettingsHandle = InitialiseBootSettingsAssetSync(RuntimeAsset);
}
/// <summary>
/// Initialise the boot settings asset with the given key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
private static async Task<AsyncOperationHandle<CustomBootSettings>> InitialiseBootSettingsAsset(string key)
{
var handle = Addressables.LoadAssetAsync<CustomBootSettings>(key);
await handle.Task;
switch (handle.Status)
{
case AsyncOperationStatus.Failed:
Debug.LogError(handle.OperationException);
break;
case AsyncOperationStatus.Succeeded:
await handle.Result.Initialise();
break;
}
return handle;
}
/// <summary>
/// Initialise the boot settings asset with the given key synchronously
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
private static AsyncOperationHandle<CustomBootSettings> InitialiseBootSettingsAssetSync(string key)
{
var handle = Addressables.LoadAssetAsync<CustomBootSettings>(key);
var result = handle.WaitForCompletion();
result.InitialiseSync();
return handle;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8b562285bf55436dbf0dd87e3abcee2a
timeCreated: 1724007283

View File

@@ -0,0 +1,100 @@
using System.Threading.Tasks;
using UnityEngine;
namespace Bootstrap
{
/// <summary>
/// Settings for the custom boot process
/// </summary>
public class CustomBootSettings : ScriptableObject
{
/// <summary>
/// A list of prefabs which should be loaded during boot
/// </summary>
public GameObject[] BootPrefabs;
/// <summary>
/// Internal references to instances of the prefabs from <see cref="BootPrefabs"/>
/// </summary>
private GameObject[] Instances;
/// <summary>
/// Runtime container object which acts as the parent for any BootPrefab instances
/// </summary>
private GameObject RuntimeContainer;
/// <summary>
/// Initialise the boot settings object asynchronously, loading each prefab in <see cref="BootPrefabs"/>
/// </summary>
public async Task Initialise()
{
RuntimeContainer = new GameObject($"{name}_Container");
DontDestroyOnLoad(RuntimeContainer);
Instances = new GameObject[BootPrefabs.Length];
for (var i = 0; i < BootPrefabs.Length; i++)
{
if (!BootPrefabs[i]) continue;
var instance = GameObject.InstantiateAsync(BootPrefabs[i], RuntimeContainer.transform);
while (!instance.isDone)
await Task.Yield();
Instances[i] = instance.Result[0];
}
}
/// <summary>
/// Initialise the boot settings object synchronously, loading each prefab in <see cref="BootPrefabs"/>
/// </summary>
public void InitialiseSync()
{
RuntimeContainer = new GameObject($"{name}_Container");
if (Application.isPlaying)
{
DontDestroyOnLoad(RuntimeContainer);
}
Instances = new GameObject[BootPrefabs.Length];
for (var i = 0; i < BootPrefabs.Length; i++)
{
if (!BootPrefabs[i]) continue;
var instance = GameObject.Instantiate(BootPrefabs[i], RuntimeContainer.transform);
Instances[i] = instance;
}
}
/// <summary>
/// Destroy all loaded instances referenced by <see cref="Instances"/>
/// </summary>
public void Cleanup()
{
foreach (var t in Instances)
{
if (t)
{
if (Application.isPlaying)
{
GameObject.Destroy(t);
}
else
{
GameObject.DestroyImmediate(t);
}
}
}
Instances = null;
if (Application.isPlaying)
{
GameObject.Destroy(RuntimeContainer);
}
else
{
GameObject.DestroyImmediate(RuntimeContainer);
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6b4618adec17468d8064d6088ef8e38b
timeCreated: 1724007691