メインコンテンツまでスキップ

ネットワーク同期

カスタム Synchronization のサンプル

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

SDK は、開発者の方々がすぐに使える便利なコンポーネントを提供しています。

NetworkTransform

このコンポーネントはGameObjectTransform を同期します。開発者は、どのフィールドを同期するかを選択できます。

NetworkRigidbody

このコンポーネントは NetworkTransform に似ていますが、Rigidbody と互換性のあるコンポーネントです。スケールに関するフィールドが欠けていますが、もしスケールの同期が必要な場合はNetworkTransform を利用してください。開発者はどのフィールドを同期するかを選択できます。

NetworkAnimator

Animator のパラメータを同期するために利用します。現在のところFloatIntBool の 3 つのパラメータのみをサポートしています。Trigger は未サポートです。開発者はどのフィールドを同期するかを選択できます。

NetworkCameraProxy

このコンポーネントは、NetworkCamera を利用する際にインスタンス化されます。開発者がこれを直接利用することは稀です。このコンポーネントは、カメラプロキシをすべての接続されたユーザーへ同期する役割を担います。