2 Comments

Google Protocol Buffer Tutorial

Recently we needed needed to serialize some data for a Unity project. We started using C#’s BinaryFormatter class and got it working great on PC/Mac but it would give us errors when running on an iPad. After a little digging we realized that BinaryFormatter uses JIT (just in time) compilation and that is not available on mobile devices which use AOT (ahead of time) compilation.

We found that Google’s Protocol Buffer for .net works fabulously! It’s a little confusing at first to setup but is very easy to work with.

Download protobuf-net

Download protobuf-net here: http://code.google.com/p/protobuf-net/downloads/list. We used protobuf-net r580.zip. Since we’re using a Unity project, the only file we needed in the entire protobuf-net download is located at protobuf-net r580\Full\unity\protobuf-net.dll. Remember where this file is or copy it somewhere easily accessible.

Project Setup

Two Visual Studio projects are required for this: DataModels project and SerializerBuilder project. They will be explained below.

DataModels Project

  1. Create a new VS project of type Class Library (building the project it will export a .dll file which your Unity project needs)
  2. In Solution Explorer, right click References and Add Reference. Browse to your protobuf-net.dll file and add the reference.
  3. Go to Project > (Project Name) Properties > Application and make sure the Target Framework is set for .Net 2.0 or else Unity will complain!
  4. Start adding data you need to serialize.

Example DataModels file: http://pastebin.com/T6uz4UxR

SerializerBuilder Project

  1. Create a new VS project of type Console Application.
  2. In Solution Explorer, right click References and Add Reference. Add both protobuf-net.dll and the exported DataModels.dll from your DataModels project’s bin/debug (or release) folder.
  3. Go to Project > (Project Name) Properties > Application and make sure the Target Framework is set for .Net 2.0 or else Unity will complain!
  4. Add the classes you need to be serialized/deserialized from DataModels and have the project compile them.

Example ModelSerializer file: http://pastebin.com/jLBkwphk

When you run this project, it will make a new .dll file called ModelSerializer.dll. Copy the three .dll files (protobuf-net.dll, DataModels.dll and ModelSerializer.dll) from your project’s bin/debug (or release) folder into your Unity project and make sure they’re imported. See “Post-Build Script” below for a quick script that will auto-copy the .dll files for you.

Unity Serializer/Deserialize

Use the new types that you can access from the .dll files to serialize/deserialize data within your Unity project!

Example Unity file: http://pastebin.com/q0FJ0hXb

Post-Build Script

Since we ended up editing the DataModels script quite a bit, I wrote a quick post-build script that automatically copies the three .dll files to our Unity project when I run the SerializerBuilder project. You do this by going to Project > (Project Name) Properties. Under the Build Events tab on the left, there is a Post-build event command line text box. Here is the script I used.

cd $(ProjectDir)

copy /y bin\debug\*.dll $(ProjectDir)\..\..\..\..\Unity_Project\Assets\Support\ProtoBuf (make this whatever directory you want to store the .dll files in within your Unity project)

Summary

This was something we would never have figured out without the help of Prime31’s suggestion and especially Friction Point Studio’s tutorial. In the end we’re very pleased with Protobuf-net and how quickly it serializes and deserializes our data and recommend it for anyone needing serialization with for Unity on iPad/iPhone.

Also, you may notice that this post is VERY similar to sims11tz’s post on the Unity forums. Thanks goes to him – we worked on this project together and he posted that on the Unity forums. I wanted to go through his tutorial and write it all up again as a refresher for me and so it would be in at least one other spot on the interwebs😛

Links

Leave a comment

About Time

I often get programming help by reading people’s blogs. I’ve said countless times that I need to start a blog in hopes that I can possibly help someone in return. Hopefully at least one of my posts will help someone waste less time on silly things and spend more time having fun programming.

Colter Haycock

Follow

Get every new post delivered to your Inbox.