Home > Net >  Unity Networking Serialize Array of Vector3 and Quaternion
Unity Networking Serialize Array of Vector3 and Quaternion

Time:02-10

i'm trying to serialize this structs for sending it via Unity NetworkList. Probably i'm doing the wrong serialization but idk what, unity documentation: https://docs-multiplayer.unity3d.com/docs/advanced-topics/serialization/inetworkserializable

public struct PlayerPositionData : INetworkSerializable, IEquatable<PlayerPositionData>
{
    public int numeroAncora;
    public Vector3[] posizioni;
    public Quaternion[] rotazioni;
 
    public PlayerPositionData(int numeroAnc, Vector3[] posiz, Quaternion[] rotaz)
    {
        numeroAncora = numeroAnc;
        posizioni = posiz;
        rotazioni = rotaz;
    }
 
 
    public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    {
        serializer.SerializeValue(ref numeroAncora);
 
        //SERIALIZZO ARRAY POSIZIONI
        int length = 0;
        if (!serializer.IsReader)
        {
            length = posizioni.Length;
        }
 
        serializer.SerializeValue(ref length);
 
        // Array
        if (serializer.IsReader)
        {
            posizioni = new Vector3[length];
        }
 
        for (int n = 0; n < length;   n)
        {
            serializer.SerializeValue(ref posizioni[n]);
        }
 
 
     
        //SERIALIZZO ARRAY ROTAZIONI
        if (!serializer.IsReader)
        {
            length = rotazioni.Length;
        }
 
        serializer.SerializeValue(ref length);
 
        // Array
        if (serializer.IsReader)
        {
            rotazioni = new Quaternion[length];
        }
 
        for (int n = 0; n < length;   n)
        {
            serializer.SerializeValue(ref rotazioni[n]);
        }
    }
 
    public bool Equals(PlayerPositionData other)
    {
        return posizioni.Length == other.posizioni.Length && numeroAncora == other.numeroAncora;
    }
}

and this:

public struct PlayerData : INetworkSerializable, IEquatable<PlayerData>
{
    public ulong idRete;
    public int numeroPlayer;
    public PlayerPositionData posizioni;
 
    public PlayerData(ulong idRet, int payerN, PlayerPositionData posiz)
    {
        idRete = idRet;
        numeroPlayer = payerN;
        posizioni = posiz;
    }
 
 
    public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    {
        serializer.SerializeValue(ref idRete);
        serializer.SerializeValue(ref numeroPlayer);
 
        posizioni.NetworkSerialize(serializer);
    }
 
    public bool Equals(PlayerData other)
    {
        return idRete == other.idRete;
    }
}

in this way:

private NetworkList<PlayerData> datiPlayers = new NetworkList<PlayerData>();

but i recive this error:

error CS8377: The type 'PlayerData' must be a non-nullable value type, along with all fields at any level of nesting, in order to use it as parameter 'T' in the generic type or method 'NetworkList<T>'

why? the PlayerData struct work without nested PlayerPositionData...

CodePudding user response:

  •  Tags:  
  • Related