我正在编写一个自定义网络消息类,该类继承自 MessageBase ,并覆盖其 Serialize 和 Deserialize 函数。
>我希望能够在此消息中发送播放器的IP地址。为此,我将外部类(均为字符串类型)的 SpawnMessage.Address 属性设置为 client.connection.address 。
但是,当我序列化->发送->反序列化此消息时,在此特定示例中,当我期望“ localServer”时,我正在从NetworkReader获取IP地址的废话字符串。
我要去哪里错了?还有潜在的更好方法吗?
public class LineObjectMessage : MessageBase
{
public LineSpawn SpawnMessage = new LineSpawn();
public override void Serialize(NetworkWriter writer)
{
writer.StartMessage(NetworkMessageTypes.LineObjectSpawnMessage);
writer.Write(SpawnMessage.IpAddress);
writer.Write(SpawnMessage.NetId);
writer.Write(SpawnMessage.Thickness);
writer.FinishMessage();
}
public override void Deserialize(NetworkReader reader)
{
SpawnMessage = new LineSpawn();
SpawnMessage.IpAddress = reader.ReadString();
SpawnMessage.NetId = reader.ReadNetworkId();
SpawnMessage.Thickness = reader.ReadDouble();
}
[Serializable]
public partial class LineSpawn
{
public string IpAddress;
public double Thickness;
public NetworkInstanceId NetId;
}
}