You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Take care with using `INetworkSerializeByMemcpy`, and especially `ForceNetworkSerializeByMemcpy`, because not all unmanaged structs are actually compatible with this type of serialization. Anything that includes pointer types (including Native Collections like `NativeArray<>`) won't function correctly when serialized this way, and will likely cause memory corruption or crashes on the receiving side.
Copy file name to clipboardExpand all lines: docs/basics/connection-approval.md
+6-2
Original file line number
Diff line number
Diff line change
@@ -3,9 +3,13 @@ id: connection-approval
3
3
title: Connection approval
4
4
---
5
5
6
-
With every new connection, Netcode for GameObjects performs a handshake in addition to handshakes done by the transport. This ensures that the NetworkConfig on the client matches the server's NetworkConfig. You can enable ConnectionApproval in the NetworkManager or via code by setting `NetworkManager.NetworkConfig.ConnectionApproval` to `true`. Connection approval allows you to decide, on a per connection basis, whether to allow a connection. You can also use connection approval to specify the player Prefab to create, allowing you to override the default NetworkManager-defined player Prefab on a per player basis.
6
+
Connection approval allows you to decide for every new client connection attempt whether to allow or deny the client to connect.
7
7
8
-
When you set the ConnectionApproval property of the NetworkManager to true, Netcode for GameObjects checks to make sure the `NetworkManager.ConnectionApprovalCallback` has been assigned. If assigned, the connection approval process is used for connecting clients to decide whether to allow a connection or deny it. If you don't assign the `NetworkManager.ConnectionApprovalCallback` (even with the `NetworkManager.ConnectionApprovalCallback` set to `true`), Netcode for GameObjects uses basic authentication for the user, which automatically authorizes and assigns the default player Prefab.
8
+
You can also send additional payload data via connection approval to initialize the client. For instance you can use this data to have a client specify a custom player Prefab (see example below).
9
+
10
+
You have to enable ConnectionApproval in the NetworkManager Inspector or by setting `NetworkManager.NetworkConfig.ConnectionApproval = true` in a script before starting the host/server. If connection approval is not enabled, the `NetworkManager.ConnectionApprovalCallback` is not invoked.
11
+
12
+
In both cases clients also have to pass internal authentication, to confirm that their NetworkConfig matches that of the server.
Copy file name to clipboardExpand all lines: docs/basics/networkobject.md
+29-2
Original file line number
Diff line number
Diff line change
@@ -70,7 +70,7 @@ When you want to despawn and destroy the owner but you don't want to destroy a s
70
70
71
71
## Player NetworkObjects
72
72
73
-
Player objects are an optional feature in Netcode for GameObjects that you can use to assign a networked object to a specific client. A client can always only have at most one player object.
73
+
Player objects are an optional feature in Netcode for GameObjects that you can use to assign a networked object to a specific client. A client can only have at most one player object.
74
74
75
75
If you want a client to control more than one NetworkObject, use the ownership methods described above under the ownership section.
If the player already had a prefab instance assigned, then the client owns the NetworkObject of that prefab instance unless there's additional server-side specific user code that removes or changes the ownership.
90
90
91
+
### Defining defaults for PlayerObjects
92
+
93
+
If you're using `UnityEngine.InputSystem.PlayerInput` or `UnityEngine.PhysicsModule.CharacterController` components on your player prefab(s), you should disable them by default and only enable them for the local client's PlayerObject. Otherwise, you may get events from the most recently instantiated player prefab instance, even if it isn't the local client instance.
94
+
95
+
You can disable these components in the **Inspector** view on the prefab itself, or disable them during `Awake` in one of your `NetworkBehaviour` components. Then you can enable the components only on the owner's instance using code like the example below:
96
+
97
+
```
98
+
PlayerInput m_PlayerInput;
99
+
private void Awake()
100
+
{
101
+
m_PlayerInput = GetComponent<PlayerInput>();
102
+
m_PlayerInput.enabled = false;
103
+
}
104
+
105
+
public override void OnNetworkSpawn()
106
+
{
107
+
m_PlayerInput.enabled = IsOwner;
108
+
base.OnNetworkSpawn();
109
+
}
110
+
111
+
public override void OnNetworkDespawn()
112
+
{
113
+
m_PlayerInput.enabled = false;
114
+
base.OnNetworkDespawn();
115
+
}
116
+
```
117
+
91
118
### Finding PlayerObjects
92
119
93
120
To find a PlayerObject for a specific client ID, you can use the following methods:
@@ -140,7 +167,7 @@ If you're planning to use a NetworkTransform, then you always want to make sure
140
167
141
168
When a GameObject is instantiated, it gets instantiated in the current active scene. However, sometimes you might find that you want to change the currently active scene and would like specific NetworkObject instances to automatically migrate to the newly assigned active scene. While you could keep a list or table of the NetworkObject instances and write the code/logic to migrate them into a newly assigned active scene, this can be time consuming and become complicated depending on project size and complexity. The alternate and recommended way to handle this is by enabling the **Active Scene Synchronization** property of each NetworkObject you want to automatically migrate into any newly assigned scene. This property defaults to disabled.
142
169
143
-
Refer to the [NetworkSceneManager active scene synchronization](../basics/scenemanagement/using-networkscenemanager#active-scene-synchronization) page for more details.
170
+
Refer to the [NetworkSceneManager active scene synchronization](../scenemanagement/using-networkscenemanager#active-scene-synchronization) page for more details.
Copy file name to clipboardExpand all lines: docs/basics/networkvariable.md
+11-5
Original file line number
Diff line number
Diff line change
@@ -70,6 +70,12 @@ Additionally, any type that has a managed type is itself a managed type - so a s
70
70
71
71
Finally, while managed `INetworkSerializable` types are serialized in-place (and thus don't incur allocations for simple value updates), C# arrays and managed types serialized through custom serialization are **not** serialized in-place, and will incur an allocation on every update.
72
72
73
+
### Using collections with `NetworkVariable`s
74
+
75
+
You can use `NetworkVariable`s with both managed and unmanaged collections, but you need to call `NetworkVariable<T>.CheckDirtyState()` after making changes to a collection (or items in a collection) for those changes to be detected. Then the [`OnValueChanged`](#onvaluechanged-example) event will trigger, if subscribed locally, and by the end of the frame the rest of the clients and server will be synchronized with the detected change(s).
76
+
77
+
`NetworkVariable<T>.CheckDirtyState()` checks every item in a collection, including recursively nested collections, which can have a significant impact on performance if collections are large. If you're making multiple changes to a collection, you only need to call `NetworkVariable<T>.CheckDirtyState()` once after all changes are complete, rather than calling it after each change.
78
+
73
79
## Synchronization and notification example
74
80
75
81
The following client-server example shows how the initial `NetworkVariable` synchronization has already occurred by the time `OnNetworkSpawn` is invoked. It also shows how subscribing to `NetworkVariable.OnValueChanged` within `OnNetworkSpawn` provides notifications for any changes to `m_SomeValue.Value` that occur.
@@ -244,7 +250,7 @@ Owner writer permissions are owner-only.
244
250
245
251
There are two options for reading a `NetworkVariable.Value`:
246
252
247
-
-**Everyone(default)**: both owners and non-owners of the NetworkObject can read the value.
253
+
-**Everyone(default)**: both owners and non-owners of the NetworkObject can read the value.
248
254
- This is useful for global states that all clients should be aware of, such as player scores, health, or any other state that all clients should know about.
249
255
- We provided an example of maintaining a door's open or closed state using the everyone permission.
250
256
-**Owner**: only the owner of the NetworkObject and the server can read the value.
@@ -254,9 +260,9 @@ There are two options for reading a `NetworkVariable.Value`:
254
260
255
261
There are two options for writing a `NetworkVariable.Value`:
256
262
257
-
-**Server(default)**: the server is the only one that can write the value.
263
+
-**Server**: the server is the only one that can write the value. This is the default for [client-server contexts](../terms-concepts/client-server.md).
258
264
- This is useful for server-side specific states that all clients should be aware of but can't change, such as an NPC's status or some global world environment state (that is, is it night or day time).
259
-
-**Owner**: only the owner of the NetworkObject can write to the value.
265
+
-**Owner**: only the owner of the NetworkObject can write to the value. This is the default for [distributed authority contexts](../terms-concepts/distributed-authority.md).
260
266
- This is useful if your `NetworkVariable` represents something specific to the client's player that only the owning client should be able to set, such as a player's skin or other cosmetics.
261
267
262
268
### Permissions example
@@ -349,7 +355,7 @@ public class PlayerState : NetworkBehaviour
349
355
voidAwake()
350
356
{
351
357
//NetworkList can't be initialized at declaration time like NetworkVariable. It must be initialized in Awake instead.
352
-
//If you do initialize at declaration, you will run into Memmory leak errors.
358
+
//If you do initialize at declaration, you will run into memory leak errors.
0 commit comments