It’s me again
For my project I need an ongoing unreliable data stream 20 times a second.
Since I’m using the HLAPI, I’d like to stay on this level as far as possible.
My idea was to write custom OnSerialize and OnDeserialze functions for a NetworkBehaviour which is collecting data from various other Behaviours and just de/serializing it all the time.
However it didn’t seem to be triggered, so I added a SetDirtyBit hack to the update function.
Now data is being transferred regularly, but the way I do this feels like dirty abuse.
//reduced version of implementation
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System;
[NetworkSettings(channel=1,sendInterval=0.05f)]
public class NetStreamer : NetworkBehaviour
{
private bool initialized; //inhibit data transfer
void Update()
{
if (isServer)
{
SetDirtyBit(1); //force serialization
}
}
public override bool OnSerialize(NetworkWriter writer, bool initialState)
{
if (initialized)
{
//server side
//serialize all kind of data here
}
return true;
}
public override void OnDeserialize(NetworkReader reader, bool initialState)
{
if (initialized)
{
//client side
//deserialize all kind of data here
}
}
}
Again, my usual question:
How is this supposed to be done in a proper way?
Thanks for any hint.