mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
common serialize funcs for delta
This commit is contained in:
parent
e4375e4f62
commit
c3d383f206
@ -204,9 +204,14 @@ void CmdClientToServerSync(Vector3? position, Quaternion? rotation, Vector3? sca
|
|||||||
if (syncDirection == SyncDirection.ClientToServer &&
|
if (syncDirection == SyncDirection.ClientToServer &&
|
||||||
connectionToClient != null && // CUSTOM CHANGE: for the drop thing..
|
connectionToClient != null && // CUSTOM CHANGE: for the drop thing..
|
||||||
!disableSendingThisToClients) // CUSTOM CHANGE: see comment at definition
|
!disableSendingThisToClients) // CUSTOM CHANGE: see comment at definition
|
||||||
|
{
|
||||||
|
|
||||||
|
using (NetworkWriterPooled writer = NetworkWriterPool.Get())
|
||||||
{
|
{
|
||||||
Debug.LogWarning($"[{name}] CmdClientToServerSync: TODO which baseline to pass in Rpc?");
|
Debug.LogWarning($"[{name}] CmdClientToServerSync: TODO which baseline to pass in Rpc?");
|
||||||
RpcServerToClientDeltaSync(0xFF, position, rotation, scale);
|
SerializeServerDelta(writer, 0xFF, position, rotation, scale);
|
||||||
|
RpcServerToClientDeltaSync(writer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -302,6 +307,36 @@ void DeserializeServerBaseline(NetworkReader reader)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SerializeServerDelta(NetworkWriter writer, byte baselineTick, Vector3? position, Quaternion? rotation, Vector3? scale)
|
||||||
|
{
|
||||||
|
writer.WriteByte(baselineTick);
|
||||||
|
|
||||||
|
if (syncPosition) writer.WriteVector3(position.Value);
|
||||||
|
if (syncRotation) writer.WriteQuaternion(rotation.Value);
|
||||||
|
if (syncScale) writer.WriteVector3(scale.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeserializeServerDelta(NetworkReader reader, out byte baselineTick, out Vector3? position, out Quaternion? rotation, out Vector3? scale)
|
||||||
|
{
|
||||||
|
baselineTick = reader.ReadByte();
|
||||||
|
position = null;
|
||||||
|
rotation = null;
|
||||||
|
scale = null;
|
||||||
|
|
||||||
|
if (syncPosition)
|
||||||
|
{
|
||||||
|
position = reader.ReadVector3();
|
||||||
|
}
|
||||||
|
if (syncRotation)
|
||||||
|
{
|
||||||
|
rotation = reader.ReadQuaternion();
|
||||||
|
}
|
||||||
|
if (syncScale)
|
||||||
|
{
|
||||||
|
scale = reader.ReadVector3();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// rpc /////////////////////////////////////////////////////////////////
|
// rpc /////////////////////////////////////////////////////////////////
|
||||||
[ClientRpc(channel = Channels.Reliable)]
|
[ClientRpc(channel = Channels.Reliable)]
|
||||||
void RpcServerToClientBaselineSync(ArraySegment<byte> message)
|
void RpcServerToClientBaselineSync(ArraySegment<byte> message)
|
||||||
@ -315,8 +350,14 @@ void RpcServerToClientBaselineSync(ArraySegment<byte> message)
|
|||||||
|
|
||||||
// only unreliable. see comment above of this file.
|
// only unreliable. see comment above of this file.
|
||||||
[ClientRpc(channel = Channels.Unreliable)]
|
[ClientRpc(channel = Channels.Unreliable)]
|
||||||
void RpcServerToClientDeltaSync(byte baselineTick, Vector3? position, Quaternion? rotation, Vector3? scale) =>
|
void RpcServerToClientDeltaSync(ArraySegment<byte> message)
|
||||||
|
{
|
||||||
|
using (NetworkReaderPooled reader = NetworkReaderPool.Get(message))
|
||||||
|
{
|
||||||
|
DeserializeServerDelta(reader, out byte baselineTick, out Vector3? position, out Quaternion? rotation, out Vector3? scale);
|
||||||
OnServerToClientDeltaSync(baselineTick, position, rotation, scale);
|
OnServerToClientDeltaSync(baselineTick, position, rotation, scale);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// server broadcasts sync message to all clients
|
// server broadcasts sync message to all clients
|
||||||
protected virtual void OnServerToClientDeltaSync(byte baselineTick, Vector3? position, Quaternion? rotation, Vector3? scale)
|
protected virtual void OnServerToClientDeltaSync(byte baselineTick, Vector3? position, Quaternion? rotation, Vector3? scale)
|
||||||
@ -444,15 +485,19 @@ void UpdateServerDelta()
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if onlySyncOnChange_BANDWIDTH_SAVING
|
#if onlySyncOnChange_BANDWIDTH_SAVING
|
||||||
RpcServerToClientDeltaSync(
|
using (NetworkWriterPooled writer = NetworkWriterPool.Get())
|
||||||
|
{
|
||||||
|
SerializeServerDelta(
|
||||||
|
writer,
|
||||||
// include the last reliable baseline tick#.
|
// include the last reliable baseline tick#.
|
||||||
// the unreliable delta is meant to go on top of it that, and no older one.
|
// the unreliable delta is meant to go on top of it that, and no older one.
|
||||||
lastSerializedBaselineTick,
|
lastSerializedBaselineTick,
|
||||||
// only sync what the user wants to sync
|
syncPosition ? snapshot.position : default(Vector3?),
|
||||||
syncPosition && positionChanged ? snapshot.position : default(Vector3?),
|
syncRotation ? snapshot.rotation : default(Quaternion?),
|
||||||
syncRotation && rotationChanged ? snapshot.rotation : default(Quaternion?),
|
syncScale ? snapshot.scale : default(Vector3?)
|
||||||
syncScale && scaleChanged ? snapshot.scale : default(Vector3?)
|
|
||||||
);
|
);
|
||||||
|
RpcServerToClientDeltaSync(writer);
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
RpcServerToClientSync(
|
RpcServerToClientSync(
|
||||||
// only sync what the user wants to sync
|
// only sync what the user wants to sync
|
||||||
|
Loading…
Reference in New Issue
Block a user