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
1
Question by Codessaurus · Jan 05, 2015 at 12:48 PM · arrayserializationrpcserialize

Serialize an array of custom objects

I'm currently using Photon Network and I need to send an RPC to the connected players with the teams. I created a Team object that defines a team, and all the teams are stored in an array.

Since arrays and custom objects are not serialized automatically, how can I do it? I have no experience in this field and I have googled a lot with no sucess. All I need is some directions on how to do it.

P.S- Actually I read something about converting it all to a byte array an then send it. Any guidelines on how to do this?

Here is my Team object:

 public class Team {
     private int teamID;
     private string[] players;
     private int playersJoined;
 }

EDIT - I folowed @ArkaneX sugestion and got this:

 private static byte[] serializeTeam (object o)
 {
     Team team = (Team)o;
     byte[] bytes = new byte[3 * 4];
     int index = 0;
     ExitGames.Client.Photon.Protocol.Serialize(team.getID(), bytes, ref index);
     ExitGames.Client.Photon.Protocol.Serialize(team.getPlayerList(), bytes, ref index);
     ExitGames.Client.Photon.Protocol.Serialize(team.getSize(), bytes, ref index);
     return bytes;
 }

 private static object deserializeTeam(byte[] bytes)
 {
     Team team = new Team();
     int index = 0;
     ExitGames.Client.Photon.Protocol.Deserialize(out team.teamID, bytes, ref index);
     ExitGames.Client.Photon.Protocol.Deserialize(out team.players, bytes, ref index);
     ExitGames.Client.Photon.Protocol.Deserialize(out team.playersJoined, bytes, ref index);
     return team;
 }

I tought that arrays of strings would spare me these troubles but I'm getting this:

 Argument `#1' cannot convert `string[]' expression to type `short'

Also I'm not sure about bytes array length.

EDIT 2 - I have this another idea, but the same error

 private static byte[] serializeTeam (object o)
 {
     Team team = (Team)o;
     byte[] bytes = new byte[3 * 4];
     int index = 0;
     ExitGames.Client.Photon.Protocol.Serialize(team.teamID, bytes, ref index);
     ExitGames.Client.Photon.Protocol.Serialize(team.players.Length, bytes, ref index);
     foreach(string s in team.players)
     {
         ExitGames.Client.Photon.Protocol.Serialize(s, bytes, ref index);
     }
     ExitGames.Client.Photon.Protocol.Serialize(team.playersJoined, bytes, ref index);
     return bytes;
 }

 private static object deserializeTeam(byte[] bytes)
 {
     Team team = new Team();
     int index = 0;
     ExitGames.Client.Photon.Protocol.Deserialize(out team.teamID, bytes, ref index);
     int lenght = 0;
     ExitGames.Client.Photon.Protocol.Deserialize(out lenght, bytes, ref index);
     team.players = new string[lenght];
     for (int i = 0; i < team.playersJoined; ++i) 
     {
         ExitGames.Client.Photon.Protocol.Deserialize(out team.players[i], bytes, ref index);
     }
     ExitGames.Client.Photon.Protocol.Deserialize(out team.playersJoined, bytes, ref index);
     return team;
 }


Comment
Add comment
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

3 Replies

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

Answer by ArkaneX · Jan 05, 2015 at 04:31 PM

Take a look at this page: http://doc.exitgames.com/en/realtime/current/reference/serialization-in-photon

It is inside 'Photon realtime' section, but contains a sample related to PUN.

EDIT:

Serialize method works by converting passed values to bytes, and filling the bytes array with result of the conversion. There's no overload of the three parameters method accepting string or array of strings, but there's a Serialize method accepting an object and returning array of bytes, so you can use it. Working code for your case should look like this:

 private static byte[] serializeTeam(object o)
 {
     Team team = (Team)o;
     var playerBytes = ExitGames.Client.Photon.Protocol.Serialize(team.players);
     byte[] bytes = new byte[playerBytes.Length + 12];
     int index = 0;
     ExitGames.Client.Photon.Protocol.Serialize(team.teamID, bytes, ref index);
     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;
 }
 
 private 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);
     team.players = (string[])ExitGames.Client.Photon.Protocol.Deserialize(playerBytes);
     index += playerBytes.Length;
     ExitGames.Client.Photon.Protocol.Deserialize(out team.playersJoined, bytes, ref index);
     return team;
 }

