Answer the question
In order to leave comments, you need to log in
Linking C# console application and Unity throws an error during deserialization. What is it connected with?
In the Unity project, I have a couple of classes that are responsible for game events (they are all inherited from the same Event class for ease). For example, the position synchronization event:
using System;
namespace ServerSystem.Events
{
[Serializable]
public class TransformSyncEvent : Event
{
public MyVector3 position;
public MyVector3 rotation;
public string GUID;
}
}
Could not find assembly 'Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
static private byte[] ObjectToByteArray(Object obj)
{
if (obj == null) return null;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
static private Events.Event ByteArrayToObject(byte[] arrBytes)
{
MemoryStream memStream = new MemoryStream();
BinaryFormatter binForm = new BinaryFormatter();
memStream.Write(arrBytes, 0, arrBytes.Length);
memStream.Seek(0, SeekOrigin.Begin);
Events.Event obj = (Events.Event)binForm.Deserialize(memStream);
return obj;
}
Events.TransformSyncEvent Sync = new Events.TransformSyncEvent();
Sync.position = Vector3UnityToMyVector(player.transform.position);
Sync.rotation = Vector3UnityToMyVector(player.transform.eulerAngles);
Sync.GUID = System.Guid.NewGuid().ToString();
server.Send(ObjectToByteArray(Sync));
Socket handler = listenSocket.Accept();
StringBuilder builder = new StringBuilder();
using (MemoryStream memStream = new MemoryStream())
{
byte[] buffer = new byte[bufferSize];
do
{
int received = handler.Receive(buffer);
memStream.Write(buffer, 0, received);
}
while (handler.Available > 0);
Events.Event e = ByteArrayToObject(memStream.ToArray());
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question