N
N
nonameException2021-08-19 15:28:27
C++ / C#
nonameException, 2021-08-19 15:28:27

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;
    }
}

The class is serialized and sent to the server where it must be deserialized (This class is the same in the console application and in Unity). But when deserializing on the server, it throws a System.Runtime.Serialization.SerializationException:
Could not find assembly 'Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.

Serialization and deserialization functions:
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;
}


Sending data:
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));


Data reception:
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());
}


Is this due to the fact that the game is on Unity and the server is on pure C #? If so, why do I not use the types of variables / functions from the UnityEngine namespace in classes

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question