- Home /
How to serialize an object to a byte array
I want to serialize an object to a byte array in order to send it over the Network.
I'm looking for a code sample, but none of the samples I could find actually compiled :-( Probably has to do with the "using" but none of that is documented anywhere I looked.
Also, since Unity has its own serialization system that it uses for the inspector, I was wondering if there was any way to use Unity's built-in serialization. Couldn't find any doc on that either.
Thanks
Answer by phineliner · Dec 01, 2016 at 01:39 PM
Taken from http://answers.unity3d.com/questions/1113376/unet-send-big-amount-of-data-over-network-how-to-s.html
Quick answer: Check out the following code:
Extension Class to provide serialisation for any object: https://bitbucket.org/stupro_hskl_betreuung_kessler/learnit_merged_ss16/raw/e5244ebb38c8fe70759e632ea4224e48f5ca5833/Unity/LearnIT_Merged/Assets/Scripts/Util/ObjectSerializationExtension.cs
Example Class which I am serializing: https://bitbucket.org/stupro_hskl_betreuung_kessler/learnit_merged_ss16/raw/e5244ebb38c8fe70759e632ea4224e48f5ca5833/Unity/LearnIT_Merged/Assets/Scripts/Model/Quiz/QuizQuestion.cs
Example usage. Check out the method "DistributeQuizQuestions()": https://bitbucket.org/stupro_hskl_betreuung_kessler/learnit_merged_ss16/raw/e5244ebb38c8fe70759e632ea4224e48f5ca5833/Unity/LearnIT_Merged/Assets/Scripts/Controller/Quiz/MPQuizController.cs