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 (HasStateAuthority && 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 MeshRender
s 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 via ChangeDetection
. The simplest way to do this in Shared Mode is to use the OnChangedRender
attribute. First add a function that will be executed whenever the NetworkedColor
changes.
C#
void ColorChanged()
{
MeshRenderer.material.color = NetworkedColor;
}
Then adjust the Networked Property with the OnChangedRender
attribute:
C#
[Networked, OnChangedRender(nameof(ColorChanged))]
public Color NetworkedColor { get; set; }
This attribute calls the ColorChanged
function each time a change is detected on the property during each render frame (Unity Update).
Add the PlayerColor
component to the PlayerCharacter
prefab and link the MeshRender
on the player. 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