Problem raised: As the original scene manager required synchronizing all scenes from the server to the client, this is suitable for end-to-end game mode and is not common in C/S mode. Opening a room game requires dynamically creating new scenes, which was not supported by the original scene manager and needs to be implemented by oneself.
Change 1: Because it is to realize online games in the game hall on the Internet, the scene manager is replaced with a simple scene manager. In this simple scene manager, the client has only one scene, while the server has many scenes. The client selects the scene, and the server synchronizes the scene accordingly.
Change 2: It is agreed that all scenes have unique names, and an empty scene named Active is loaded on the server and set as Active Scene. That is to say, all dynamically created network objects are created by default in this empty scene. After creation, use SceneManager again MoveGameObject To Scene moves it to the specified scene. In the online environment, it is impossible for everyone to specify a specific activity scenario, as this can lead to conflicts. Once agreed upon, there will be no conflicts.
Change 3: Added a method to create, load, and merge scenes. Everyone knows about the game of opening a room. There is a room creator who names newly opened room scenes using a naming convention similar to "Room_player username". And this newly created scene is loaded as a fixed scene Room and merged into a new name "Room_Player Username". Similarly, the main game scene goes through the same process, loading a fixed Game scene and merging it into the newly created "Games_Player Username" scene. Then, the scene manager implements scene synchronization between the server and client.
The instance reference for the custom scene manager is NetworkManager Singleton.SceneManager, His method is as follows:
//Server loading scenario
//SceneName is the name of the scene
void SvrLoadScene(string sceneName);
//Server uninstallation scenario
//SceneName Scene Name
void SvrUnloadScene(string sceneName);
//The server creates a new scene and loads another scene, then merges the loaded scene with offset into the newly created scene.
//ID specifies the client ID
//DesScene Target Scene Name
//SrcScene Source Scene Name
//Offset "Scene offset
void SvrCreateAndMergeScene(ulong id,string desScene,string srcScene,Vector3 offset);
//Client switches to the current scene
//CurScene specifies the scene name
void ClientSwitchScene(string curScene);
//Scenario of switching a client on the server
//ID specifies the client ID
//CurScene specifies the scene name
void ServerSwitchScene(ulong id,string curScene);
//Set the default scenario, and when the client first connects to the server, the server will synchronize the default scenario with the client.
//DefaultScene Default Scene Name
void SetDefaultScene(string defaultScene);
//The server-side scene loading completion event is triggered when the server-side scene is loaded.
//Use NetworkManager.Singleton.SceneManager.OnServerSceneLoadComplete += OnServerSceneLoadComplete;
//Add an event trigger function.
OnServerSceneLoadComplete(string scname)