Total length of result bytes array is equal to the length of serialized array of players + 12 (three int). Please note that although playerBytes.Length is not a part of your team data, it must be stored, so that during deserialization we know how many bytes we need to copy in order to deserialize string array.

I quickly tested above code by creating Team instance, passing it to first method and then passing resulting array to the second method. I think it should work during Photon (de)serialization as well.

Comment
Add comment · Show 6 · 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 ArkaneX · Jan 06, 2015 at 01:41 PM 0
Share

Hi Bruno. I see that you accepted the answer, but edits of your question indicates you had some problems. Were you able to solve them?

avatar image Codessaurus · Jan 06, 2015 at 02:38 PM 0
Share

Nope buddy I'm having some troubles. I should have "unaccepted" it but I completly forgot. The problem is I can't serialize the array of strings directly. I tried another way of doing it but now I can't serialize the strings. In the documentation it says that is possible so I must be doing something wrong.

avatar image ArkaneX · Jan 06, 2015 at 09:42 PM 0
Share

Hi Bruno - answer is updated with sample code.

avatar image Codessaurus · Jan 06, 2015 at 11:06 PM 0
Share

Goddamit I'm pretty sure I tried that before, maybe I forgot to save the script in $$anonymous$$onoDevelop... This happens so many times!

I'am really glad for all your help! I have been stuck in this for so much time and I would be even worse if it weren't for your effort. I just have 1 more thing to ask if you dont $$anonymous$$d:

I have seen some people using external libraries for Serialize/Deserialize objects. Due to the complexity of using them, Im not thinking of adopting them right now, but it got me thinking - Is it worth?

avatar image ArkaneX · Jan 06, 2015 at 11:30 PM 1
Share

It depends. You can serialize objects to many formats, for example X$$anonymous$$L, JSON, array of bytes, like in your Photon code, or some proprietary formats. Any of these has their own pros and cons, e.g. X$$anonymous$$L or JSON data might be easily edited, while binary data takes less space.

In your case, serializing to byte array is required, but in fact, you don't need to use serialization mechanism provided by ExitGames. You can for example serialize your Team instance using BinaryFormatter class, and then your serialization/deserialization code would be a few lines only, no matter how many fields your class would contain. But size of a byte array produced by BinaryFormatter would be much bigger than the one produced by code from my answer.

In your case, size of data sent over the network might be important, so you probably want to stick with custom code. But in other situation, you might want a general solution, and then BinaryFormatter might be better. At the end it's up to you :)

Show more comments
avatar image
0

Answer by Kamuiyshirou · Jan 05, 2015 at 04:30 PM

View this document friend : http://blogs.unity3d.com/pt/2014/06/24/serialization-in-unity/

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 ArkaneX · Jan 05, 2015 at 04:33 PM 1
Share

I think OP is asking about serialization specific to Photon Unity Networking.

avatar image Codessaurus · Jan 05, 2015 at 06:14 PM 0
Share

I have already tried everything in that page, I just can't get it to work :x

I will try @ArkaneX doc when I get home and lets see if can do it. Thanks (:

avatar image
0

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

You can also try this. Serialize custom object and send that serialization data (string/byte[] format) to other Photon clients. On receiving this serialization data, other clients can deserialize it and retrieve back original object. However, there is some limit on message size while sending over photon network. So its better to share lightweight data only.

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 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

28 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 avatar image

Related Questions

Serialize unity classes 0 Answers

JsonUtility not working with Arrays 2 Answers

Very Strange RPC error 1 Answer

Network Instiantiate Terrain (Voxel) issues C# 0 Answers

Serialized Array: Trying to wrap my head around it 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