Help With Serialization

Hello,

I need some help with serializing a custom class. The class I am trying to serialize is below.
public class PlayerProperties {
    public string name;
    public int id;
    public PhotonView playerObject;
    public Team team;
    public bool isLocal;
}

I am referring to the page below, but it does not contain enough information for me to figure out exactly how I would create this custom serializer.

http://doc.exitgames.com/en/realtime/cu ... -in-photon

Any guidance would be greatly appreciated. Thank you.

- TATELAX

Comments

  • Also, the "Team" type is outlined below.
    public enum Team {
        Blue,
        Yellow,
        Red,
        Green,
        NullTeam
    }
    
  • So I was able to make this serializer, but I'm getting an error that it was unable to serialize.
        public static byte[] SerializePlayerProperties(object o) {
    
            PlayerProperties player = (PlayerProperties)o;
            MemoryStream ms = new MemoryStream(4 * 4);
    
            ms.Write(System.Text.Encoding.UTF8.GetBytes(player.name), 0, 4);
            ms.Write(BitConverter.GetBytes(player.id), 0, 4);
            ms.Write(BitConverter.GetBytes(player.playerView.viewID), 0, 4);
            ms.Write(BitConverter.GetBytes((byte)player.team), 0, 4);
            ms.Write(BitConverter.GetBytes(player.isLocal), 0, 4);
    
            return ms.ToArray();
        }
    
  • When reporting errors, please post error message.

    You need to use Protocol.Serialize and Protocol.Deserialize methods as shown in doc to let Photon serialize your data properly.
    Don't forget also register your type and (de)serialization methods.
  • You can also take a look at the PunTeams script in PUN.
    Using a BitConverter on an integer is a bit wasteful and not needed really.