LogoLogo
HomeOnline DashboardAPIDiscordForums
SDK 0.5.2
SDK 0.5.2
  • Welcome
  • Overview
    • What is coherence?
    • How does coherence work?
    • Features and Roadmap
    • Requirements
  • Get Started
    • Install coherence
    • Scene setup
    • Prefab setup
    • Build and run
    • Baking and code generation
    • Create a free account
    • Deploy replication server
    • Share builds
  • Authority and communication
    • How authority works
    • Authority transfer
    • Commands
    • Client messages
    • Server-side and input queues
    • Animations
  • Persistence
    • Overview
    • Configuring persistence
    • Storage
    • Example – A global counter
  • Optimization
    • Overview
    • Simulation frequency
    • Areas of interest
    • World size
    • Level of detail
    • Interpolation
    • Extrapolation
  • Connected entities
    • Overview
    • Entity references
    • Parent-child relationships
  • Simulators
    • Overview
    • Client vs. simulator logic
    • Build and deploy
    • Simulator load balancing
  • Tutorial project
    • Get the Tutorial Project
    • Start Tutorial
      • 1. Transforms
      • 2. Physics
      • 3. Persistence
      • 4. Animation and Variables
      • 5. AI Navigation
      • 6. Network Commands
      • 7. Network Teams (draft)
  • Game Services
    • Game account
    • Key-value store
    • Matchmaking
  • Developer Portal
    • Overview
    • Dashboard
    • Resource Usage
    • Replicator and Simulator Configuration
    • Enabling Game Services
  • API reference
    • Network SDK
      • CoherenceSync
      • MonoBridge
      • LiveQuery
      • Archetype
      • Sample UI
      • Settings Window
      • Custom Bindings
    • Cloud API
      • API tokens and keys
      • Server discovery
      • Game account
      • Key-value store
      • Matchmaking
    • Replication Server
    • Simulation Server
  • Schema reference
    • Overview
    • Specification
    • Field Settings
    • Archetypes and LOD-ing
  • Resources
    • Downloads
    • SDK Update Guide
    • Video Tutorials
    • Glossary
    • CLI Utilities
    • Helper Scripts
    • Troubleshooting
  • Community
    • Discord
  • Additional information
    • Pricing
    • SLA
    • Unreal Engine support
    • WebGL
    • Peer-to-Peer (P2P)
    • Known Issues
    • Changelog
Powered by GitBook
On this page
  • 1. Create a prefab and move it to Resources
  • 2. Add CoherenceSync to the prefab
  • 3. Select variables to replicate
  • 4. Add an input script
  • 5. Disable Input on replicated object
  • 6. Additional CoherenceSync settings
  • Extras

Was this helpful?

Export as PDF
  1. Get Started

Prefab setup

Last updated 3 years ago

Was this helpful?

In this section, we will learn how to prepare a prefab for network replication.

1. Create a prefab and move it to Resources

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:

1.1 Create a simple Cube in the scene

GameObject -> 3D Object -> Cube

1.2 Convert the Cube into a prefab

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.

2. Add CoherenceSync to the prefab

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.

3. Select variables to replicate

Select which variables you would like to sync across the network. Initially, this will probably be the Transform settings; position, rotation, scale.

Under Synchronization, click Select fields and methods.

In the Bindings dialog, select position, rotation and scale.

Close the Bindings dialog.

4. Add an input script

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move : MonoBehaviour
{
    public float speed = 1f;

    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");
    
        var spf = speed * Time.deltaTime;

        transform.position += transform.forward * (v * spf);
        transform.position += transform.right * (h * spf);
    }
}

Wait for Unity to compile the file, then add it onto the prefab.

5. Disable Input on replicated object

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.

6. Additional CoherenceSync settings

From the CoherenceSync component you can configure settings for Lifetime (Session-based or Persistent, Authority (Not Transferable,Request or Stealing), Adoption settings for when local persistent entities are orphaned, Simulation type (Client Side or Server Side with Client Input), Interpolation Loop (CUpdate, Late Update, Fixed Update, Update and Fixed Update and Late Update and Fixed Update) and Manual Position Update.

You can also control what happens when the prefab is instantiated or destroyed over the network in the Network Instantiation section. Where you can also have the networked object be instantiated as a different prefab altogether.

You can find more information in the SDK Fundamentals. There are also some Events that are triggered at different times.

  • 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).

Extras

Helper scripts and samples can be found here.