Netcode for GameObjects
Netcode for GameObjects is the official networking library made by Unity which is available for free under the Unity Companion License.
Getting Started
To install Netcode for GameObjects, from the top menu, go to Tools > Kaiju Solutions > Multiplayer Engine for Steam > Dependencies > Install > Netcode for GameObjects
. If you have other networking libraries installed, it will prompt you to ask if you wish to uninstall them. It is recommended to do this to ensure conflicts do not happen. Once installed, if you have any other networking integrations in the project, you will also be prompted to remove them. This is also recommended to ensure you do not accidentally use the wrong components from the other networking integrations.
Components
The integration is composed of two components, being a transport for sending data over the network and a component to synchronize users over the network.
Transport
The SteamTransport
component is the core of the integration. To use it, assign it to your Netcode for GameObjects NetworkManager
. You do not need to access most properties or methods manually on the SteamTransport
, but there are a few which are relevant:
SteamTransport.Channel
- The main channel over which information is sent.SteamTransport.Lifetime
- A secondary channel over which networking handshaking is performed.SteamTransport.autoConnect
- If you wish to automatically start Netcode for GameObjects when joining a lobby. For most simple use cases, you will want to keep this enabled. However, if you are interested in creating a pre-match character selection or group matchmaking screen, you may consider turning this off. If turned off, you will need to later callSteamTransport.Connect()
once ready in the lobby to start Netcode for GameObjects.SteamTransport.autoDisconnect
- Determines if the client should leave the lobby when Netcode for GameObjects shuts down. If you wish to implement custom host migration, turn this off.
Host Migration
Netcode for GameObjects does not support host migration out of the box. However, MPE4S gives you a means to implement your own custom host migration. First, disable SteamTransport.autoDisconnect
. Then, listen for the SteamTransport.OnStopping
callback. When triggered, you should serialize all information needed for migration to continue the game. Then, if you are the new owner which can be checked by SteamManager.IsOwner
, call SteamTransport.Connect()
to start a new game, and load all the serialized information. Lastly, indicate to other members in the lobby that the migrated game is ready, and they will then call SteamTransport.Connect()
themselves. If the members also need to load any serialized migration data, they can then do so.
User Link
The SteamUserLink
component provides an easy means to synchronize Steam users with the network identifiers they are assigned by Netcode for GameObjects. In most use cases, you will want to attach a SteamUserLink
component to your player prefab, and MPE4S will handle the synchronization with a SteamUser
component allowing you to easily access the user's name and icon. For more on the SteamUser
component and getting user information, see the users documentation. If you wish to access the synchronized identifiers outside of the SteamUserLink
component, you can use the SteamTransport.SteamToNetcode(ulong)
and SteamTransport.NetcodeToSteam(ulong)
methods.
Sample
After installing Netcode for GameObjects, a basic sample will be created at Assets > KaijuSolutions > SteamMultiplayerEngine > NetcodeForGameObjects > Samples > Multiplayer Engine for Steam - Netcode for GameObjects
. This sample demonstrates how to utilize the transport and user link components. In this sample, control your player using WASD or the arrow keys. If the sample was not automatically created, you can manually generate it by clicking Tools > Kaiju Solutions > Multiplayer Engine for Steam > Dependencies > Netcode for GameObjects Sample
from the top menu, unless you have already removed all sample scripts from the project in which case you will need to reinstall MPE4S to get the sample back.
Resources
- Official documentation.
- A simple tutorial and a full game guide by Code Monkey. Note that some parts of these tutorials, such as port forwarding, are not relevant for MPE4S.
Testing
To test your game's multiplayer functionality locally, check out the testing documentation.