SceneSpawner

You must create separate GameObjects for different room agent components.

SceneSpawner instantiates and destroys Networked GameObject in a Scene(GameObjects that have a Network ID components).

The SceneSpawner fetches spawn history in its Start() method and calls the OnReady event after that.

If a client was not connected to the game servers when a SceneSpawner's Start() method was called, the SceneSpawner will skip the fetching and it will not invoke the OnReady event.

Name

Function

Spawner Id

Used to identify a SceneSpawner. 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.

Spawn Points

A list of transforms which are used as the initial position and rotation of Networked GameObjects.

Player Prefabs

A list of prefabs for player controlled GameObjects.

Non-player prefabs

A list of prefabs for non-player controlled GameObjects.

Events

Event

Description

OnReady

Invoked when the SceneSpawner finished fetching its spawn history from the game servers and is ready to use.

OnHostReady

Invoked when the SceneSpawner finished fetching its spawn history from the game servers and is ready to use. Only invoked on the room's host client.

Properties

Name

Description

NumberOfPlayerPrefabs

Number of player prefabs

NumberOfNonPlayerPrefabs

Number of non-player prefabs

NumberOfSpawnPoints

Number of spawn points

SpawnerId (BETA)

Gets the SpawnerId assigned to the SceneSpawner

Methods

Name

Description

SpawnForPlayer(int, int)

Spawn a player controlled networked GameObject with the specified prefabIndex and spawnPointIndex.

SpawnForPlayer(int, Vector3)

Spawn a player controlled networked GameObject with the specified prefabIndex and position.

SpawnForNonPlayer(int, int)

Spawn a non-player controlled networked GameObject with the specified prefabIndex and spawnPointIndex.

SpawnForNonPlayer(int, Vector3)

Spawn a non-player controlled networked GameObject with the specified prefabIndex and position.

DestroyGameObject(GameObject)

Destroy the specified gameObject. If the gameObject has a NetworkID component, the remote duplicates of the gameObject will be destroyed as well.

PlayerFinishedSceneSetup()

Set FinishedSceneSetup to true for the local client.

HostFinishedSceneSetup()

Set FinishedSceneSetup to true for the room host client.

Example

This example demonstrates how to spawn a player controlled GameObject. As shown in the screenshot, the SceneSpawner has 1 PlayerPrefab and 4 SpawnPoints.

// OnSpawnerReady(bool finishedSceneSetup) method is added to handle the On Ready Event of the SceneSpawner.
public void OnSpawnerReady(bool finishedSceneSetup, SceneSpawner sceneSpawner)
{
Debug.Log("OnSpawnerReady " + finishedSceneSetup);
// Check finishedSceneSetup 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 (!finishedSceneSetup)
{
// If finishedSceneSetup 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.
// finishedSceneSetup will be true when SceneSpawn becomes ready next time.
sceneSpawner.PlayerFinishedSceneSetup();
}
}