This document is about: FUSION 1
SWITCH TO

This page is a work in progress and could be pending updates.

4 - Network Properties

Overview

This section shows how to synchronize additional data over the network in addition to the player's position using Networked Properties.

Network Properties

Fusion synchronizes the transforms of NetworkObjects when you add a NetworkTransform component to them. Other states such as variables in your scripts are not synchronized over the network. To have state synchronized over the network a [Networked] Property is needed. Networked Properties synchronize their state from the StateAuthority to all other clients.

If a client changes a Networked Property on an object over which it has no StateAuthority the change is not synchronized over the network but instead applied as a local prediction and can be overridden by changes from the StateAuthority in the future. Be careful to only update Networked Properties on the StateAuthority if you want it to update on every client.

A simple example for a Network Property would be a player's color. First create a new script and name it PlayerColor. Add a Networked property and a public field to reference the MeshRender of the object to it.

C#

using Fusion;
using UnityEngine;

public class PlayerColor : NetworkBehaviour
{
    public MeshRenderer MeshRenderer;
    
    [Networked]
    public Color NetworkedColor { get; set; }
}

Note that Networked Properties have to be properties ({get; set;}), regular fields are not supported.

Next in Update add code that allows the StateAuthority to change the color:

C#

void Update()
{
    if (Input.GetKeyDown(KeyCode.E))
    {
        // Changing the material color here directly does not work since this code is only executed on the client pressing the button and not on every client.
        NetworkedColor = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f), 1f);
    }
}

When the button is pressed the MeshRenders color is not changed immediately. Instead, the Networked Property is updated. That way the change in color will be replicated to all clients. What's left is for each client to check for changes to the Networked Property and adjust the mesh color accordingly.

In Fusion this is done with OnChanged callbacks. First create a function to be called whenever the Networked Property changes:

C#

private static void NetworkColorChanged(Changed<PlayerColor> changed)
{
    changed.Behaviour.MeshRenderer.material.color = changed.Behaviour.NetworkedColor;
}

The changed parameter is needed and gives you access to the NetworkBehaviour instance that is being changed to get/set properties. Note that the type is Changed<PlayerColor> which is the type of the NetworkBehaviour and not just the type of the NetworkedColor Networked Property.

Finally, to hook up the callback to the property replace the [Networked] attribute with:

C#

[Networked(OnChanged = nameof(NetworkColorChanged))]
public Color NetworkedColor { get; set; }

Add the PlayerColor component to the player object and link the MeshRender from the interpolation target. Enter play mode and press E. You can check in the edit view that the player is changing colors. At this point you can also create a build to verify that the color is synchronized over the network correctly.

Next Shared Mode Basics 5 - Remote Procedure Calls

Back to top