In this section, we will learn how to prepare a prefab for network replication.
Add an asset and create a prefab of it. Make sure the prefab is in a Resources
folder in your Unity project.
Here is an example:
GameObject -> 3D Object -> Cube
Create a Resources
folder in your Project. Drag the Cube into the Resources folder to turn it into a prefab.
It's important that your prefab is in a Resources folder so that Unity can load it at runtime. This is a Unity requirement, more info here.
The CoherenceSync
component will help you prepare an object for network synchronization during design time. It also exposes an API that allows us to manipulate the object during runtime.
CoherenceSync
will query all public variables and methods on any of the attached components, for example Unity components such as Transform
, Animator
, etc.. This will include any custom scripts such as PlayerInput
and even scripts that came with Asset Store packages you may have downloaded.
You can find out more about CoherenceSync here.
Select which variables you would like to sync across the network. Initially, this will probably be the Transform
settings; position, rotation, scale.
Under Configure
, click Select fields and methods
.
In the Configuration dialog, select position, rotation and scale.
Close the Configuration dialog_._
This simple input script will use WASD or the Arrow keys to move the prefab around the scene.
Click on Assets -> Create -> C# Script
.
Name it Move.cs
. Copy-paste the following content into the file.
Wait for Unity to compile the file, then add it onto the prefab.
We have added a Move
script to the prefab. This means that if we just run the scene, we will be able to use the keyboard to move the object around.
But what happens on another client where this object is not authoritative, but rather replicated? We will want the position to be replicated over the network, without the keyboard input interfering with it.
Open the Events
section in CoherenceSync
. Add a new On Network Instantiation
handler by clicking on the plus sign next to it.
Pull the Cube prefab into the Runtime Only / None (Object) field.
Now click the dropdown No Function and select Move -> bool enabled
.
Leave the Boolean field unchecked.
From the CoherenceSync
component you can configure settings for Lifetime (Session-based
or Persistent
, Authority transfer (Request
or Steal
), Simulation model (Client Side
, Server Side
or Server Side with Client Input
) and Adoption settings for when local persistent entities are orphaned.
You can find more information in the SDK Fundamentals. There are also some Events that are triggered at different times.
On Before Networked Instantiation
(before the network is instantiated)
On Networked Instantiation
(when the GameObject is instantiated)
On Networked Destruction
(when the GameObject is destroyed)
On Authority Gained
(when authority over the GameObject is transferred to the local client)
On Authority Lost
(when authority over the GameObject is transferred to another client)
On After Authority Transfer Rejected
(when GameObject's Authority transfer was requested and denied).
On Input Simulator Connected
(when client with simulator is ready for Server-side with Client Input)
There are some constraints when setting up a prefab with CoherenceSync
, hereafter referred to as Sync Prefab
.
A Sync Prefab
has one, and only one CoherenceSync
component in its hierarchy
The CoherenceSync
component must be at the Sync Prefab
root
A Sync Prefab
cannot contain instances of other Sync Prefabs
A hierarchy in a scene can contain multiple Sync Prefabs
. However, such a hierarchy cannot be saved as a Sync Prefab
as that would break rule 1-3.