I have posted a similar question to this one, but I had to do some changes to the code so I ended up creating a new question. (Serialize an array of custom objects - Unity Answers)
I have a Team class that (now) has an LauncherPlayer array that stores info about the players. Serialization\deserialziation methods for this class I know they are working fine because I already tested them.
The problem is that in my Team class I’am serializing that array, with no problems (I guess, at least I get no errors), but upon deserialization I get an object of type string instead of LauncherPlayer type.
public class Team
{
private int teamID;
private LauncherPlayer[] players;
private int playersJoined = 0;
}
public static byte[] serializeTeam(object o)
{
Team team = (Team)o;
var playerBytes = ExitGames.Client.Photon.Protocol.Serialize(team.players);
//Lenght = playerBytes.Lenght + (4 * 3) (4 bytes in size, 3 ints)
byte[] bytes = new byte[playerBytes.Length + 12];
int index = 0;
ExitGames.Client.Photon.Protocol.Serialize(team.teamID, bytes, ref index);
//We need to store the lenght for deserialization
ExitGames.Client.Photon.Protocol.Serialize(playerBytes.Length, bytes, ref index);
System.Array.Copy(playerBytes, 0, bytes, index, playerBytes.Length);
index += playerBytes.Length;
ExitGames.Client.Photon.Protocol.Serialize(team.playersJoined, bytes, ref index);
return bytes;
}
public static object deserializeTeam(byte[] bytes)
{
Team team = new Team();
int index = 0;
int playerBytesLength;
ExitGames.Client.Photon.Protocol.Deserialize(out team.teamID, bytes, ref index);
ExitGames.Client.Photon.Protocol.Deserialize(out playerBytesLength, bytes, ref index);
var playerBytes = new byte[playerBytesLength];
System.Array.Copy(bytes, index, playerBytes, 0, playerBytesLength);
index += playerBytes.Length;
Debug.Log(ExitGames.Client.Photon.Protocol.Deserialize(playerBytes).GetType());
Debug.Log((string)ExitGames.Client.Photon.Protocol.Deserialize(playerBytes)+",");
//team.players = (LauncherPlayer[]) ExitGames.Client.Photon.Protocol.Deserialize(playerBytes);
ExitGames.Client.Photon.Protocol.Deserialize(out team.playersJoined, bytes, ref index);
return team;
}
public class LauncherPlayer {
private string playerName;
private int teamID = -1;
private int pos = -1;
private bool ready = false;
}
public static byte[] serializePlayer(object o)
{
LauncherPlayer player = (LauncherPlayer)o;
var nameBytes = ExitGames.Client.Photon.Protocol.Serialize(player.playerName);
//Lenght playerBytes.Lenght + (4 * 3) (4 bytes in size, 3 ints) + (1 byte from bool)
byte[] bytes = new byte[nameBytes.Length + 13];
int index = 0;
//We need to store the lenght for deserialization
ExitGames.Client.Photon.Protocol.Serialize(nameBytes.Length, bytes, ref index);
System.Array.Copy(nameBytes, 0, bytes, index, nameBytes.Length);
index += nameBytes.Length;
ExitGames.Client.Photon.Protocol.Serialize(player.teamID, bytes, ref index);
ExitGames.Client.Photon.Protocol.Serialize(player.pos, bytes, ref index);
bytes[index] = player.ready ? (byte) 1 : (byte) 0;
return bytes;
}
public static object deserializePlayer(byte[] bytes)
{
LauncherPlayer player = new LauncherPlayer();
int index = 0;
int nameBytesLenght;
ExitGames.Client.Photon.Protocol.Deserialize(out nameBytesLenght, bytes, ref index);
var nameBytes = new byte[nameBytesLenght];
System.Array.Copy(bytes, index, nameBytes, 0, nameBytesLenght);
player.playerName = (string)ExitGames.Client.Photon.Protocol.Deserialize(nameBytes);
index += nameBytes.Length;
ExitGames.Client.Photon.Protocol.Deserialize(out player.teamID, bytes, ref index);
ExitGames.Client.Photon.Protocol.Deserialize(out player.pos, bytes, ref index);
byte b = bytes [index];
player.ready = b == (byte)1 ? true : false;
return player;
}
Console output:
System.String
,