[CLOSED]Problem with serialization after PUN update
After PUN update from 1.52 to 1.6 serialization stopped working. I'm getting exception "Exception: cannot serialize(): Run.Domain.NetworkPlayer..."
public sealed class PhotonSerializableNetworkPlayer : INetworkSerializable
{
...
#region INetworkSerializable implementation
public Byte[] Serialize(object customObject)
{
NetworkPlayer player = (NetworkPlayer)customObject;
Int32 index = 0;
Int16 nicknameLength = (Int16)player.Nickname.Length;
Byte[] result = new Byte[sizeof(Int16) + nicknameLength + PhotonSerializableColor.ByteArraySize + 5 * sizeof(Byte)];
Protocol.Serialize(nicknameLength, result, ref index);
Byte[] nicknameBytes = Protocol.Serialize(player.Nickname);
Buffer.BlockCopy(nicknameBytes, 0, result, index, nicknameBytes.Length);
index += nicknameLength;
Byte[] colorBytes = Protocol.Serialize(player.Color);
Buffer.BlockCopy(colorBytes, 0, result, index, colorBytes.Length);
index += colorBytes.Length;
result[index++] = (player.IsReady) ? (Byte)1 : (Byte)0;
return result;
}
public object Deserialize(Byte[] byteArray)
{
NetworkPlayer result = new NetworkPlayer();
Int32 index = 0;
Int16 nicknameLength;
Protocol.Deserialize(out nicknameLength, byteArray, ref index);
Byte[] nicknameBytes = new Byte[nicknameLength];
Buffer.BlockCopy(byteArray, index, nicknameBytes, 0, nicknameLength);
result.Nickname = (String)Protocol.Deserialize(nicknameBytes);
index += nicknameLength;
Byte[] colorBytes = new Byte[PhotonSerializableColor.ByteArraySize];
Buffer.BlockCopy(byteArray, index, colorBytes, 0, PhotonSerializableColor.ByteArraySize);
result.Color = (SerializableColor)Protocol.Deserialize(colorBytes);
index += PhotonSerializableColor.ByteArraySize;
result.IsReady = (byteArray[index++] == (Byte)1) ? true : false;
return result;
}
#endregion
then I register delegates:
{
...
PhotonPeer.RegisterType(typeof(NetworkPlayer), (byte)'P', playerSerialization.Serialize, playerSerialization.Deserialize);
...
}
Player's byte key is unique(i mean 'P', none of the other types uses it). What can be the problem? Unity 4.6.7 PUN 1.6
Answer by john_yarko · Aug 09, 2015 at 12:34 PM
Managed to find the problem. It was in byte-key, for some reason it was either used by some other type or smth else:) After changing it to e.g. 'L' everything started to work as intended.
Answer by nogebatorr · Oct 01, 2016 at 08:50 PM
@john_yarko, I got the same problem when try to register my own type with byte 'P'. Then I found your post and try to use 'W' with the same result. Then I try 'Z' and 'A' - works good.
So I search in the project for the PhotonPeer.RegisterType call and found this snippet in Photon sources :)
/// <summary>
/// Internally used class, containing de/serialization methods for various Unity-specific classes.
/// Adding those to the Photon serialization protocol allows you to send them in events, etc.
/// </summary>
internal static class CustomTypes
{
/// <summary>Register</summary>
internal static void Register()
{
PhotonPeer.RegisterType(typeof(UnityEngine.Vector2), (byte)'W', SerializeVector2, DeserializeVector2);
PhotonPeer.RegisterType(typeof(Vector3), (byte)'V', SerializeVector3, DeserializeVector3);
PhotonPeer.RegisterType(typeof(Quaternion), (byte)'Q', SerializeQuaternion, DeserializeQuaternion);
PhotonPeer.RegisterType(typeof(PhotonPlayer), (byte)'P', SerializePhotonPlayer, DeserializePhotonPlayer);
}
}
I think it could be necessary for Photon team to inform developers about this class here