Any way to serialize NetworkViewID or a workaround to not need them at all?

I’m creating a wrapper for network traffic, in an authoritative server setup, to send compact byte arrays exclusively. I’ve got my custom class serializable using protobuf-net… except for NetworkViewIDs, which I was planning on using to associate particular GameObjects across clients. I don’t need all the features of NetworkViewID – I think all I need is the ability to associate GameObjects across different clients (so when the server tells them gameObjectA moved, they all know which object to move), so here’s what I was trying.

The problem with NetworkViewID is that I can’t seem to break it down into the few primitives I need for serialization and then reconstruct them into a new NetworkViewID on the receiving side. The only way I’m aware of creating a new NetworkViewID is to use Network.AllocateViewID but I can’t create a NetworkViewID with the ID of my choosing or change the ID after creating one with Network.AllocateViewID so I simply can’t pass a NetworkViewID to a client.

So I went looking to see if I could serialize NetworkViewID… I found BitStream.Serialize and got optimistic. But it seems that BitStream.Serialize is only useful inside OnSerializeNetworkView. I don’t think it’s possible to use it to convert NetworkViewID into a bitstream or byte array and vice versa whenever I want.

So it’s looking like I’ll have to make an alternative to NetworkViewID. Are there any resources I can read about optimal ways to allocate the view ids? Since I won’t be relying on Unity’s NetworkViewID, I figure I can just have the server allocate them all and if I need to specify an owner the server can do that too (with Unity’s NetworkViewID, the intended owner has to allocate the viewID and then tell the server). If I simplify it like this, do I just have the server increment an int for viewID allocation? Or is this an oversimplification? Think my brain is a little fried right now. Would it be worth it to just write my own socket at this point?

I know this may be a late reply, but I may have found a solution to this. I used C# reflection to access the private fields of the struct. This is obviously going to be slower, but for load and connect time uses, shouldn’t be too bad. Also, you can create a NetworkViewID with NetworkViewID.unassigned, or just a default constructer, since it is a struct.

//--------------------------------------------------------------
public override void Serialize (ref NetworkViewID viewID)
{
object obj = viewID;
SerializeFieldInt(“a”, obj);
SerializeFieldInt(“b”, obj);
SerializeFieldInt(“c”, obj);
viewID = (NetworkViewID)obj;
}

//--------------------------------------------------------------
void SerializeFieldInt (string name, object obj)
{
int i = 0;
Serialize(ref i);

FieldInfo field = obj.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic);
field.SetValue(obj, i);
}

I ended up just writing my own alternative

Hopefully someone else finds that useful though :slight_smile: