Class NetworkSpawnManager
Class that handles object spawning
Inherited Members
Namespace: Unity.Netcode
Assembly: Unity.Netcode.Runtime.dll
Syntax
public class NetworkSpawnManager
Fields
OwnershipToObjectsTable
Use to get all NetworkObjects owned by a client Ownership to Objects Table Format: [ClientId][NetworkObjectId][NetworkObject] Server: Keeps track of all clients' ownership Client: Keeps track of only its ownership
Declaration
public readonly Dictionary<ulong, Dictionary<ulong, NetworkObject>> OwnershipToObjectsTable
Field Value
Type | Description |
---|---|
Dictionary<ulong, Dictionary<ulong, NetworkObject>> |
SpawnedObjects
The currently spawned objects
Declaration
public readonly Dictionary<ulong, NetworkObject> SpawnedObjects
Field Value
Type | Description |
---|---|
Dictionary<ulong, NetworkObject> |
SpawnedObjectsList
A list of the spawned objects
Declaration
public readonly HashSet<NetworkObject> SpawnedObjectsList
Field Value
Type | Description |
---|---|
HashSet<NetworkObject> |
Properties
NetworkManager
Gets the NetworkManager associated with this SpawnManager.
Declaration
public NetworkManager NetworkManager { get; }
Property Value
Type | Description |
---|---|
NetworkManager |
PlayerObjects
In distributed authority mode, a list of known spawned player NetworkObject instance is maintained by each client.
Declaration
public IReadOnlyList<NetworkObject> PlayerObjects { get; }
Property Value
Type | Description |
---|---|
IReadOnlyList<NetworkObject> |
Methods
GetClientOwnedObjects(ulong)
Returns an array of all NetworkObjects that belong to a client.
Declaration
public NetworkObject[] GetClientOwnedObjects(ulong clientId)
Parameters
Type | Name | Description |
---|---|---|
ulong | clientId | the client's id LocalClientId |
Returns
Type | Description |
---|---|
NetworkObject[] | returns an array of the NetworkObjects owned by the client |
GetConnectedPlayers()
Declaration
public List<ulong> GetConnectedPlayers()
Returns
Type | Description |
---|---|
List<ulong> |
GetLocalPlayerObject()
Returns the local player object or null if one does not exist
Declaration
public NetworkObject GetLocalPlayerObject()
Returns
Type | Description |
---|---|
NetworkObject | The local player object or null if one does not exist |
GetPlayerNetworkObject(ulong)
Returns the player object with a given clientId or null if one does not exist. This is only valid server side.
Declaration
public NetworkObject GetPlayerNetworkObject(ulong clientId)
Parameters
Type | Name | Description |
---|---|---|
ulong | clientId | the client identifier of the player |
Returns
Type | Description |
---|---|
NetworkObject | The player object with a given clientId or null if one does not exist |
GetPlayerNetworkObjects(ulong)
Returns all NetworkObject instances assigned to the client identifier
Declaration
public List<NetworkObject> GetPlayerNetworkObjects(ulong clientId)
Parameters
Type | Name | Description |
---|---|---|
ulong | clientId | the client identifier of the player |
Returns
Type | Description |
---|---|
List<NetworkObject> | A list of NetworkObject instances (if more than one are assigned) |
InstantiateAndSpawn(NetworkObject, ulong, bool, bool, bool, Vector3, Quaternion)
Use this method to easily instantiate and spawn an instance of a network prefab. InstantiateAndSpawn will:
- Find any override associated with the NetworkObject prefab
- If there is no override, then the current NetworkObject prefab type is used.
- Create an instance of the NetworkObject prefab (or its override).
- Spawn the NetworkObject prefab instance
Declaration
public NetworkObject InstantiateAndSpawn(NetworkObject networkPrefab, ulong ownerClientId = 0, bool destroyWithScene = false, bool isPlayerObject = false, bool forceOverride = false, Vector3 position = default, Quaternion rotation = default)
Parameters
Type | Name | Description |
---|---|---|
NetworkObject | networkPrefab | The NetworkObject of the pefab asset. |
ulong | ownerClientId | The owner of the NetworkObject instance (defaults to server). |
bool | destroyWithScene | Whether the NetworkObject instance will be destroyed when the scene it is located within is unloaded (default is false). |
bool | isPlayerObject | Whether the NetworkObject instance is a player object or not (default is false). |
bool | forceOverride | Whether you want to force spawning the override when running as a host or server or if you want it to spawn the override for host mode and the source prefab for server. If there is an override, clients always spawn that as opposed to the source prefab (defaults to false). |
Vector3 | position | The starting poisiton of the NetworkObject instance. |
Quaternion | rotation | The starting rotation of the NetworkObject instance. |
Returns
Type | Description |
---|---|
NetworkObject | The newly instantiated and spawned NetworkObject prefab instance. |
InternalOnOwnershipChanged(ulong, ulong)
Declaration
protected virtual void InternalOnOwnershipChanged(ulong perviousOwner, ulong newOwner)
Parameters
Type | Name | Description |
---|---|---|
ulong | perviousOwner | |
ulong | newOwner |