Version: 2021.1

MonoBehaviour.OnSerializeNetworkView(BitStream,NetworkMessageInfo)

切换到手册

描述

用于在网络视图监视的脚本中自定义变量同步。

自动确定是否应该发送或接收被序列化的变量, 请参阅下面的示例来获得更好的理解。这取决于谁拥有该对象, 即所有者发送,其他各方接收。

// This objects health information

using UnityEngine; using System.Collections;

public class ExampleClass : MonoBehaviour { public int currentHealth = 0;

void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) { if (stream.isWriting) { int healthC = currentHealth; stream.Serialize(ref healthC); } else { int healthZ = 0; stream.Serialize(ref healthZ); currentHealth = healthZ; } } }