Skip to content

Commit 13985e0

Browse files
update: NetworkManager start and stop notifications (#1480)
Co-authored-by: Amy Reeve <amy.reeve@unity3d.com>
1 parent 3335f15 commit 13985e0

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

docs/components/networkmanager.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,56 @@ public class ConnectionNotificationManager : MonoBehaviour
256256
}
257257
```
258258

259+
## NetworkManager start and stop notifications
260+
261+
The Netcode for GameObjects `NetworkManager` component has several events that provide additional opportunities for configuring and cleaning up depending on the notification type. The available notifications are:
262+
263+
- `OnInstantiated`: A static event that's invoked each time a new `NetworkManager` instance is created.
264+
- `OnClientStarted`: An event that's invoked before returning from the `NetworkManager.StartClient` or `NetworkManager.StartHost` methods.
265+
- `OnClientStopped`: An event that's invoked before the `NetworkManager` client or host instance is 100% shutdown.
266+
- If you plan on joining another session from within a subscribed callback, you should use a `CoRoutine` to delay for the end of the frame or the next frame (at a minimum) to provide the `NetworkManager` time to finalize the shutdown sequence.
267+
- `OnPreShutdown`: An event that's invoked just before the `NetworkManager` instance shuts down. This is a good place to save any states you want persisted.
268+
- `OnServerStarted`: An event that's invoked before returning from the `NetworkManager.StartServer` or `NetworkManager.StartHost` method.
269+
- `OnServerStopped`: An event that's invoked before the `NetworkManager` server or host instance is 100% shutdown.
270+
- If you plan on starting another session from within a subscribed callback, you should use a `CoRoutine` to delay for the end of the frame or the next frame (at a minimum) to provide the `NetworkManager` time to finalize the shutdown sequence.
271+
- `OnDestroying`: A static event that's invoked just before destroying `NetworkManager` instance.
272+
273+
### Host start and stop
274+
275+
When running a `NetworkManager` as a host you can receive `OnClientStarted`, `OnServerStarted`, `OnClientStopped`, and `OnServerStopped` notifications since a host is both a server and a client. Both the `OnServerStopped` and `OnClientStopped` notifications provide a `bool` as a parameter that reflects whether you were running as a host (`true` if running as a host and `false` if not) to provide you with context in the event you are using the same callback for clients too.
276+
277+
### Restarting a NetworkManager Session
278+
279+
If you plan to use a logic flow where, when an `OnClientStopped` or `OnServerStopped` event notification is triggered, you want to immediately join or start a new session, then it's highly recommended to use a `CoRoutine` within the subscribed callback that waits until the end of the frame or the next frame (at a minimum) before starting the `NetworkManager` again. This provides the `NetworkManager` time to finalize the shutdown sequence.
280+
281+
#### Example
282+
283+
```csharp
284+
private void OnServerStopped(bool isHost)
285+
{
286+
NetworkManager.Singleton.OnServerStopped -= OnServerStopped;
287+
// Defer starting the server until the next update loop/frame
288+
StartCoroutine(DelayedServerStart());
289+
}
290+
291+
private IEnumerator DelayedServerStart()
292+
{
293+
// Wait for the next fixed update (or any specified period of time)
294+
yield return new WaitForFixedUpdate();
295+
// Start the server again
296+
if (!NetworkManager.Singleton.StartHost())
297+
{
298+
Debug.LogError("[DelayedServerStart] Failed to start host!");
299+
}
300+
else
301+
{
302+
NetworkManager.Singleton.OnServerStopped += OnServerStopped;
303+
}
304+
yield break;
305+
}
306+
```
307+
308+
259309
## Additional resources
260310

261311
- [`NetworkManager` API documentation](https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@latest?subfolder=/api/Unity.Netcode.NetworkManager.html)

0 commit comments

Comments
 (0)