Migrating From UNet to Netcode for GameObjects
Use this step-by-step guide to migrate your projects from Unity UNet to Netcode for GameObjects (Netcode) Sample code is provided as available. We also recommend reviewing the latest Release Notes .
note
If you need help, contact us in the Unity Multiplayer Networking Discord.
#
Current limitationsReview the following limitations for upgrade and migrations from previous versions of Unity UNet to Netcode:
- Naming constraints may cause issues. UNet prefixed methods with
Cmd
orRpc
. Netcode requires postfix. This may require either complicated multi-line regex to find and replace, or manual updates. For example,CommandAttribute
has been renamedServerRpcAttribute
andClientRPCAttribute
has been renamedClientRpcAttribute
. - Errors for RPC postfix naming patterns do not show in your IDE.
- Client and Server have separate representations in UNet. UNet has a number of callbacks that do not exist for Netcode.
- Prefabs need to be added to the prefab registration list for Netcode.
- Matchmaking is not available in Netcode at this time.
#
Backup your projectIt is recommended that you back up your project before proceeding with the migration. For example:
- Create a copy of your entire project folder.
- Use source control software, like Git.
bestpractice
We recommend using both methods to backup your project. This gives you a copied project and tracking through committed changes and history.
#
Install Netcode and restart UnitySee Installation for more information.
note
If you install Git for the first time, you will need to restart your system.
#
RPC InvokingInvoking an RPC works the same way as in UNet. Just call the function and it will send an RPC.
#
NetworkManagerUNET’s NetworkManager
is also called NetworkManager
in Netcode and works in a similar way.
note
We recommend you don't inherit from NetworkManager
in Netcode, which was a recommended pattern in UNET.
#
Replace NetworkManagerHUDCurrently Netcode offers no replacment for the NetworkMangerHUD.
The Community Contributions Extension Package contains a a drop in NetworkManagerHud
component you can use for a quick substitute.
#
Replace NetworkIdentity with NetworkObjectUNet’s NetworkIdentity
is called NetworkObject
in Netcode and works in a similar way.
#
Replace UNet NetworkTransform with Netcode NetworkTransformUNet’s NetworkTransform
is also called NetworkTransform
in Netcode and works in a similar way.
The NetworkTransform
does not have full feature parity with UNET's NetworkTransform
. It lacks features like position synchronizing for rigidbodies.
#
Replace UNet NetworkAnimator with Netcode NetworkAnimatorReplace UNEt's NetworkAnimator
with Netcode's NetworkAnimator
component everywhere in your project.
#
Update NetworkBehaviourReplace UNet NetworkBehaviour
with Netcode's NetworkBehaviour
everywhere in your project.
- UNET Example
- Netcode for GameObjects Example
See NetworkBehaviour for more information.
#
Replace SyncVarReplace SyncVar
with NetworkVariable
everywhere in your project.
To achieve equivalent functionality of SyncVar
hooks in Netcode subscribe a function to the OnValueChanged
callback of the NetworkVariable
. A noteable difference between the UNet hooks and Netcode's OnValueChanged
callback is that Netcode gives you both the old and the newly changed value while UNet provides you only with the old value. With UNet, you also had to manually assign the value of the SyncVar.
- UNET Example
- Netcode for GameObjects Example
Replace all postfix increment and decrement usages of SyncVar in your project. Netcode's NetworkVariable.Value
exposes a value type that's why postfix increment/decrement is not supported.
- UNET Example
- Netcode for GameObjects Example
See NetworkVariable for more information.
#
Replace SyncList with NetworkListReplace SyncList<T>
with NetworkList<T>
everywhere in your project. NetworkList
has a OnListChanged
event which is similar to UNet's Callback
.
- UNET Example
- Netcode for GameObjects Example
#
Replace Command/ClientRPCUNet’s Command/ClientRPC
is replaced with Server/ClientRpc
in Netcode which works in a similar way.
- UNET Example
- Netcode for GameObjects Example
note
In Netcode, RPC function names must end with a ClientRpc/ServerRpc
suffix.
See Messaging System for more information.
#
Replace OnServerAddPlayerReplace OnServerAddPlayer
with ConnectionApproval
everywhere in your project.
- UNET Example
- Netcode for GameObjects Example
Server-only example:
See Connection Approval for more information.
#
Replace NetworkServer.Spawn with NetworkObject.SpawnReplace NetworkServer.Spawn
with NetworkObject.Spawn
everywhere in your project.
- UNET Example
- Netcode for GameObjects Example
See Object Spawning for more information.
#
Custom Spawn HandlersNetcode has Custom Spawn Handlers
to replace UNet's Custom Spawn Functions
. See Object Pooling for more information.
#
Replace NetworkContextPropertiesNetcode has IsLocalPlayer
, IsClient
, IsServer
and IsHost
to replace UNets isLocalPlayer
, isClient
and isServer
. In Netcode each object can be owned by a specific peer. This can be checked with IsOwner
which is similar to UNets hasAuthority
.
#
Network Proximity Checker/ OnCheckObserver with Netcode visibilityThere is no direct equivalent to the NetworkPromimityChecker
UNet component in Netcode. Network visiblilty for clients works similar as in UNet. Netcode does not have an equivalent to the ObjectHide
message from UNet. In Netcode networked objects on the host are always visible. There is no equivalent to the OnSetLocalVisibility
function in UNet. A manual network promiximty implementation with the OnCheckObserver
can be ported to Netcode by using NetworkObject.CheckObjectVisibility
. OnRebuildObservers
is not needed for Netcode visibilty system.
- UNET Example
- Netcode for GameObjects Example
See Object Visbility to learn more about Netcodes network visiblity check.
#
Update SceneManagementIn Netcode, scene management is not done over the NetworkManager
like in UNet. The NetworkSceneManager
provides equivalent functionality for switching scenes.
- UNET Example
- Netcode for GameObjects Example
#
Update ClientAttribute/ClientCallbackAttribute and ServerAttribute/ServerCallbackAttributeNetcode currently does not offer a replacement for marking a function with an attribute so that it only runs on the server or the client. You can manually return out of the function instead.
- UNET Example
- Netcode for GameObjects Example
#
Replace SyncEvent with RPC eventNetcode does not provide an equivalent for SyncEvent
. To port SyncEvent
code from UNet to Netcode, send an RPC to invoke the event on the other side.
- UNET Example
- Netcode for GameObjects Example
#
Network DiscoveryNetcode does not provide Network Discovery. The Contributions repository provides an example implementation for NetworkDiscovery.
#
See AlsoFor more information, see the following:
- Release Notes - Learn more about updated and changed features, bug fixes, and known issues for Unity Netcode.
- API Reference - Review available APIs.
- Guides and tutorials - Review guides for Netcodev and check tutorials.