Setting up the Scene Spawner
5 - 10 minutes read
Last updated
5 - 10 minutes read
Last updated
At this point, you have the Player RealTime Agent set up. Select the Player GameObject in the Scene Hierarchy and drag it into the Prefabs folder in the Project Assets folder.
Add a new Empty GameObject and name it "SceneSpawner". Click the Add Component button and Search "Spawner", select Scene Spawner to attach it to the Player GameObject.
You will see that a Network System ID component was attached to SceneSpawner GameObject automatically. Network System ID component help SWNetwork to identify SWNetwork's internal GameObjects.
SceneSpawner instantiates and destroys Networked GameObject in a Scene(GameObjects that have a Network ID components). If you have multiple scenes in your game, you need to add a SceneSpawner for each of them. Make sure you give different Spawner Id to different SceneSpawners because SceneSpawner stores important GameObject Spawn History of a scene. GameObject Spawn History is used to restore GameObjects when players disconnected and reconnected to a game.
Our game only has one game scene, we can set the Spawner Id to 1.
Create an Empty GameObject and name it "SpawnPoints", reset its position to (0, 0, 0).
Create an Empty GameObject and name it "SpawnPoint", reset it position to (0, 0, 0).
Select an Icon for the SpawnPoint so we can easily find it in the Scene.
Select the SpawnPoint GameObject in the Hierarchy and drag it into the Prefabs Folder to create a SpawnPoint Prefab.
Drag the SpawnPoint prefab into the SpawnPoints GameObject and create 4 SpawnPoints.
Move the SpawnPoints to the places you like to spawn the players. Set the Position Y to 1.5 to make sure the players are spawned above the ground.
Select the SceneSpawner GameObject, open the Spawn Point section and set the Size to 4. Drag the SpawnPoint into the slots.
Open the Player Prefabs section of the SceneSpawner, drag the Player Prefab into it.
Open the GameSceneManager.cs script, and add a method to handle the On Ready Event of SceneSpawner.
Your GameSceneManager.cs script should look like:
In the SceneSpawner component, add a listener to the On Ready event. Select OnSpawnerReady(bool, SceneSpawner) in the GameSceneManager.cs script.
Make sure to select the OnSpawnerReady method from the Dynamic bool SceneSpawner section in the list.