fusion | v2 switch to V1  

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.

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:

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 via a ChangeDetector. Add a new ChangeDetector to the script and initialize it in Spawned like this:

private ChangeDetector _changeDetector;

public override void Spawned()
{
    _changeDetector = GetChangeDetector(ChangeDetector.Source.SimulationState);
}

Then at the end of the Update function at the following:

foreach (var change in _changeDetector.DetectChanges(this))
    {
        switch (change)
        {
            case nameof(NetworkedColor):
                MeshRenderer.material.color = NetworkedColor;
                break;
        }
    }

This code iterates over all changes that happened to the Networked Properties since the last call to the ChangeDetector. So in this case since the last Update. If a change in the color value is detected on any client the MeshRenderer is updated.

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

기술 문서 TOP으로 돌아가기