public override void OnSerializeDelta(NetworkWriter writer) { // write all the queued up changes writer.WriteUInt((uint)changes.Count); for (int i = 0; i < changes.Count; i++) { Change change = changes[i]; writer.WriteByte((byte)change.operation); switch (change.operation) { case Operation.OP_ADD: writer.Write(change.item); break; case Operation.OP_CLEAR: break; case Operation.OP_REMOVEAT: writer.WriteUInt((uint)change.index); break; case Operation.OP_INSERT: case Operation.OP_SET: writer.WriteUInt((uint)change.index); writer.Write(change.item); break; } } }
public static void WriteNetworkIdentity(this NetworkWriter writer, NetworkIdentity value) { if (value == null) { writer.WriteUInt(0); return; } writer.WriteUInt(value.netId); }
public static void WriteNetworkBehaviour(this NetworkWriter writer, NetworkBehaviour value) { if (value == null) { writer.WriteUInt(0); return; } writer.WriteUInt(value.netId); writer.WriteByte((byte)value.ComponentIndex); }
// for byte arrays with dynamic size, where the reader doesn't know how many will come // (like an inventory with different items etc.) public static void WriteBytesAndSize(this NetworkWriter writer, byte[] buffer, int offset, int count) { // null is supported because [SyncVar]s might be structs with null byte[] arrays // write 0 for null array, increment normal size by 1 to save bandwidth // (using size=-1 for null would limit max size to 32kb instead of 64kb) if (buffer == null) { writer.WriteUInt(0u); return; } writer.WriteUInt(checked ((uint)count) + 1u); writer.WriteBytes(buffer, offset, count); }
public override void OnSerializeAll(NetworkWriter writer) { // if init, write the full list content writer.WriteUInt((uint)objects.Count); foreach (T obj in objects) { writer.Write(obj); } // all changes have been applied already // thus the client will need to skip all the pending changes // or they would be applied again. // So we write how many changes are pending writer.WriteUInt((uint)changes.Count); }
public void OnSerializeAll(NetworkWriter writer) { // if init, write the full list content writer.WriteUInt((uint)objects.Count); foreach (KeyValuePair <TKey, TValue> syncItem in objects) { writer.Write(syncItem.Key); writer.Write(syncItem.Value); } // all changes have been applied already // thus the client will need to skip all the pending changes // or they would be applied again. // So we write how many changes are pending writer.WriteUInt((uint)changes.Count); }
public static void WriteUIntNullable(this NetworkWriter writer, uint?value) { writer.WriteBool(value.HasValue); if (value.HasValue) { writer.WriteUInt(value.Value); } }
public static void WriteFloat(this NetworkWriter writer, float value) { UIntFloat converter = new UIntFloat { floatValue = value }; writer.WriteUInt(converter.intValue); }
public static void WriteGameObject(this NetworkWriter writer, GameObject value) { if (value == null) { writer.WriteUInt(0); return; } NetworkIdentity identity = value.GetComponent <NetworkIdentity>(); if (identity != null) { writer.WriteUInt(identity.netId); } else { Debug.LogWarning("NetworkWriter " + value + " has no NetworkIdentity"); writer.WriteUInt(0); } }
public static void WriteTransform(this NetworkWriter writer, Transform value) { if (value == null) { writer.WriteUInt(0); return; } NetworkIdentity identity = value.GetComponent <NetworkIdentity>(); if (identity != null) { writer.WriteUInt(identity.netId); } else { Debug.LogWarning($"NetworkWriter {value} has no NetworkIdentity"); writer.WriteUInt(0); } }
public static void WriteNetworkIdentity(this NetworkWriter writer, NetworkIdentity value) { if (value == null) { writer.WriteUInt(0); return; } // users might try to use unspawned / prefab GameObjects in // rpcs/cmds/syncvars/messages. they would be null on the other // end, and it might not be obvious why. let's make it obvious. // https://github.com/vis2k/Mirror/issues/2060 // // => warning (instead of exception) because we also use a warning // if a GameObject doesn't have a NetworkIdentity component etc. if (value.netId == 0) { Debug.LogWarning($"Attempted to serialize unspawned GameObject: {value.name}. Prefabs and unspawned GameObjects would always be null on the other side. Please spawn it before using it in [SyncVar]s/Rpcs/Cmds/NetworkMessages etc."); } writer.WriteUInt(value.netId); }
public static void WriteGameObject(this NetworkWriter writer, GameObject value) { if (value == null) { writer.WriteUInt(0); return; } // warn if the GameObject doesn't have a NetworkIdentity, NetworkIdentity identity = value.GetComponent <NetworkIdentity>(); if (identity == null) { Debug.LogWarning($"NetworkWriter {value} has no NetworkIdentity"); } // serialize the correct amount of data in any case to make sure // that the other end can read the expected amount of data too. writer.WriteNetworkIdentity(identity); }
// serialization is needed by OnSerialize and by manual sending from authority // public only for tests public static void SerializeIntoWriter(NetworkWriter writer, Vector3 position, Quaternion rotation, Vector3 scale, bool compressRotation, bool syncScale) { // serialize position, rotation, scale // => compress rotation from 4*4=16 to 4 bytes // => less bandwidth = better CCU tests / scale writer.WriteVector3(position); if (compressRotation) { // smalles three compression for 3D writer.WriteUInt(Compression.CompressQuaternion(rotation)); } else { // uncompressed for 2D writer.WriteQuaternion(rotation); } if (syncScale) { writer.WriteVector3(scale); } }
public static void WriteInt(this NetworkWriter writer, int value) => writer.WriteUInt((uint)value);
public static void WriteUInt32(this NetworkWriter writer, uint value) => writer.WriteUInt(value);