Good time of day.
For some time I was distracted from Unity3d, in order to write my own Network Suite.
Just a couple of days ago, faced with a problem that I can’t solve
It consists of the following:
Conventionally, there are three scripts.
Vector2Event.cs
using System;
using System.IO;
using UnityEngine;
namespace TestServerKenderEvents
{
[Serializable]
public class Vector2Event : KenderEventSet.Event
{
public Vector2Event(Vector2 vector2)
{
this.x = vector2.x;
this.y = vector2.y;
this.Vector2 = vector2;
}
public Vector2Event(BinaryReader reader)
{
this.x = reader.ReadSingle();
this.y = reader.ReadSingle();
this.Vector2 = new Vector2(this.x, this.y);
}
public Vector2 Vector2
{
get;
private set;
}
public float x
{
get;
private set;
}
public float y
{
get;
private set;
}
public override void Serialize(BinaryWriter writer)
{
writer.Write(this.x);
writer.Write(this.y);
}
}
}
JoinRequest.cs
using System;
using System.IO;
using System.Collections;
using UnityEngine;
namespace TestServerKenderEvents
{
[Serializable]
public class JoinRequest : KenderEventSet.Event
{
public JoinRequest(System.Drawing.Color color, Vector2 vector2, float radiusOfInterest)
{
this.Color = color;
this.Vector2 = vector2;
this.RadiusOfInterest = radiusOfInterest;
}
public JoinRequest(BinaryReader reader)
{
this.Color = new ColorEvent(reader).Color;
this.Vector2 = new Vector2Event(reader).Vector2;
this.RadiusOfInterest = reader.ReadSingle();
}
public System.Drawing.Color Color
{
get;
private set;
}
public Vector2 Vector2
{
get;
private set;
}
public float RadiusOfInterest
{
get;
private set;
}
public override void Serialize(BinaryWriter writer)
{
new ColorEvent(this.Color).Serialize(writer);
new Vector2Event(this.Vector2).Serialize(writer);
writer.Write(this.RadiusOfInterest);
}
}
}
KenderEventSet.cs
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace KenderEventSet
{
public static class EventSet
{
public static object Deserialize(byte[] message)
{
BinaryFormatter bin = new BinaryFormatter();
MemoryStream mem = new MemoryStream();
mem.Write(message, 0, message.Length);
mem.Seek(0, 0);
return bin.Deserialize(mem);
}
public static byte[] Serialize(object Object)
{
try
{
BinaryFormatter bin = new BinaryFormatter();
MemoryStream mem = new MemoryStream();
bin.Serialize(mem, Object); // KenderEventSet.cs:string 24
return mem.GetBuffer();
}
catch (SerializationException)
{
return null;
}
}
}
[Serializable]
public abstract class Event
{
public abstract void Serialize(BinaryWriter writer);
}
}
… And when I call the following code:
...
playerEventInterlocutor.SendEvent( // Form1.cs:string 409
TestServerKenderEventSet.Serialize(
new JoinRequest(
this.player1Pen.Color,
new Vector2(
this.player1XPosition,
this.player1YPosition
),
this.areaOfInterest
)
),
false
);
...
…, I get the funny exception:
Simply put, I think swearing on the non-serializable Vector2 unfounded, because I write only its coordinates.
Write a surrogate for every such case is no desire, for it may eventually make the library’s users crazy
In general, I would be grateful to anyone who can help! Thanks in advance!