Search Unity

How to load multiple scenes with the Network SceneManager?

Discussion in 'Netcode for GameObjects' started by Mauschelbaer, Apr 24, 2023.

  1. Mauschelbaer

    Mauschelbaer

    Joined:
    Aug 4, 2019
    Posts:
    17
    I would like to load the "in-game" scene in my netcode-based multiplayer game in such a way that the level (and possibly other sub-scenes) can be loaded additively.
    That means
    1. I load the scene "InGame" first.
    2. This contains a GameObject which contains a script called "InGameController".
    3. In this script, I listen to events from the NetworkSceneManager and check if it's the InGame scene itself.
    4. If so, then I let the Level01 scene load additively.
    5. I also check whether this has been loaded and then trigger further logic (this becomes more understandable in the source code).
    However, for some reason, the event that the map is loaded is executed twice.

    I'm now wondering why this could be and whether this is a good way to load additv scenes. As I understand it, it is not possible to load several scenes at the same time with the NetworkSceneManager.

    Can anyone give me some advice here?

    Code (CSharp):
    1. public class InGameController : NetworkBehaviour
    2. {
    3.     private PlayersSpawnManager playersSpawnManager;
    4.  
    5.     private void Awake()
    6.     {
    7.         NetworkManager.SceneManager.OnLoadEventCompleted += OnLoadEventCompleted;
    8.         playersSpawnManager = GetComponent<PlayersSpawnManager>();
    9.     }
    10.  
    11.     public override void OnDestroy()
    12.     {
    13.         NetworkManager.SceneManager.OnLoadEventCompleted -= OnLoadEventCompleted;
    14.         base.OnDestroy();
    15.     }
    16.  
    17.     private void OnLoadEventCompleted(string scenename, LoadSceneMode loadscenemode, List<ulong> clientscompleted,
    18.         List<ulong> clientstimedout)
    19.     {
    20.         if (scenename == "InGame")
    21.         {
    22.             if (NetworkManager.IsServer)
    23.             {
    24.                 // This is called once
    25.                 NetworkManager.SceneManager.LoadScene("Level01", LoadSceneMode.Additive);
    26.             }
    27.         }
    28.         else if (scenename == "Level01")
    29.         {
    30.             if (NetworkManager.IsServer)
    31.             {
    32.                 // This is called TWICE!!!
    33.                 playersSpawnManager.SpawnPlayers();
    34.             }
    35.         }
    36.     }
    37.  
    38. }
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,937