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 > Integrations > 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 KaijuTransport 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 KaijuTransport, but there are a few which are relevant:
KaijuTransport.Channel- The main channel over which information is sent.KaijuTransport.Lifetime- A secondary channel over which networking handshaking is performed.KaijuTransport.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 callKaijuTransport.Connect()once ready in the lobby to start Netcode for GameObjects.KaijuTransport.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, KMPE gives you a means to implement your own custom host migration. First, disable KaijuTransport.autoDisconnect. Then, listen for the KaijuTransport.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 KaijuMultiplayerManager.IsOwner, call KaijuTransport.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 KaijuTransport.Connect() themselves. If the members also need to load any serialized migration data, they can then do so.
User Link
The KaijuUserLink component provides an easy means to synchronize Kaiju Users with the network identifiers they are assigned by Netcode for GameObjects. In most use cases, you will want to attach a KaijuUserLink component to your player prefab, and KMPE will handle the synchronization with a KaijuUser component allowing you to easily access the user's name and icon. For more on the KaijuUser component and getting user information, see the users documentation. If you wish to access the synchronized identifiers outside of the KaijuUserLink component, you can use the KaijuTransport.SteamToNetcode(ulong) and KaijuTransport.NetcodeToSteam(ulong) methods.
Sample
To access the sample, open the package manager window by going to Window > Package Management > Package Manager. In the In Project tab, click on Kaiju Multiplayer Engine and click the Samples tab. First, import the Common sample followed by the Netcode for GameObjects sample. Then, from the top menu, go to Tools > Kaiju Solutions > Multiplayer Engine > Samples > Netcode for GameObjects to generate the sample scene.
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 KMPE.
Testing
To test your game's multiplayer functionality locally, check out the testing documentation.