Skip to content

Instantly share code, notes, and snippets.

@stonstad
Created June 10, 2025 13:14
Show Gist options
  • Save stonstad/a0f7cccfc2261850b69406ece450a002 to your computer and use it in GitHub Desktop.
Save stonstad/a0f7cccfc2261850b69406ece450a002 to your computer and use it in GitHub Desktop.
Example Unity Scene Bootloader w/ Local Physics
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.SceneManagement;
namespace Example
{
public class Bootloader : MonoBehaviour
{
public SceneInstance AScene { get; private set; }
public SceneInstance BScene { get; private set; }
public SceneInstance CScene { get; private set; }
public SceneInstance DScene { get; private set; }
private void Awake()
{
if (AConstants.UseBootLoader)
StartCoroutine(LoadScenesInOrder());
}
private IEnumerator LoadScenesInOrder()
{
List<AsyncOperationHandle<SceneInstance>> handles = new List<AsyncOperationHandle<SceneInstance>>();
handles.Add(LoadSceneAsync("A"));
handles.Add(LoadSceneAsync("B"));
handles.Add(LoadSceneAsync("C"));
handles.Add(LoadSceneAsync("D"));
foreach (var handle in handles)
yield return new WaitUntil(() => handle.IsDone);
for (int i = 0; i < handles.Count; i++)
if (handles[i].Status != AsyncOperationStatus.Succeeded)
{
Debug.LogError($"Failed to load scene: {handles[i].DebugName}");
yield break;
}
foreach (var handle in handles)
yield return handle.Result.ActivateAsync();
AScene = handles[0].Result;
BScene = handles[1].Result;
CScene = handles[2].Result;
DScene = handles[3].Result;
//foreach (var handle in handles)
//Addressables.Release(handle);
}
private static AsyncOperationHandle<SceneInstance> LoadSceneAsync(string sceneName)
{
LoadSceneParameters loadSceneParameters = new LoadSceneParameters(LoadSceneMode.Additive, LocalPhysicsMode.Physics3D);
var handle = Addressables.LoadSceneAsync($"Assets/Scenes/{sceneName}.unity", loadSceneParameters, activateOnLoad: false);
return handle;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment