Class NetworkSandbox
- Namespace
- Netick
- Assembly
- Netick.dll
Network Sandbox is what controls the whole network simulation. It can be thought of as the manager of the simulation. You can have more than one network sandbox in a single Unity game, and that happens when you start both a client and a server on the same project. This can be extremely useful for testing/debugging, because it allows you to run a server and a client(or multiple thereof) in the same project and therefore see what happens at both at the same time, without interference.
[DisallowMultipleComponent]
public sealed class NetworkSandbox : MonoBehaviour
- Inheritance
-
ObjectComponentBehaviourMonoBehaviourNetworkSandbox
- Inherited Members
-
MonoBehaviour.IsInvoking()MonoBehaviour.CancelInvoke()MonoBehaviour.StopCoroutine(Coroutine)MonoBehaviour.StopAllCoroutines()MonoBehaviour.useGUILayoutMonoBehaviour.runInEditModeBehaviour.enabledBehaviour.isActiveAndEnabledComponent.GetComponent<T>()Component.TryGetComponent<T>(out T)Component.GetComponentInChildren<T>()Component.GetComponentsInChildren<T>()Component.GetComponentInParent<T>()Component.GetComponentsInParent<T>()Component.GetComponents<T>()Component.transformComponent.gameObjectComponent.tagObject.GetInstanceID()Object.GetHashCode()Object.Instantiate(Object, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion, Transform)Object.Instantiate(Object)Object.Instantiate(Object, Transform)Object.Instantiate<T>(T)Object.Instantiate<T>(T, Vector3, Quaternion)Object.Instantiate<T>(T, Vector3, Quaternion, Transform)Object.Instantiate<T>(T, Transform)Object.Destroy(Object)Object.DestroyImmediate(Object)Object.DontDestroyOnLoad(Object)Object.DestroyObject(Object)Object.FindObjectsOfType<T>()Object.FindObjectOfType<T>()Object.ToString()Object.nameObject.hideFlags
- Extension Methods
Constructors
NetworkSandbox()
public NetworkSandbox()
Properties
Config
Configuration data for Netick.
public NetickConfig Config { get; }
Property Value
ConnectedClients
A list containing all connected clients currently.
Note: if you want the clients + the server, use ConnectedPlayers.
public NetickList<ServerConnection> ConnectedClients { get; }
Property Value
ConnectedPlayers
A list containing all connected clients currently, in addition to the server.
Note: if you only want the clients, use ConnectedClients.
public NetickList<NetworkPlayer> ConnectedPlayers { get; }
Property Value
ConnectedServer
The server the client is connected to.
public NetworkConnection ConnectedServer { get; }
Property Value
DeltaTime
public float DeltaTime { get; }
Property Value
FixedDeltaTime
Time period between network simulation steps. Same as UnityEngine.Time.fixedDeltaTime
public float FixedDeltaTime { get; }
Property Value
GetVersion
public string GetVersion { get; }
Property Value
InKBps
Incoming data in kilobytes per second (KBps).
public float InKBps { get; }
Property Value
InputEnabled
public bool InputEnabled { get; set; }
Property Value
InterpolationDelay
[Client Only] Interpolation delay in seconds.
public float InterpolationDelay { get; }
Property Value
IsApplyingReceivedData
Returns true when the client is at the stage of appling received server data.
public bool IsApplyingReceivedData { get; }
Property Value
IsClient
Returns true if this NetworkSandbox is a client.
public bool IsClient { get; }
Property Value
IsConnected
[Client Only] Returns true if this client is currently connected to a server.
public bool IsConnected { get; }
Property Value
IsResimulating
Returns true if we are currently resimulating a previous input/tick of the past. On the server, it always returns false since only the clients resimulate.
public bool IsResimulating { get; }
Property Value
IsRunning
Returns true if this NetworkSandbox has been started.
public bool IsRunning { get; }
Property Value
IsServer
Returns true if this NetworkSandbox is the server.
public bool IsServer { get; }
Property Value
IsVisiable
public bool IsVisiable { get; set; }
Property Value
LocalInterpolation
public LocalInterpolation LocalInterpolation { get; }
Property Value
LocalPlayer
This player.
public NetworkPlayer LocalPlayer { get; }
Property Value
Monitor
public Monitor Monitor { get; }
Property Value
Name
The name of this NetworkSandbox
public string Name { get; }
Property Value
NetworkTime
public float NetworkTime { get; }
Property Value
Objects
A list containing all simulated/registered network objects [NetworkObject] currently.
Note: to get a network object by id, use: TryGetObject(int, out NetworkObject)
public ObjectList Objects { get; }
Property Value
OutKBps
Outgoing data in kilobytes per second (KBps).
public float OutKBps { get; }
Property Value
Physics
The UnityEngine.PhysicsScene associated with this NetworkSandbox. Always use this to perform physics queries inside the sandbox instead of UnityEngine.Physics.
public PhysicsScene Physics { get; }
Property Value
- PhysicsScene
Physics2D
The UnityEngine.PhysicsScene2D associated with this NetworkSandbox. Always use this to perform physics queries inside the sandbox instead of UnityEngine.Physics2D.
public PhysicsScene2D Physics2D { get; }
Property Value
- PhysicsScene2D
Port
The network port used by Netick.
public int Port { get; }
Property Value
PostNetworkFixedUpdate
Called after all NetworkFixedUpdate methods have been executed.
Note: it's called with every resimulation on the client.
public Action PostNetworkFixedUpdate { get; set; }
Property Value
PreNetworkFixedUpdate
Called before any NetworkFixedUpdate method have been executed.
Note: it's called with every resimulation on the client.
public Action PreNetworkFixedUpdate { get; set; }
Property Value
RTT
[Client Only] The round-trip time (RTT) of the client in seconds.
public double RTT { get; }
Property Value
RemoteInterpolation
public RemoteInterpolation RemoteInterpolation { get; }
Property Value
ResimulationStep
public int ResimulationStep { get; }
Property Value
Resimulations
public int Resimulations { get; }
Property Value
RpcSource
Always always returns null except when called inside the body of an RPC method, it returns the NetworkConnection we are executing RPCs from.
public NetworkConnection RpcSource { get; }
Property Value
ScaledFixedDeltaTime
public float ScaledFixedDeltaTime { get; }
Property Value
Scene
The UnityEngine.SceneManagement.Scene associated with this NetworkSandbox.
public Scene Scene { get; }
Property Value
- Scene
SceneBuildIndex
public int SceneBuildIndex { get; }
Property Value
ServerEndPoint
[Client Only] The IPEndPoint of the server you are connected to. Returns null if you are not connected to any server, or if you are the server.
public IPEndPoint ServerEndPoint { get; }
Property Value
StartMode
public StartMode StartMode { get; }
Property Value
Tick
Current simulation tick.
On the server, it's always going forward/increasing.
On the client, during resimulations it returns the current resimulated tick. To check for resimulations, use IsResimulating.
public Tick Tick { get; }
Property Value
Timer
public GameClock Timer { get; }
Property Value
Transport
public NetworkTransport Transport { get; }
Property Value
UserObject
Use this to associate an object with this sandbox.
public object UserObject { get; set; }
Property Value
Methods
AttachBehaviour(NetickBehaviour)
Attachs a NetickBehaviour to the simulation.
public void AttachBehaviour(NetickBehaviour behaviour)
Parameters
behaviour
NetickBehaviour
AttachNonNetworkedRigidbody(GameObject)
public void AttachNonNetworkedRigidbody(GameObject rigidbodyGameObject)
Parameters
rigidbodyGameObject
GameObject
Connect(int, string, byte[], int)
[Client Only] Connects to a remote server.
Note: if you are already connected to a server, and you want to connect to a different one, you have to disconnect before calling this. Otherwise this method does nothing.
Note: connectionData array length should not be more than 200.
public void Connect(int port, string ip, byte[] connectionData = null, int connectionDataLength = 0)
Parameters
port
intPort of the server.
ip
stringIP address of the server.
connectionData
byte[]Data sent with the connection request. Could be used to decide whether or not to allow the connection.
connectionDataLength
int
CreateMatch(string)
[Server Only] Creates a match. Only functional if the low-level transport has support for matchmaking.
public void CreateMatch(string name)
Parameters
name
stringName of the match.
CreateRpc(int, NetworkBehaviour, bool, RpcPeers)
Internal method. Never call this.
public NetworkRPC CreateRpc(int id, NetworkBehaviour behaviour, bool isReliable, RpcPeers target)
Parameters
id
intbehaviour
NetworkBehaviourisReliable
booltarget
RpcPeers
Returns
DeattachBehaviour(NetickBehaviour)
Deattachs a NetickBehaviour from the simulation.
public void DeattachBehaviour(NetickBehaviour behaviour)
Parameters
behaviour
NetickBehaviour
DeattachNonNetworkedRigidbody(GameObject)
public void DeattachNonNetworkedRigidbody(GameObject rigidbodyGameObject)
Parameters
rigidbodyGameObject
GameObject
Destroy(NetworkObject)
Destroys a network object. Only call this on the server or on the client for spawn-predicted objects which have -1 Ids.
Note: never call this on a child NetworkObject of the original prefab, only the root of the prefab.
public void Destroy(NetworkObject obj)
Parameters
obj
NetworkObjectThe object to destroy.
DestroyPool(GameObject)
public void DestroyPool(GameObject networkedPrefab)
Parameters
networkedPrefab
GameObject
DisableComponent(Component)
public void DisableComponent(Component component)
Parameters
component
Component
DisconnectFromServer()
[Client Only] Disconnects this client from the server.
public void DisconnectFromServer()
EnableComponent(Component)
public void EnableComponent(Component component)
Parameters
component
Component
FetchPlayerInput<T>(out T, NetworkPlayer)
public bool FetchPlayerInput<T>(out T input, NetworkPlayer player) where T : NetworkInput
Parameters
input
Tplayer
NetworkPlayer
Returns
Type Parameters
T
FindGameObjectWithTag(string, bool)
public GameObject FindGameObjectWithTag(string tag, bool activeOnly = true)
Parameters
Returns
- GameObject
FindGameObjectsWithTag(string, bool)
public List<GameObject> FindGameObjectsWithTag(string tag, bool activeOnly = true)
Parameters
Returns
- List<GameObject>
FindGameObjectsWithTag(string, List<GameObject>, bool)
public List<GameObject> FindGameObjectsWithTag(string tag, List<GameObject> results, bool activeOnly = true)
Parameters
Returns
- List<GameObject>
FindObjectOfType<T>(bool)
public T FindObjectOfType<T>(bool activeOnly = true) where T : Component
Parameters
activeOnly
bool
Returns
- T
Type Parameters
T
FindObjectsOfType<T>(bool)
public List<T> FindObjectsOfType<T>(bool activeOnly = true) where T : Component
Parameters
activeOnly
bool
Returns
- List<T>
Type Parameters
T
FindObjectsOfType<T>(List<T>, bool)
public void FindObjectsOfType<T>(List<T> results, bool activeOnly = true) where T : Component
Parameters
Type Parameters
T
GetInput<T>()
Gets an input of the specified type. Use this method to get and update your inputs.
public T GetInput<T>() where T : NetworkInput, new()
Returns
- T
Type Parameters
T
Type of the input that will returned for you.
GetObject(int)
Gets the NetworkObject with the specified id. Returns null in case no object with that id exists.
public NetworkObject GetObject(int id)
Parameters
id
intThe id of the NetworkObject
Returns
GetPrefab(string)
Gets a Network Prefab by name.
Note: this causes GC.
public GameObject GetPrefab(string name)
Parameters
name
string
Returns
- GameObject
The Network Prefab
GetRpcCaller()
public NetworkPlayer GetRpcCaller()
Returns
InitializePool(GameObject, int, bool)
Initializes the pool for the specified prefab. After this method has been called for a certain prefab, all instances of that prefab will be recycled and reset when created/destroyed.
Note: this method should only be called on OnStartup(NetworkSandbox), in other words, just after Netick has been started.
public void InitializePool(GameObject networkedPrefab, int preloadedAmount, bool hideInactiveMembers = false)
Parameters
networkedPrefab
GameObjectPrefab to enable pooling for.
preloadedAmount
intHow many instances to be preloaded.
hideInactiveMembers
boolPass true to hide inactive pool members.
Instantiate(GameObject, Vector3, Quaternion)
Instantiates a prefab. This must be called instead of UnityEngine.Object.Instantiate(UnityEngine.Object) or its variations.
Note: this must be called only for non-networked prefabs. If you want to instantiate a network prefab, use NetworkInstantiate(GameObject, Vector3, Quaternion, NetworkPlayer) instead.
public GameObject Instantiate(GameObject prefab, Vector3 position, Quaternion rotation)
Parameters
prefab
GameObjectThe prefab to be instantiated.
position
Vector3Position of the instantiated object.
rotation
QuaternionRotation of the instantiated object.
Returns
- GameObject
Kick(NetworkConnection)
[Server Only] Disconnects a client from the server.
public void Kick(NetworkConnection client)
Parameters
client
NetworkConnectionThe client to be disconnected.
Log(object)
public void Log(object message)
Parameters
message
object
LogError(object)
public void LogError(object message)
Parameters
message
object
LogWarning(object)
public void LogWarning(object message)
Parameters
message
object
NetworkInstantiate(GameObject, Vector3, Quaternion, NetworkPlayer, SpawnPredictionKey)
Instantiates a network prefab.
Commonly, this should only be called on the server, since only the server can instantiate network prefabs. However, the client can also call this to spawn-predict a prefab by providing a spawn key which must be the same on the server and client when this method is called. Read the docs to learn more about how to use spawn-prediction.
Note: make sure the prefab has been registered.
Note: the specified input source will be given to every NetworkObject child of this prefab.
public NetworkObject NetworkInstantiate(GameObject prefab, Vector3 position, Quaternion rotation, NetworkPlayer inputSource = null, SpawnPredictionKey spawnPredictionKey = default)
Parameters
prefab
GameObjectThe prefab to be instantiated.
position
Vector3Position of the instantiated object.
rotation
QuaternionRotation of the instantiated object.
inputSource
NetworkPlayerInput source of the instantiated object.
spawnPredictionKey
SpawnPredictionKey
Returns
RefreshMatchList()
Requests an update to the match list. Only functional if the low-level transport has support for matchmaking.
public void RefreshMatchList()
SendRpc(NetworkRPC)
Internal method. Never call this.
public void SendRpc(NetworkRPC rpc)
Parameters
rpc
NetworkRPC
SetComponentEnabled(Component, bool)
public void SetComponentEnabled(Component component, bool enabled)
Parameters
component
Componentenabled
bool
SwitchScene(int)
[Server Only] Switches to a different scene. This must be called instead of LoadSceneAsync(int) or its variations.
public void SwitchScene(int sceneBuildIndex)
Parameters
sceneBuildIndex
intIndex of the desired scene.
TickToTime(Tick)
Converts tick
to time in seconds.
public float TickToTime(Tick tick)
Parameters
tick
Tick
Returns
TickToTime(float)
Converts tick
to time in seconds.
public float TickToTime(float tick)
Parameters
tick
float
Returns
TryGetBehaviour<T>(int, out T)
Trys to get the NetworkBehaviour of a NetworkObject with the specified id.
public bool TryGetBehaviour<T>(int id, out T behaviour) where T : NetworkBehaviour
Parameters
id
intThe id of the network object
behaviour
T
Returns
Type Parameters
T
TryGetObject(int, out NetworkObject)
Trys to get the NetworkObject with the specified id.
public bool TryGetObject(int id, out NetworkObject obj)
Parameters
id
intThe id of the NetworkObject
obj
NetworkObject