This document is about: FUSION 2-SHARED
SWITCH TO

Scene Loading

Overview

Fusion internally contains no Unity Scene handling implementations, but it does provide an INetworkSceneManager interface, where you can define how Fusion responds to various Scene related events - such as scene changes. An implementation of INetworkSceneManager hereon will be referred to as a Scene Manager.

An implementation of INetworkSceneManager needs to be assigned in the NetworkRunner.StartGame() method via the StartGameArgs.SceneManager field. If no implementation is provided (null), Fusion will create an instance of the NetworkSceneManagerDummy class and log an error communicating that it will not be able to link scene objects.

An default implementation named NetworkSceneManagerDefault comes with Fusion which can:

  • Load and Unload scenes.
  • Supports regular scene loading and scene loading via addressables.
  • Can reload currently active scenes.
  • Loads up to 8 scenes simultaneously using additive scene loading.
  • Supports multi-peer scene loading with correct PhysicsScene associations for each Runner.

Load and Unload scenes

Important: Both LoadScene() and UnloadScene() are only allowed to be called on the Master Client. This is enforced by the default scene manager implementation.

To load a scene, simply call NetworkRunner.LoadScene() passing the SceneRef of the scene and the LoadSceneParameters. Keep in mind that loading with single load mode will unload all previous scenes, while with additive scene loading previous scenes remain loaded.

C#

// Loading 3 scenes in additive mode.
if (Runner.IsSceneAuthority) {
  Runner.LoadScene(SceneRef.FromIndex(1), LoadSceneMode.Additive);
  Runner.LoadScene(SceneRef.FromIndex(2), LoadSceneMode.Additive);
  Runner.LoadScene(SceneRef.FromIndex(3), LoadSceneMode.Additive);
}

The index used to create the SceneRef is the index of the scene in the Unity build. To get the index of the current scene you can use SceneManager.GetActiveScene().buildIndex. For other scenes the index can be found by scene path using SceneUtility.GetBuildIndexByScenePath. For instance to load the Scene that is stored at Assets/Scenes/GameScene.unity the following code is used:

C#

  Runner.LoadScene(SceneRef.FromIndex(SceneUtility.GetBuildIndexByScenePath("Assets/Scenes/GameScene.unity")), LoadSceneMode.Additive);

To unload a scene, simply call NetworkRunner.UnloadScene() passing the SceneRef of the scene to be unloaded.

C#

// Unloading scene 1.
if (Runner.IsSceneAuthority) {
  Runner.UnloadScene(SceneRef.FromIndex(1));
}

It is also possible to reload scenes by unloading and loading them again in sequence.
The NetworkSceneManagerDefault implementation maintain a Version field that works as a counter, increasing every time a scene is loaded or unloaded,
this value is used to register the load ID of the scene object to differentiate between the last loaded scene and the new loaded one, even though they're
the same scene.

C#

// Reloading scene 2.
if (Runner.IsSceneAuthority) {
  Runner.UnloadScene(SceneRef.FromIndex(2));
  Runner.LoadScene(SceneRef.FromIndex(2), LoadSceneMode.Additive);
}

SceneRefs can be created from a scene index with SceneRef.FromIndex(int index) or from a path, mostly used to create references to addressables scenes, with SceneRef.FromPath(string path).

Back to top