- Home /
Serialize unity classes
To send through RPC my custom classes I'm converting them into byte array then I deserialize it in the other side but inside my custom class I'm using a NetworkPlayer variable.
Is there a way to convert a NetworkPlayer variable into a byte array ? I tried getting the original NetworkPlayer.cs file and insert the [System.Serializable] thing but it can't acess others unity built-in classes due to their protection level.
Thanks in advance.
I'm not familiar, with the NetworkPlayer class, but virtually every Unity-specific class can be serialized by finding a workaround for it; For example, a Vector3 can't be serialized, but a custom class instance that hold floats for the xyz values of position, rotation and scale can.
Do you really need to serialize whole classes? Synchronizing certain variables should suffice in most cases and would be easier on the network.
Is there a way to not serialize a specific variable of a custom class?
See my comment over here. A NetworkPlayer is just a struct with a single "int field". As i said in the comment i haven't tested if that "index field" is unique across all clients.
If you use the latest Unity version, keep in $$anonymous$$d that Unity now (5.1+) has a new network system which is much more flexible.
Is there a way to not serialize a specific variable of a custom class?
You should be able to write a ISerializationSurrogate that serializes your custom class instance without the field in question, but I only ever used ISS with BinaryFormatters so I dont know if it works for what you are trying to do.