Migrating From UNet to MLAPI
Use this step-by-step guide to migrate your project from UNet to MLAPI. Sample code is provided as available. We also recommend reviewing the latest MLAPI Release Notes.
note
If you need help, contact us in the Unity MLAPI Discord.
#
Current limitationsReview the following limitations for upgrade and migrations from previous versions of UNet to Unity MLAPI:
- Naming constraints may cause issues. UNet prefixed methods with
Cmd
orRpc
. MLAPI 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.
- MLAPI RPCs do not support arrays yet.
- Client and Server have separate representations in UNet. UNet has a number of callbacks that do not exist for MLAPI.
- Prefabs need to be added to the prefab registration list for MLAPI.
- Connection callbacks do not happen in single player or host mode.
- Matchmaking is not available in MLAPI 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.
#
Install MLAPI and restart UnitySee Installation for more information.
#
Update InvokingInvoking in the MLAPI is done by calling the Invoke
method on the NetworkBehaviour
instead of calling the method directly like in UNet.
See NetworkBehaviour for more information.
#
NetworkManagerUNET’s NetworkManager
is also called NetworkManager
in the MLAPI and works in a similar way.
note
You cannot inherit from NetworkManager
in MLAPI, which was a recommended pattern in UNET.
#
Replace NetworkManagerHUDCurrently MLAPI 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 the MLAPI and works in a similar way.
#
Replace UNet NetworkTransform with MLAPI NetworkTransformUNet’s NetworkTransform
is also called NetworkTransform
in the MLAPI 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 MLAPI NetworkAnimatorReplace UNEt's NetworkAnimator
with MLAPI's NetworkAnimator
component everywhere in your project.
#
Update NetworkBehaviourReplace UNet NetworkBehaviour
with MLAPI NetworkBehaviour
everywhere in your project.
- UNET Example
- MLAPI Example
See NetworkBehaviour for more information.
#
NetworkStartIn the MLAPI, RPC
s and NetworkVariable
changes will not be replicated if they are done before the NetworkStart
method is called. The NetworkStart
method is called when the NetworkObject
is ready for network use.
- MLAPI Example
#
Replace SyncVarReplace SyncVar
with NetworkVariable
everywhere in your project.
To achieve equivalent functionality of SyncVar
hooks in MLAPI subscribe a function to the OnValueChanged
callback of the NetworkVariable
. A noteable difference between the UNet hooks and the MLAPI OnValueChanged
callback is that MLAPI 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
- MLAPI Example
Replace all postfix increment and decrement usages of SyncVar in your project. MLAPI's NetworkVariable.Value
exposes a value type that's why postfix increment/decrement is not supported.
- UNET Example
- MLAPI 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
- MLAPI Example
#
Replace Command/ClientRPCUNet’s Command/ClientRPC
is replaced with Server/ClientRpc
in the MLAPI which works in a similar way.
- UNET Example
- MLAPI Example
note
In MLAPI 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
- MLAPI 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
- MLAPI Example
See Object Spawning for more information.
#
Custom Spawn HandlersMLAPI has Custom Spawn Handlers
to replace UNet's Custom Spawn Functions
. See Object Pooling for more information.
#
Replace NetworkContextPropertiesThe MLAPI has IsLocalPlayer
, IsClient
, IsServer
and IsHost
to replace UNets isLocalPlayer
, isClient
and isServer
. In the MLAPI 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 MLAPI visibilityThere is no direct equivalent to the NetworkPromimityChecker
UNet component in MLAPI. Network visiblilty for clients works similar as in UNet. MLAPI does not have an equivalent to the ObjectHide
message from UNet. In MLAPI 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 MLAPI by using NetworkObject.CheckObjectVisibility
. OnRebuildObservers
is not needed for MLAPIs visibilty system.
- UNET Example
- MLAPI Example
See Object Visbility to learn more about MLAPIs network visiblity check.
#
Update SceneManagementIn MLAPI, scene management is not done over the NetworkManager
like in UNet. The NetworkSceneManager
provides equivalent functionality for switching scenes.
- UNET Example
- MLAPI Example
#
Update ClientAttribute/ClientCallbackAttribute and ServerAttribute/ServerCallbackAttributeMLAPI 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
- MLAPI Example
#
Replace SyncEvent with RPC eventMLAPI does not provide an equivalent for SyncEvent
. To port SyncEvent
code from UNet to MLAPI, send an RPC to invoke the event on the other side.
- UNET Example
- MLAPI Example
#
Network DiscoveryMLAPI does not provide Network Discovery. The UNet Network Discovery is a standalone component that can be used with any networking solution. You can use the UNet Network Discovery to discover a broadcasting MLAPI host and then connect to it with MLAPI.
#
See AlsoFor more information, see the following: