I'm having some trouble with serializing basic objects ... can anyone help?? I'm building my own Unity server in c# ... I created a basic messaging clas like this ...
[Serializable]
public class BaseMessage
{
public int size;
[NonSerialized]
public Socket Socket;
[NonSerialized]
public List<byte> TransmissionBuffer = new List<byte>();
//public byte[] buffer = new byte[1024];
//Usually you shouldn't but these 2 methods in your class because they don't operate specifically on this object
//and we would get allot of duplicate code if we would put those 2 methods in each class we would like to
//be able to send but to not wind up having to write a couple of utility classes (where these should reside)
// I let them reside here for now.
public byte[] SerializeMe()
{
BinaryFormatter bin = new BinaryFormatter();
MemoryStream mem = new MemoryStream();
bin.Serialize(mem, this);
return mem.GetBuffer();
}
public BaseMessage DeSerializeMe()
{
byte[] dataBuffer = TransmissionBuffer.ToArray();
BinaryFormatter bin = new BinaryFormatter();
MemoryStream mem = new MemoryStream();
mem.Write(dataBuffer, 0, dataBuffer.Length);
mem.Seek(0, 0);
return (BaseMessage)bin.Deserialize(mem);
}
}
[Serializable]
public class Hello : BaseMessage
{
public IPEndPoint IP { get; set; }
public Hello()
{
}
}
this messaging class is compiled into a .dll and avaliable on both sides.
In my unity editor I created a basic connection class that works fine ... I make the connection to the server and send of the hello msg like this ...
Hello hello = new Hello();
hello.IP = serverEndPoint;
byte[] msg = hello.SerializeMe();
Debug.Log(hello.IP);
clientStream.Write(msg, 0, msg.Length);
clientStream.Flush();
The problem is on the server side the hello.IP is null !!! this seems weird to me because the client works fine as a c# project but not with unity ... what am I missing here??