This scene will show you how easy it is to set up Networking in your Unity project and sync GameObject transforms and Physics Objects across the network whilst keeping them persistent. As long as the Server is running you can disconnect and re-connect, and your world will persist.
In this example a right click will spawn local Physics-based Objects that all other players will see. These Physics Objects will interact with each other and the physics for every object will be simulated locally on its authority. Clicking one of these objects will either take or relieve authority of the local client over the clicked object. You can disconnect and re-connect and the persistent Entities will all remain.
The controls at the top right of the screen allow the spawning of a unique object that works very similarly to the other Physics-based Objects. This bigger cube is set to No Duplicates
in its Uniqueness
property which means the Server will only allow one instance of this object to exist at a time. It also has its Lifetime
setting set to Session Based
this will cause the object to be deleted when its owner disconnects. Just like with the Physics-based Objects, a player can claim authority over this object by clicking it. If that player would then disconnect all clients will have the unique object deleted.
In the Hierarchy of the scene you can see three core Prefabs:
Core Scene Setup
and Coherence Setup
are present in all scenes and described in detail on Start Tutorial page.
Coherence Entity
is not present in this scene.
Input Manager
in the Core Scene Setup
Prefab is set up to spawn a Sample Entity Physics Persistent
where a click is performed.
Coherence Connection Events
handles overall scene connectivity. In this scene we use it to clean up objects the client has authority over when disconnecting.
The Physics Entity Spawner
component is a simple script to instantiate a Coherence Entity Physics Persistent
Prefab with a coherenceSync component that replicates the transform and position. The component also changes the material based on whether it is locally simulated or synced over the network.
The Coherence Entity Physics
variants have an Entity Lifetime Type
set to Persistent
. This means it will remain in the world as long as the Replication Server is running, even if all Clients disconnect. It also has Authority Transfer Style
set to Stealing
which means the Entity can be "stolen" and simulated on a Client requesting authority.
This is done via the Input Manager
in the Core Scene Setup
prefab. When the object is left-clicked, it sends the message "Adopt" to the GameObject on the specific Layer Mask "Physics Entities". The component called Coherence Handler
on Coherence Entity Physics
objects handles the Adopt call and requests authority change via the coherenceSync component.
Coherence Handler
is a basic layer for handling Commands and Events, both sending and receiving. You can create your own or reuse and extend this for your project.
You can build this scene via the Build Settings. Run the local Replication Server through the Window > Coherence > Settings window and see how it works. You can try running multiple Clients rather than just two and see how replication works for each.