Setting up the Scene Spawner

5 - 10 minutes read

Player Prefab

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.

SceneSpawner

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.

Choose a unique Spawner Id

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.

Creating Spawn Points

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.

Adding the Player Prefab to the Scene Spawner

Open the Player Prefabs section of the SceneSpawner, drag the Player Prefab into it.

Handling SceneSpawner OnReady Event

Open the GameSceneManager.cs script, and add a method to handle the On Ready Event of SceneSpawner.

Your GameSceneManager.cs script should look like:

using UnityEngine.SceneManagement;
using UnityEngine;
using SWNetwork;

/// <summary>
/// Game scene manager manages game scenes state.
/// </summary>
public class GameSceneManager : MonoBehaviour
{
    public GameObject winnerPanel;
    public GameObject gameOverPanel;

    public void Exit()
    {
        // TODO
        // exit the game
    }

    // OnSpawnerReady(bool alreadySetup) method is added to handle the On Ready Event.
    public void OnSpawnerReady(bool alreadySetup, SceneSpawner sceneSpawner)
    {
        Debug.Log("OnSpawnerReady " + alreadySetup);

        // Check alreadySetup to see if the scene has been set up before. 
        // If it is true, it means the player disconnected and reconnected to the game. 
        // In this case, we should not spawn a new Player GameObject for the player.
        if (!alreadySetup)
        {
            // If alreadySetup is false, it means the player just started the game. 
            // We randomly select a SpawnPoint and ask the SceneSpawner to spawn a Player GameObject. 
            // we have 1 playerPrefabs so playerPrefabIndex is 0.
            // We have 4 spawnPoints so we generated a random int between 0 to 3.
            int spawnPointIndex = Random.Range(0, 3);
            sceneSpawner.SpawnForPlayer(0, spawnPointIndex);

            // Tell the spawner that we have finished setting up the scene. 
            // alreadySetup will be true when SceneSpawn becomes ready next time.
            sceneSpawner.PlayerFinishedSceneSetup();
        }
    }
}

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.

Last updated