Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Codessaurus · Jan 15, 2015 at 05:31 PM · serializationcustom classdeserialization

Serialization\Deserialization problem

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. (http://answers.unity3d.com/questions/869284/serialize-an-array-of-custom-objects.html)

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
 ,
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Codessaurus · Jan 15, 2015 at 05:33 PM 0
Share

@ArkaneX I'am really sorry to bother you but since you helped me a lot on the last one, my guess is that I'am just failing something obvious

avatar image Codessaurus · Jan 26, 2015 at 03:00 AM 0
Share

I anyone is interested to follow the discussion at Exit Games forum here is the link: http://forum.exitgames.com/viewtopic.php?f=17&t=5647

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ArkaneX · Jan 15, 2015 at 06:52 PM

The only difference I see, is changed type of players field, so this looks like a source of problem.

Photon knows how to serialize string (and string[]) types, but it doesn't know how to serialize LauncherPlayer type. You need to register it, and provide serialize/deserialize methods for this type, the same way you did for Team class.

EDIT

I checked the code of Serialize(object) method, and in my opinion it contains error. To serialize it uses a readonly MemoryStream, which is reused for each call. If at some point this stream has for example length of 10, then serializing an object which should result in 3-elements array, will still return 10-element array.

Anyway - I created a method, which can be used instead of Serialize(object), and in my tests it works:

 public static byte[] SerializeObject(object obj)
 {
     var serializeMethod = typeof(ExitGames.Client.Photon.Protocol).GetMethod("Serialize",
         System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic, null,
         new System.Type[] { typeof(System.IO.MemoryStream), typeof(object), typeof(bool) }, null);
     using (var ms = new System.IO.MemoryStream())
     {
         serializeMethod.Invoke(null, new object[] { ms, obj, true });
         return ms.ToArray();
     }
 }

Please note that this uses reflection, so it is not blazing fast, and additionally might stop working in the next version of PUN.

I suggest posting this issue in ExitGame's forums, and asking for some guidance. Maybe their Serialize method was meant to be used in a different way?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Codessaurus · Jan 15, 2015 at 07:19 PM 0
Share

I did, and it's working fine! I will post the code

avatar image ArkaneX · Jan 16, 2015 at 12:48 AM 0
Share

Answer updated. I hope I wrote it clearly enough, because it's quite late here, and I'm slowly falling asleep...

avatar image
0

Answer by Voxel-Busters · Aug 07, 2015 at 12:06 PM

For serializing you can choose c# Binary formatter or else third party plugin from asset store. I would also recommend you to check out our plugin Runtime Serialization for Unity which can serialize custom c# objects as well as Unity objects like GameObjects, Transform, MonoBehaviours etc. For complete list of supported types, please check this link.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is scene object serialized multiple times when entering play mode? 1 Answer

Serialization of custom class 1 Answer

Serialization creates file while returning nothing 2 Answers

How do I remove a serialized field correctly? 0 Answers

Json deserialisation of server results? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges