Network Synchronization
Custom Synchronization Example
using PretiaArCloud.Networking;
using UnityEngine;
// It is required to inherit the NetworkBehaviour
public class TestSyncComponent : NetworkBehaviour
{
// Enable this flag to make use of the new synchronization APIs.
protected override bool NetSyncV2 => true;
// Optionally you can also enable the ClientAuthority flag
// to specify that this component's updates will be sent by each clients.
// By default this value is set to false, and will have Host authority,
// which means that the Host will be the on sending the updates.
// protected override bool ClientAuthority => true;
// Additionally if you set ClientAuthority to true,
// you can choose to have the synchronization updates sent only to the host.
// By default, this value will be set to SynchronizationTarget.All,
// which means that it will be sent to all connected clients.
// protected override SynchronizationTarget SyncTarget => SynchronizationTarget.HostOnly;
// Use the NetworkVariable class to define any variables
// that you want to synchronize
private NetworkVariable<Vector3> _pos;
private NetworkVariable<Quaternion> _rot;
private void Awake()
{
// Initialize the network variables
// It takes the initial value as the input parameter.
_pos = new NetworkVariable<Vector3>(transform.position);
_rot = new NetworkVariable<Quaternion>(transform.rotation);
}
// Override the SyncUpdate function to update the values of the network variables.
// SyncUpdate will be called every network tick, which the frequency can be adjusted
// from the NetworkSettings scriptable object.
// The caller of this function will depend on who has the authority.
// Assign the network variables to the new value you want to update it to.
protected override void SyncUpdate(int tick)
{
_pos.Value = transform.position;
_rot.Value = transform.rotation;
}
// Override the ApplySyncUpdate to apply the synchronization updates
// received from other clients or from the host
protected override void ApplySyncUpdate(int tick)
{
transform.position = _pos.Value;
transform.rotation = _rot.Value;
}
// Override the SerializeNetworkVars function to write the network variables
// to the network stream.
// The content of this function should mostly look like a boilerplate code,
// but it's important because this function is responsible for sending your data
// across the network.
// What you need to do is to call writer.Write for each network variables
// IMPORTANT: the order of write should be the same as the read order defined below.
protected override void SerializeNetworkVars(ref NetworkVariableWriter writer)
{
writer.Write(_pos);
writer.Write(_rot);
}
// This function is the counterpart of SerializeNetworkVars, and is responsible for
// reading your data from the network stream and deserializing it into your network variable.
// Again, the content of this function should look like boilerplate code,
// and all you need to do is to call reader.Read function for each of the network variables.
// IMPORTANT: the order of read should be the same as the write order defined above.
protected override void DeserializeNetworkVars(ref NetworkVariableReader reader)
{
reader.Read(_pos);
reader.Read(_rot);
}
}
Synchronization Components
The SDK provides some helper components that can be used immediately by the developers.
NetworkTransform
This component will synchronizes the Transform
of GameObject
. Developers can choose which fields to be synchronized.
NetworkRigidbody
This component is similar to NetworkTransform
, but it is compatible with the Rigidbody
component. It is also missing the scale field, but if you need to synchronize scale please use the NetworkTransform
. Developers can choose which fields to be synchronized.
NetworkAnimator
This component is used to synchronize the parameters value of an Animator. Currently it only supports Float
, Int
, and Bool
parameters. Trigger
parameter is not supported yet. Developers can choose which parameters to be synchronized.
NetworkCameraProxy
This component is instantiated if you use the NetworkCamera
component. Developers will rarely have to use this component directly. This component is responsible for synchronizing each camera proxies of all connected players.