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

Taken from uNET - Send big amount of data over network (How to split send byte[]?) - Questions & Answers - Unity Discussions

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