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 but what about other values such as health, stamina or simply an object's color?
Fusion can synchronize properties from your scripts for you. Simply use a C# auto-implemented property for the value, add Fusion's [Networked]
attribute and it becomes part of the networked state as a "Networked Property".
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 auto-implemented 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 it does not change the MeshRenderer.material.color
directly. Instead, the Networked Property is updated which will replicate the value to all clients.
You could extend the Update() method to set the material color every frame and all clients will change the object's color eventually. However, this is relatively costly and there is a more elegant solution: Change Detection.
OnChangeRender - Change Detection
What's left is for each client to check for changes of the Networked Property and adjust the mesh color accordingly.
In Fusion this is done with an OnChangedRender
attribute on the networked property. It is used to hook up a method to call on change.
For the tutorial, first add a function that will be executed whenever the NetworkedColor
changes. This is the best place to update the material.
C#
void ColorChanged()
{
MeshRenderer.material.color = NetworkedColor;
}
Then add the OnChangedRender
attribute to the NetworkedColor
:
C#
[Networked, OnChangedRender(nameof(ColorChanged))]
public Color NetworkedColor { get; set; }
With this attribute, Fusion 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