- Home /
Can I see how many bytes are being sent by a particular SyncVar variable?
I've been experimenting with sending my information in two ways.
The first way is to create a struct and put the [SyncVar] tag on it.
The second way is to convert the 'would-be' struct into a string and [SyncVar] on a string. Basically a Vector3 would turn into something like, "1.2343\t5.2634\t9.23456" which I would then rebuild by manually parsing. Realistically there would be more than just a Vector3, but hopefully you get the idea
Really, all I want to know is which way actually sends less information, but I can't find a way to do this. The Unity profiler doesn't have the actual bytes, and NetworkServer.GetStatsIn/Out doesn't do anything. I'm also running it on my localhost and I don't know how to capture that information with programs like Wireshark.
Question: How can I measure the amount of bytes being sent by a SyncVar, OR Would my ghetto string method be smaller than syncing actual Vector3/Quaternion/floats/doubles/etc since it's all on a single variable?
I think if theres anything to be had its in here.
The maximum number of bytes will not change given the amount of memory required for that data type and the max send interval. Also take in to account that a SyncVar registers changes and does not poll. I do believe that it only likes basic data-types and so will likely not work on a Struct.
There is a SyncListStruct but its not an attribute, its a class.
I tried it with a struct and it worked fine. I passed this struct through.
public struct PosRot$$anonymous$$odel {
public Vector3 Pos;
public Quaternion Rot;
public double Timestamp;
public PosRot$$anonymous$$odel(Vector3 Pos, Quaternion Rot, double Timestamp){
this.Pos = Pos;
this.Rot = Rot;
this.Timestamp = Timestamp;
}
}
The class script I used was
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
[NetworkSettings (channel = 0, sendInterval = 0.1f)]
public class NetworkSyncPosRotStruct : NetworkBehaviour {
[SyncVar (hook = "Sync$$anonymous$$essage")]
private PosRot$$anonymous$$odel $$anonymous$$essage = new PosRot$$anonymous$$odel();
void FixedUpdate(){
var message = new PosRot$$anonymous$$odel (this.transform.position, this.transform.rotation, Network.time);
this.Transmit$$anonymous$$essage (message);
}
public virtual bool TransmitCondition(){
return this.isLocalPlayer;
}
[Command]
protected virtual void Cmd_Trasmit$$anonymous$$essageFromClientToServer(PosRot$$anonymous$$odel model){
this.$$anonymous$$essage = model;
}
[ClientCallback]
protected virtual void Transmit$$anonymous$$essage(PosRot$$anonymous$$odel model){
if(this.isLocalPlayer){
this.Cmd_Trasmit$$anonymous$$essageFromClientToServer(model);
}
}
[Client]
protected virtual void Sync$$anonymous$$essage(PosRot$$anonymous$$odel message){
this.$$anonymous$$essage = message;
}
///@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
///@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
///@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
///@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
public float LerpRate = 5f;
// Update is called once per frame
void Update () {
if (!this.isLocalPlayer) {
this.LerpPositionRotation ();
}
}
void LerpPositionRotation(){
this.transform.position = Vector3.Lerp (this.transform.position, this.$$anonymous$$essage.Pos, LerpRate);
this.transform.rotation = Quaternion.Lerp (this.transform.rotation, this.$$anonymous$$essage.Rot, LerpRate);
}
}
I tried NetworkTransport.GetPacketReceivedRate, but I keep getting 50 whether I have 1 script or 2 sending messages so I figure I'm doing it wrong.
[Server]
void Update(){
byte e;
int r = NetworkTransport.GetPacketReceivedRate(0,
1,
out e);
byte e2;
int r2 = NetworkTransport.GetPacketReceivedRate(0,
0,
out e2);
byte e3;
int r3 = NetworkTransport.GetPacketReceivedRate(0,
2,
out e3);
Debug.Log (r + "-" + e + "\n" + r2 + "-" + e2+ "\n" + r3 + "-" + e3);
}
I replaced GetPacketReceivedRate with GetPacketSentRate and got 16-20 bytes per second for each script, regardless of whether I was only sending a single int or whether I was sending a struct with a Vector3/Quaternion/double. So I assume that both functions aren't telling me what's going on.
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
[NetworkSettings (channel = 0, sendInterval = 0.1f)]
public class NetworkNothing : NetworkBehaviour {
[SyncVar (hook = "SyncInt")]
private int $$anonymous$$essage;
void FixedUpdate(){
var message = Random.Range(1,10000);
this.Transmit$$anonymous$$essage (message);
}
public virtual bool TransmitCondition(){
return this.isLocalPlayer;
}
[Command]
protected virtual void Cmd_Trasmit$$anonymous$$essageFromClientToServer(int model){
this.$$anonymous$$essage = model;
}
[ClientCallback]
protected virtual void Transmit$$anonymous$$essage(int model){
if(this.isLocalPlayer){
this.Cmd_Trasmit$$anonymous$$essageFromClientToServer(model);
}
}
[Client]
protected virtual void SyncInt(int message){
this.$$anonymous$$essage = message;
}
}
I don't actually know the answer, but I can make a guess. Floats take up 4 bytes each, and a Vector3 has three floats, for a total of 12 bytes. Strings take up two bytes per character (I believe), which means your example string, with 21 characters, takes up 42 bytes. Unless the Vector3 has much higher overhead, I would expect it to be more efficient.
Answer by dsull · Jul 22, 2015 at 09:28 AM
While I haven't technically found an answer on how to measure the amount of information being sent, I've decided to use a simple approach suggested by Nisovin to just measure the size of objects being synced by their fields and estimate using that.
Quote:
I don't actually know the answer, but I can make a guess. Floats take up 4 bytes each, and a Vector3 has three floats, for a total of 12 bytes. Strings take up two bytes per character (I believe), which means your example string, with 21 characters, takes up 42 bytes. Unless the Vector3 has much higher overhead, I would expect it to be more efficient.
Your answer
Follow this Question
Related Questions
Multiple NetworkBehaviours with the same SyncVar hook not working 1 Answer
Loading spawned prefabs upon connecting to server? 1 Answer
Re-hosting a Match in unity creates a bunch of random errors 1 Answer
Mirror: How to have the player object active before connecting to spawn it. 1 Answer
Hosting my multiplayer game 1 Answer