Skip to content

Commit e8f8981

Browse files
chore: Add DA ConnectionApprovalCallback warnings (#3658)
## Purpose of this PR [A post on the discussions board](https://discussions.unity.com/t/connection-approval-callback-not-firing/1682730) pointed out that we don't have any information around the `ConnectionApprovalCallback` indicating that it won't work with distributed authority. This PR: - Adds a note on the connection approval docs page to indicate that it doesn't work with DA - Updates the NetworkManager docs page to be up-to-date with DA - Adds a developer log level warning on ConnectionApproval when using DA that the ConnectionApproval setting is not valid (might not be the best location, maybe better in `StartClient`?) - Updates the NetworkTransformOwnershipTests to be DA friendly (needed for manual testing of this feature) ### Jira ticket n/a ### Changelog [//]: # (updated with all public facing changes - API changes, UI/UX changes, behaviour changes, bug fixes. Remove if not relevant.) - Added: A warning when `NetworkManager.NetworkConfig.ConnectionApproval` is set in a DA context. ## Documentation - Includes edits to existing public API documentation. ## Docs screenshots ### Connection Approval: <img width="737" height="145" alt="Screenshot 2025-09-09 at 11 37 58 AM" src="https://github.com/user-attachments/assets/97f321cf-a69b-46b1-b4bb-c2e5b14a214c" /> ### NetworkConfig settings <img width="726" height="750" alt="Screenshot 2025-09-09 at 11 37 48 AM" src="https://github.com/user-attachments/assets/6e55db7a-c51e-4083-9562-fc93db8ff08a" /> ### NetworkManager start <img width="722" height="646" alt="Screenshot 2025-09-09 at 11 37 36 AM" src="https://github.com/user-attachments/assets/5e7a81d7-7108-40b2-81da-f2986513e2f5" /> --------- Co-authored-by: Noel Stephens <[email protected]>
1 parent d660dfa commit e8f8981

File tree

5 files changed

+70
-35
lines changed

5 files changed

+70
-35
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1010

1111
### Added
1212

13+
- Added a warning when `NetworkManager.NetworkConfig.ConnectionApproval` is set in a distributed authority context. (#3658)
1314

1415
### Changed
1516

com.unity.netcode.gameobjects/Documentation~/basics/connection-approval.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Connection approval
22

3+
> [!NOTE]
4+
>Connection approval is not currently supported when using a [distributed authority topology](../terms-concepts/distributed-authority.md).
5+
36
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.
47

58
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.

com.unity.netcode.gameobjects/Documentation~/components/core/networkmanager.md

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ The NetworkManager is a required Netcode for GameObjects component that has all
55
## NetworkManager Inspector properties
66

77
- **LogLevel**: Sets the network logging level
8-
- **PlayerPrefab**: When a Prefab is assigned, the Prefab will be instantiated as the player object and assigned to the newly connected and authorized client. For more information about player prefabs, refer to [Player NetworkObjects](networkobject.md#player-networkobjects).
8+
- **PlayerPrefab**: When a Prefab is assigned, the Prefab will be instantiated as the player object. For more information about player prefabs, refer to [Player NetworkObjects](networkobject.md#player-networkobjects).
99
- **NetworkPrefabs**: Where you register your network prefabs. You can also create a single network Prefab override per registered network Prefab here.
1010
- **Protocol Version**: Set this value to help distinguish between builds when the most current build has new assets that can cause issues with older builds connecting.
11-
- **Network Transport**: Where your network specific settings and transport type is set. This field accepts any INetworkTransport implementation. However, unless you have unique transport specific needs UnityTransport is the recommended transport to use with Netcode for GameObjects.
11+
- **Network Transport**: Where your network specific settings and transport type is set. When using a [client-server topology](../../terms-concepts/client-server.md), This field accepts any INetworkTransport implementation. However, unless you have unique transport specific needs UnityTransport is the recommended transport to use with Netcode for GameObjects. For a [distributed authority topology](../../terms-concepts/distributed-authority.md), refer to the [distributed authority quickstart](../../learn/distributed-authority-quick-start.md).
1212
- **Tick Rate**: This value controls the network tick update rate.
1313
- **Ensure Network Variable Length Safety**: (Increases cpu processing and bandwidth) When this property is checked, Netcode for GameObjects will prevent user code from writing past the boundaries of a NetworkVariable.
14-
- **Connection Approval**: This enables [connection approval](../../basics/connection-approval.md) when this is checked and the `NetworkManager.ConnectionApprovalCallback` is assigned.
14+
- **Connection Approval**: This enables [connection approval](../../basics/connection-approval.md) when this is checked and the `NetworkManager.ConnectionApprovalCallback` is assigned. This setting does nothing when using a [distributed authority topology](../../terms-concepts/distributed-authority.md).
1515
- **Client Connection Buffer Timeout**: This value sets the amount of time that has to pass for a connecting client to complete the connection approval process. If the time specified is exceeded the connecting client will be disconnected.
16-
- **Force Same Prefabs**: When checked it will always verify that connecting clients have the same registered network prefabs as the server. When not checked, Netcode for GameObjects will ignore any differences.
16+
- **Force Same Prefabs**: When checked it will always verify that connecting clients have the same registered network prefabs as the [authority game client](../../terms-concepts/authority.md). When not checked, Netcode for GameObjects will ignore any differences.
1717
- **Recycle Network Ids**: When checked this will re-use previously assigned `NetworkObject.NetworkObjectIds` after the specified period of time.
1818
- **Network Id Recycle Delay**: The time it takes for a previously assigned but currently unassigned identifier to be available for use.
1919
- **Enable Scene Management**: When checked, Netcode for GameObjects will handle scene management and client synchronization for you. When not checked, you will have to create your own scene management scripts and handle client synchronization.
@@ -34,12 +34,32 @@ NetworkManager is also where you can find references to other Netcode related ma
3434

3535
## Starting a server, host, or client
3636

37-
To perform any netcode-related action that involves sending messages, you must first have a server started and listening for connections with at least one client (_a server can send RPCs to itself when running as a host_) that is connected. To accomplish this, you must first start your NetworkManager as a server, host, or client. There are three NetworkManager methods you can invoke to accomplish this:
37+
To perform any netcode-related action that involves sending messages you must first start your NetworkManager. Your NetworkManager can be started as either a server, host, or a client.
38+
39+
### Starting as a server
40+
41+
A server can't act as a local client. When starting as a server, no messages will be sent until at least one other client is connected. A server can only be used with a [client-server topology](../../terms-concepts/client-server.md).
42+
43+
```csharp
44+
NetworkManager.Singleton.StartServer();
45+
```
46+
47+
### Starting as a host
48+
49+
A host is both a server and a client (that is, has local client). A host can send RPCs to itself, and so can start sending some messages before another client is connected. A host can only be used with a [client-server topology](../../terms-concepts/client-server.md).
50+
51+
```csharp
52+
NetworkManager.Singleton.StartHost();
53+
```
54+
55+
### Starting as a client
56+
57+
This is the most general option for the NetworkManager. Starting as a client starts a single game client that will either connect to the server or host (under a client-server topology) or connect to a distributed authority session (under a distributed authority topology).
58+
59+
When using the distributed authority topology, the first client who joins and connects to the session will become the [session owner](../../terms-concepts/distributed-authority.md#session-ownership)
3860

3961
```csharp
40-
NetworkManager.Singleton.StartServer(); // Starts the NetworkManager as just a server (that is, no local client).
41-
NetworkManager.Singleton.StartHost(); // Starts the NetworkManager as both a server and a client (that is, has local client)
42-
NetworkManager.Singleton.StartClient(); // Starts the NetworkManager as just a client.
62+
NetworkManager.Singleton.StartClient();
4363
```
4464

4565
> [!NOTE]
@@ -56,7 +76,8 @@ When starting a client, the NetworkManager uses the IP and the Port provided in
5676

5777
The below examples use [Unity Transport](https://docs.unity3d.com/Packages/com.unity.transport@latest?subfolder=/manual/index.html) to show a few ways you can gain access to the `UnityTransport` component via the `NetworkManager.Singleton` to configure your project's network settings programmatically:
5878

59-
If you are only setting the IP address and port number, then you can use the `UnityTransport.SetConnectionData` method:
79+
If you are only setting the IP address and port number, then you can use the `UnityTransport.SetConnectionData` method. This will return a [`ConnectionAddressData` **struct**](https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/blob/11922a0bc100a1615c541aa7298c47d253b74937/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs#L239-L286), holding this info. You are strongly advised to use the `SetConnectionData` method to update this info.
80+
6081
```csharp
6182
NetworkManager.Singleton.GetComponent<UnityTransport>().SetConnectionData(
6283
"127.0.0.1", // The IP address is a string
@@ -65,6 +86,7 @@ NetworkManager.Singleton.GetComponent<UnityTransport>().SetConnectionData(
6586
```
6687

6788
If you are using the same code block to configure both your server and your client and you want to configure your server to listen to all IP addresses assigned to it, then you can also pass a 'listen address' of "0.0.0.0" to the `SetConnectionData` method, like so:
89+
6890
```csharp
6991
NetworkManager.Singleton.GetComponent<UnityTransport>().SetConnectionData(
7092
"127.0.0.1", // The IP address is a string
@@ -76,11 +98,8 @@ NetworkManager.Singleton.GetComponent<UnityTransport>().SetConnectionData(
7698
> [!NOTE]
7799
> Using an IP address of 0.0.0.0 for the server listen address will make a server or host listen on all IP addresses assigned to the local system. This can be particularly helpful if you are testing a client instance on the same system as well as one or more client instances connecting from other systems on your local area network. Another scenario is while developing and debugging you might sometimes test local client instances on the same system and sometimes test client instances running on external systems.
78100
79-
It is possible to access the current connection data at runtime, via `NetworkManager.Singleton.GetComponent<UnityTransport>().ConnectionData`. This will return a [`ConnectionAddressData` **struct**](https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/blob/11922a0bc100a1615c541aa7298c47d253b74937/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs#L239-L286), holding this info. You are strongly advised to use the `SetConnectionData` method to update this info.
80-
81101
If you are using Unity Relay to handle connections, however, **don't use `SetConnectionData`**. The host should call [`SetHostRelayData`](https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/blob/11922a0bc100a1615c541aa7298c47d253b74937/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs#L575), and clients should call [`SetClientRelayData`](https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/blob/11922a0bc100a1615c541aa7298c47d253b74937/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs#L588). Attempting to join a **Relay**-hosted game via entering IP/port number (via `SetConnectionData`) **won't work**.
82102

83-
84103
[More information about Netcode for GameObjects Transports](../../advanced-topics/transports.md)
85104

86105
## Disconnecting and shutting down
@@ -136,21 +155,24 @@ void DisconnectPlayer(NetworkObject player)
136155

137156
### Client disconnection notifications
138157

139-
Both the client and the server can subscribe to the `NetworkManager.OnClientDisconnectCallback` event to be notified when a client is disconnected.
158+
Subscribe to the `NetworkManager.OnClientDisconnectCallback` event to receive notifications when a client is disconnected.
140159

141160
**When disconnect notifications are triggered:**
142-
- Clients are notified when they're disconnected by the server.
161+
162+
- Clients are notified when they're disconnected by the server or from the distributed authority session.
143163
- The server is notified when any client disconnects from the server, whether the server disconnects the client or the client disconnects itself.
144164
- Both the server and clients are notified when their network connection is unexpectedly disconnected (network interruption).
145165

146166
**Client notification identifiers**
167+
147168
- On the server-side, the client identifier parameter is the identifier of the client that disconnects.
148169
- On the client-side, the client identifier parameter is the identifier assigned to the client.
149170
- _The exception to this is when a client is disconnected before its connection is approved._
150171

151172
You can also use the `NetworkManager.OnServerStopped` and `NetworkManager.OnClientStopped` callbacks to get local notifications when the server or client stops respectively.
152173

153174
### Connection notification manager example
175+
154176
Below is one example of how you can provide client connect and disconnect notifications to any type of NetworkBehaviour or MonoBehaviour derived component.
155177

156178
> [!NOTE]

com.unity.netcode.gameobjects/Runtime/Messaging/Messages/ConnectionApprovedMessage.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,16 @@ public void Handle(ref NetworkContext context)
278278
if (networkManager.DistributedAuthorityMode)
279279
{
280280
networkManager.SetSessionOwner(GetSessionOwner());
281-
if (networkManager.LocalClient.IsSessionOwner && networkManager.NetworkConfig.EnableSceneManagement)
281+
if (networkManager.LocalClient.IsSessionOwner)
282282
{
283-
networkManager.SceneManager.InitializeScenesLoaded();
283+
if (networkManager.NetworkConfig.EnableSceneManagement)
284+
{
285+
networkManager.SceneManager.InitializeScenesLoaded();
286+
}
287+
if (networkManager.NetworkConfig.ConnectionApproval && networkManager.LogLevel <= LogLevel.Developer)
288+
{
289+
NetworkLog.LogWarning($"{nameof(NetworkConfig.ConnectionApproval)} is enabled but is not supported when using a distributed authority topology. The {nameof(NetworkManager.ConnectionApprovalCallback)} will not be invoked.");
290+
}
284291
}
285292
}
286293

0 commit comments

Comments
 (0)