docs.unity3d.com
    Show / Hide Table of Contents

    Class NetworkManager

    The main component of the library

    Inheritance
    Object
    Object
    Component
    Behaviour
    MonoBehaviour
    NetworkManager
    Inherited Members
    MonoBehaviour.IsInvoking()
    MonoBehaviour.CancelInvoke()
    MonoBehaviour.Invoke(String, Single)
    MonoBehaviour.InvokeRepeating(String, Single, Single)
    MonoBehaviour.CancelInvoke(String)
    MonoBehaviour.IsInvoking(String)
    MonoBehaviour.StartCoroutine(String)
    MonoBehaviour.StartCoroutine(String, Object)
    MonoBehaviour.StartCoroutine(IEnumerator)
    MonoBehaviour.StartCoroutine_Auto(IEnumerator)
    MonoBehaviour.StopCoroutine(IEnumerator)
    MonoBehaviour.StopCoroutine(Coroutine)
    MonoBehaviour.StopCoroutine(String)
    MonoBehaviour.StopAllCoroutines()
    MonoBehaviour.print(Object)
    MonoBehaviour.useGUILayout
    MonoBehaviour.runInEditMode
    Behaviour.enabled
    Behaviour.isActiveAndEnabled
    Component.GetComponent(Type)
    Component.GetComponent<T>()
    Component.TryGetComponent(Type, Component)
    Component.TryGetComponent<T>(T)
    Component.GetComponent(String)
    Component.GetComponentInChildren(Type, Boolean)
    Component.GetComponentInChildren(Type)
    Component.GetComponentInChildren<T>(Boolean)
    Component.GetComponentInChildren<T>()
    Component.GetComponentsInChildren(Type, Boolean)
    Component.GetComponentsInChildren(Type)
    Component.GetComponentsInChildren<T>(Boolean)
    Component.GetComponentsInChildren<T>(Boolean, List<T>)
    Component.GetComponentsInChildren<T>()
    Component.GetComponentsInChildren<T>(List<T>)
    Component.GetComponentInParent(Type)
    Component.GetComponentInParent<T>()
    Component.GetComponentsInParent(Type, Boolean)
    Component.GetComponentsInParent(Type)
    Component.GetComponentsInParent<T>(Boolean)
    Component.GetComponentsInParent<T>(Boolean, List<T>)
    Component.GetComponentsInParent<T>()
    Component.GetComponents(Type)
    Component.GetComponents(Type, List<Component>)
    Component.GetComponents<T>(List<T>)
    Component.GetComponents<T>()
    Component.CompareTag(String)
    Component.SendMessageUpwards(String, Object, SendMessageOptions)
    Component.SendMessageUpwards(String, Object)
    Component.SendMessageUpwards(String)
    Component.SendMessageUpwards(String, SendMessageOptions)
    Component.SendMessage(String, Object)
    Component.SendMessage(String)
    Component.SendMessage(String, Object, SendMessageOptions)
    Component.SendMessage(String, SendMessageOptions)
    Component.BroadcastMessage(String, Object, SendMessageOptions)
    Component.BroadcastMessage(String, Object)
    Component.BroadcastMessage(String)
    Component.BroadcastMessage(String, SendMessageOptions)
    Component.transform
    Component.gameObject
    Component.tag
    Object.GetInstanceID()
    Object.GetHashCode()
    Object.Equals(Object)
    Object.Instantiate(Object, Vector3, Quaternion)
    Object.Instantiate(Object, Vector3, Quaternion, Transform)
    Object.Instantiate(Object)
    Object.Instantiate(Object, Transform)
    Object.Instantiate(Object, Transform, Boolean)
    Object.Instantiate<T>(T)
    Object.Instantiate<T>(T, Vector3, Quaternion)
    Object.Instantiate<T>(T, Vector3, Quaternion, Transform)
    Object.Instantiate<T>(T, Transform)
    Object.Instantiate<T>(T, Transform, Boolean)
    Object.Destroy(Object, Single)
    Object.Destroy(Object)
    Object.DestroyImmediate(Object, Boolean)
    Object.DestroyImmediate(Object)
    Object.FindObjectsOfType(Type)
    Object.DontDestroyOnLoad(Object)
    Object.DestroyObject(Object, Single)
    Object.DestroyObject(Object)
    Object.FindSceneObjectsOfType(Type)
    Object.FindObjectsOfTypeIncludingAssets(Type)
    Object.FindObjectsOfType<T>()
    Object.FindObjectOfType<T>()
    Object.FindObjectsOfTypeAll(Type)
    Object.FindObjectOfType(Type)
    Object.ToString()
    Object.name
    Object.hideFlags
    Object.Equals(Object, Object)
    Object.ReferenceEquals(Object, Object)
    Object.GetType()
    Object.MemberwiseClone()
    Namespace: Unity.Netcode
    Syntax
    [AddComponentMenu("Netcode/Network Manager", -100)]
    public class NetworkManager : MonoBehaviour, INetworkUpdateSystem

    Fields

    LogLevel

    The log level to use

    Declaration
    [HideInInspector]
    public LogLevel LogLevel
    Field Value
    Type Description
    LogLevel

    NetworkConfig

    The current NetworkConfig

    Declaration
    [HideInInspector]
    public NetworkConfig NetworkConfig
    Field Value
    Type Description
    NetworkConfig

    PendingClients

    Gets a dictionary of the clients that have been accepted by the transport but are still pending by the Netcode. This is only populated on the server.

    Declaration
    public readonly Dictionary<ulong, PendingClient> PendingClients
    Field Value
    Type Description
    Dictionary<UInt64, PendingClient>

    RunInBackground

    Gets or sets if the application should be set to run in background

    Declaration
    [HideInInspector]
    public bool RunInBackground
    Field Value
    Type Description
    Boolean

    ServerClientId

    The client id used to represent the server

    Declaration
    public const ulong ServerClientId = 0UL
    Field Value
    Type Description
    UInt64

    Properties

    ConnectedClients

    Gets a dictionary of connected clients and their clientId keys. This is only accessible on the server.

    Declaration
    public IReadOnlyDictionary<ulong, NetworkClient> ConnectedClients { get; }
    Property Value
    Type Description
    IReadOnlyDictionary<UInt64, NetworkClient>

    ConnectedClientsIds

    Gets a list of just the IDs of all connected clients. This is only accessible on the server.

    Declaration
    public IReadOnlyList<ulong> ConnectedClientsIds { get; }
    Property Value
    Type Description
    IReadOnlyList<UInt64>

    ConnectedClientsList

    Gets a list of connected clients. This is only accessible on the server.

    Declaration
    public IReadOnlyList<NetworkClient> ConnectedClientsList { get; }
    Property Value
    Type Description
    IReadOnlyList<NetworkClient>

    ConnectedHostname

    The current host name we are connected to, used to validate certificate

    Declaration
    public string ConnectedHostname { get; }
    Property Value
    Type Description
    String

    ConnectionApprovalCallback

    The callback to invoke during connection approval. Allows client code to decide whether or not to allow incoming client connection

    Declaration
    public Action<NetworkManager.ConnectionApprovalRequest, NetworkManager.ConnectionApprovalResponse> ConnectionApprovalCallback { get; set; }
    Property Value
    Type Description
    Action<NetworkManager.ConnectionApprovalRequest, NetworkManager.ConnectionApprovalResponse>

    CustomMessagingManager

    Gets the CustomMessagingManager for this NetworkManager

    Declaration
    public CustomMessagingManager CustomMessagingManager { get; }
    Property Value
    Type Description
    CustomMessagingManager

    DisconnectReason

    When disconnected from the server, the server may send a reason. If a reason was sent, this property will tell client code what the reason was. It should be queried after the OnClientDisconnectCallback is called

    Declaration
    public string DisconnectReason { get; }
    Property Value
    Type Description
    String

    IsApproved

    Is true when the client has been approved.

    Declaration
    public bool IsApproved { get; }
    Property Value
    Type Description
    Boolean
    Remarks

    This only reflects the client's approved status and does not mean the client has finished the connection and synchronization process. The server-host will always be approved upon being starting the NetworkManager IsConnectedClient

    IsClient

    Gets Whether or not a client is running

    Declaration
    public bool IsClient { get; }
    Property Value
    Type Description
    Boolean

    IsConnectedClient

    When true, the client is connected, approved, and synchronized with the server.

    Declaration
    public bool IsConnectedClient { get; }
    Property Value
    Type Description
    Boolean

    IsHost

    Gets if we are running as host

    Declaration
    public bool IsHost { get; }
    Property Value
    Type Description
    Boolean

    IsListening

    Gets Whether or not we are listening for connections

    Declaration
    public bool IsListening { get; }
    Property Value
    Type Description
    Boolean

    IsServer

    Gets Whether or not a server is running

    Declaration
    public bool IsServer { get; }
    Property Value
    Type Description
    Boolean

    LocalClient

    Gets the local NetworkClient for this client.

    Declaration
    public NetworkClient LocalClient { get; }
    Property Value
    Type Description
    NetworkClient

    LocalClientId

    Returns ServerClientId if IsServer or LocalClientId if not

    Declaration
    public ulong LocalClientId { get; }
    Property Value
    Type Description
    UInt64

    LocalTime

    The local NetworkTime

    Declaration
    public NetworkTime LocalTime { get; }
    Property Value
    Type Description
    NetworkTime

    NetworkTickSystem

    Accessor for the NetworkTickSystem of the NetworkManager.

    Declaration
    public NetworkTickSystem NetworkTickSystem { get; }
    Property Value
    Type Description
    NetworkTickSystem

    NetworkTimeSystem

    Accessor for the NetworkTimeSystem of the NetworkManager. Prefer the use of the LocalTime and ServerTime properties

    Declaration
    public NetworkTimeSystem NetworkTimeSystem { get; }
    Property Value
    Type Description
    NetworkTimeSystem

    PrefabHandler

    The NetworkPrefabHandler instance created after starting the NetworkManager

    Declaration
    public NetworkPrefabHandler PrefabHandler { get; }
    Property Value
    Type Description
    NetworkPrefabHandler

    SceneManager

    The NetworkSceneManager instance created after starting the NetworkManager

    Declaration
    public NetworkSceneManager SceneManager { get; }
    Property Value
    Type Description
    NetworkSceneManager

    ServerTime

    The NetworkTime on the server

    Declaration
    public NetworkTime ServerTime { get; }
    Property Value
    Type Description
    NetworkTime

    ShutdownInProgress

    Can be used to determine if the NetworkManager is currently shutting itself down

    Declaration
    public bool ShutdownInProgress { get; }
    Property Value
    Type Description
    Boolean

    Singleton

    The singleton instance of the NetworkManager

    Declaration
    public static NetworkManager Singleton { get; }
    Property Value
    Type Description
    NetworkManager

    SpawnManager

    Gets the SpawnManager for this NetworkManager

    Declaration
    public NetworkSpawnManager SpawnManager { get; }
    Property Value
    Type Description
    NetworkSpawnManager

    Methods

    AddNetworkPrefab(GameObject)

    Adds a new prefab to the network prefab list. This can be any GameObject with a NetworkObject component, from any source (addressables, asset bundles, Resource.Load, dynamically created, etc)

    There are three limitations to this method:

    • If you have NetworkConfig.ForceSamePrefabs enabled, you can only do this before starting networking, and the server and all connected clients must all have the same exact set of prefabs added via this method before connecting
    • Adding a prefab on the server does not automatically add it on the client - it's up to you to make sure the client and server are synchronized via whatever method makes sense for your game (RPCs, configs, deterministic loading, etc)
    • If the server sends a Spawn message to a client that has not yet added a prefab for, the spawn message and any other relevant messages will be held for a configurable time (default 1 second, configured via NetworkConfig.SpawnTimeout) before an error is logged. This is intented to enable the SDK to gracefully handle unexpected conditions (slow disks, slow network, etc) that slow down asset loading. This timeout should not be relied on and code shouldn't be written around it - your code should be written so that the asset is expected to be loaded before it's needed.
    Declaration
    public void AddNetworkPrefab(GameObject prefab)
    Parameters
    Type Name Description
    GameObject prefab
    Exceptions
    Type Condition
    Exception

    DisconnectClient(UInt64)

    Disconnects the remote client.

    Declaration
    public void DisconnectClient(ulong clientId)
    Parameters
    Type Name Description
    UInt64 clientId

    The ClientId to disconnect

    DisconnectClient(UInt64, String)

    Disconnects the remote client.

    Declaration
    public void DisconnectClient(ulong clientId, string reason)
    Parameters
    Type Name Description
    UInt64 clientId

    The ClientId to disconnect

    String reason

    Disconnection reason. If set, client will receive a DisconnectReasonMessage and have the reason available in the NetworkManager.DisconnectReason property

    GetNetworkPrefabOverride(GameObject)

    Returns the GameObject to use as the override as could be defined within the NetworkPrefab list Note: This should be used to create GameObject pools (with NetworkObject components) under the scenario where you are using the Host model as it spawns everything locally. As such, the override will not be applied when spawning locally on a Host. Related Classes and Interfaces: NetworkPrefabHandler INetworkPrefabInstanceHandler

    Declaration
    public GameObject GetNetworkPrefabOverride(GameObject gameObject)
    Parameters
    Type Name Description
    GameObject gameObject

    the GameObject to be checked for a NetworkManager defined NetworkPrefab override

    Returns
    Type Description
    GameObject

    a GameObject that is either the override or if no overrides exist it returns the same as the one passed in as a parameter

    NetworkUpdate(NetworkUpdateStage)

    The update method that is being executed in the context of related NetworkUpdateStage.

    Declaration
    public void NetworkUpdate(NetworkUpdateStage updateStage)
    Parameters
    Type Name Description
    NetworkUpdateStage updateStage

    The NetworkUpdateStage that is being executed.

    Implements
    INetworkUpdateSystem.NetworkUpdate(NetworkUpdateStage)

    RemoveNetworkPrefab(GameObject)

    Remove a prefab from the prefab list. As with AddNetworkPrefab, this is specific to the client it's called on - calling it on the server does not automatically remove anything on any of the client processes.

    Like AddNetworkPrefab, when NetworkConfig.ForceSamePrefabs is enabled, this cannot be called after connecting.

    Declaration
    public void RemoveNetworkPrefab(GameObject prefab)
    Parameters
    Type Name Description
    GameObject prefab

    SetSingleton()

    Set this NetworkManager instance as the static NetworkManager singleton

    Declaration
    public void SetSingleton()

    Shutdown(Boolean)

    Globally shuts down the library. Disconnects clients if connected and stops server if running.

    Declaration
    public void Shutdown(bool discardMessageQueue = false)
    Parameters
    Type Name Description
    Boolean discardMessageQueue

    If false, any messages that are currently in the incoming queue will be handled, and any messages in the outgoing queue will be sent, before the shutdown is processed. If true, NetworkManager will shut down immediately, and any unprocessed or unsent messages will be discarded.

    StartClient()

    Starts a client

    Declaration
    public bool StartClient()
    Returns
    Type Description
    Boolean

    (/) returns true if NetworkManager started in client mode successfully.

    StartHost()

    Starts a Host

    Declaration
    public bool StartHost()
    Returns
    Type Description
    Boolean

    (/) returns true if NetworkManager started in host mode successfully.

    StartServer()

    Starts a server

    Declaration
    public bool StartServer()
    Returns
    Type Description
    Boolean

    (/) returns true if NetworkManager started in server mode successfully.

    Events

    OnClientConnectedCallback

    The callback to invoke once a client connects. This callback is only ran on the server and on the local client that connects.

    Declaration
    public event Action<ulong> OnClientConnectedCallback
    Event Type
    Type Description
    Action<UInt64>

    OnClientDisconnectCallback

    The callback to invoke when a client disconnects. This callback is only ran on the server and on the local client that disconnects.

    Declaration
    public event Action<ulong> OnClientDisconnectCallback
    Event Type
    Type Description
    Action<UInt64>

    OnClientStarted

    The callback to invoke once the local client is ready

    Declaration
    public event Action OnClientStarted
    Event Type
    Type Description
    Action

    OnClientStopped

    The callback to invoke once the local client stops

    Declaration
    public event Action<bool> OnClientStopped
    Event Type
    Type Description
    Action<Boolean>
    Remarks

    The parameter states whether the client was running in host mode

    OnServerStarted

    This callback is invoked when the local server is started and listening for incoming connections.

    Declaration
    public event Action OnServerStarted
    Event Type
    Type Description
    Action

    OnServerStopped

    This callback is invoked once the local server is stopped.

    Declaration
    public event Action<bool> OnServerStopped
    Event Type
    Type Description
    Action<Boolean>

    OnTransportFailure

    The callback to invoke if the NetworkTransport fails.

    Declaration
    public event Action OnTransportFailure
    Event Type
    Type Description
    Action
    Remarks

    A failure of the transport is always followed by the NetworkManager shutting down. Recovering from a transport failure would normally entail reconfiguring the transport (e.g. re-authenticating, or recreating a new service allocation depending on the transport) and restarting the client/server/host.

    Extension Methods

    NetworkUpdateLoop.RegisterAllNetworkUpdates(INetworkUpdateSystem)
    NetworkUpdateLoop.RegisterNetworkUpdate(INetworkUpdateSystem, NetworkUpdateStage)
    NetworkUpdateLoop.UnregisterAllNetworkUpdates(INetworkUpdateSystem)
    NetworkUpdateLoop.UnregisterNetworkUpdate(INetworkUpdateSystem, NetworkUpdateStage)
    In This Article
    • Fields
      • LogLevel
      • NetworkConfig
      • PendingClients
      • RunInBackground
      • ServerClientId
    • Properties
      • ConnectedClients
      • ConnectedClientsIds
      • ConnectedClientsList
      • ConnectedHostname
      • ConnectionApprovalCallback
      • CustomMessagingManager
      • DisconnectReason
      • IsApproved
      • IsClient
      • IsConnectedClient
      • IsHost
      • IsListening
      • IsServer
      • LocalClient
      • LocalClientId
      • LocalTime
      • NetworkTickSystem
      • NetworkTimeSystem
      • PrefabHandler
      • SceneManager
      • ServerTime
      • ShutdownInProgress
      • Singleton
      • SpawnManager
    • Methods
      • AddNetworkPrefab(GameObject)
      • DisconnectClient(UInt64)
      • DisconnectClient(UInt64, String)
      • GetNetworkPrefabOverride(GameObject)
      • NetworkUpdate(NetworkUpdateStage)
      • RemoveNetworkPrefab(GameObject)
      • SetSingleton()
      • Shutdown(Boolean)
      • StartClient()
      • StartHost()
      • StartServer()
    • Events
      • OnClientConnectedCallback
      • OnClientDisconnectCallback
      • OnClientStarted
      • OnClientStopped
      • OnServerStarted
      • OnServerStopped
      • OnTransportFailure
    • Extension Methods
    Back to top Copyright © 2023 Unity Technologies — Terms of use
    Generated by DocFX
    on 26 October 2023