diff --git a/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/TestNetworkedGameObject.prefab b/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/TestNetworkedGameObject.prefab index 096eb0d..3be87ea 100644 --- a/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/TestNetworkedGameObject.prefab +++ b/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/TestNetworkedGameObject.prefab @@ -161,6 +161,7 @@ MonoBehaviour: m_EditorClassIdentifier: networkObject: {fileID: 3951900052977689805} serializationRateHz: 60 + hybridOnChangeCompression: 1 useLocalTransform: 0 teleportDistance: 0 teleportAngle: 0 diff --git a/TestVelGameServer/Packages/VelNetUnity/Runtime/Util/BinaryWriterExtensions.cs b/TestVelGameServer/Packages/VelNetUnity/Runtime/Util/BinaryWriterExtensions.cs index 2b39a98..a605837 100644 --- a/TestVelGameServer/Packages/VelNetUnity/Runtime/Util/BinaryWriterExtensions.cs +++ b/TestVelGameServer/Packages/VelNetUnity/Runtime/Util/BinaryWriterExtensions.cs @@ -65,6 +65,16 @@ namespace VelNet #endregion + public static bool SameAs(this byte[] bytes, byte[] otherBytes) + { + if (bytes.Length != otherBytes.Length) + { + return false; + } + + return !bytes.Where((t, i) => t != otherBytes[i]).Any(); + } + /// /// Compresses the list of bools into bytes using a bitmask /// diff --git a/TestVelGameServer/Packages/VelNetUnity/Runtime/Util/NetworkSerializedObject.cs b/TestVelGameServer/Packages/VelNetUnity/Runtime/Util/NetworkSerializedObject.cs index 0569eed..f6fc434 100644 --- a/TestVelGameServer/Packages/VelNetUnity/Runtime/Util/NetworkSerializedObject.cs +++ b/TestVelGameServer/Packages/VelNetUnity/Runtime/Util/NetworkSerializedObject.cs @@ -1,6 +1,6 @@ -using System.Collections; +using System; +using System.Collections; using UnityEngine; -using UnityEngine.Serialization; namespace VelNet { @@ -9,6 +9,15 @@ namespace VelNet [Tooltip("Send rate of this object. This caps out at the framerate of the game.")] public float serializationRateHz = 30; + /// + /// If the data hasn't changed, only sends updates across the network at 1Hz + /// + public bool hybridOnChangeCompression = true; + + private byte[] lastSentBytes; + private double lastSendTime; + private const double slowSendInterval = 2; + protected virtual void Awake() { StartCoroutine(SendMessageUpdate()); @@ -18,9 +27,30 @@ namespace VelNet { while (true) { - if (IsMine) + try { - SendBytes(SendState()); + if (IsMine) + { + byte[] newBytes = SendState(); + if (hybridOnChangeCompression) + { + if (Time.timeAsDouble - lastSendTime > slowSendInterval || !lastSentBytes.SameAs(newBytes)) + { + SendBytes(newBytes); + } + } + else + { + SendBytes(newBytes); + } + + lastSendTime = Time.timeAsDouble; + lastSentBytes = newBytes; + } + } + catch (Exception e) + { + Debug.LogError(e); } yield return new WaitForSeconds(1f / serializationRateHz); diff --git a/TestVelGameServer/Packages/VelNetUnity/Runtime/Util/NetworkSerializedObjectStream.cs b/TestVelGameServer/Packages/VelNetUnity/Runtime/Util/NetworkSerializedObjectStream.cs index 161791f..d3ac592 100644 --- a/TestVelGameServer/Packages/VelNetUnity/Runtime/Util/NetworkSerializedObjectStream.cs +++ b/TestVelGameServer/Packages/VelNetUnity/Runtime/Util/NetworkSerializedObjectStream.cs @@ -2,7 +2,6 @@ using System.Collections; using System.IO; using UnityEngine; -using UnityEngine.Serialization; namespace VelNet { @@ -11,6 +10,16 @@ namespace VelNet [Tooltip("Send rate of this object. This caps out at the framerate of the game.")] public float serializationRateHz = 30; + /// + /// If the data hasn't changed, only sends updates across the network at 1Hz + /// + public bool hybridOnChangeCompression = true; + + private byte[] lastSentBytes; + private double lastSendTime; + private const double slowSendInterval = 2; + + protected virtual void Awake() { StartCoroutine(SendMessageUpdate()); @@ -27,7 +36,22 @@ namespace VelNet using MemoryStream mem = new MemoryStream(); using BinaryWriter writer = new BinaryWriter(mem); SendState(writer); - SendBytes(mem.ToArray()); + + byte[] newBytes = mem.ToArray(); + if (hybridOnChangeCompression) + { + if (Time.timeAsDouble - lastSendTime > slowSendInterval || !lastSentBytes.SameAs(newBytes)) + { + SendBytes(newBytes); + } + } + else + { + SendBytes(newBytes); + } + + lastSendTime = Time.timeAsDouble; + lastSentBytes = newBytes; } } catch (Exception e) @@ -44,7 +68,7 @@ namespace VelNet { using MemoryStream mem = new MemoryStream(message); using BinaryReader reader = new BinaryReader(mem); - + ReceiveState(reader); }