Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created December 14, 2013 02:17
Show Gist options
  • Save tsubaki/7954893 to your computer and use it in GitHub Desktop.
Save tsubaki/7954893 to your computer and use it in GitHub Desktop.
msgpack with unity sample. used msgpack_cli(project=MsgPack.compats.Net.3.5 ,target framework=mono3.5). builded binary https://www.dropbox.com/s/rbnamvknbvwx58h/MsgPack.dll
using MsgPack;
public class DataClass : IPackable, IUnpackable{
public string data;
public int count;
public void UnpackFromMessage (Unpacker unpacker)
{
unpacker.ReadInt32(out count);
unpacker.ReadString(out data);
}
public void PackToMessage (Packer packer, PackingOptions options)
{
packer.Pack(count);
packer.PackString(data);
}
}
using System.IO;
using MsgPack;
using UnityEngine;
public class Test : MonoBehaviour {
void Start () {
byte[] bytes = null;
// pack
var data = new DataClass()
{
count = 120,
data = "hoge",
};
using ( MemoryStream mem = new MemoryStream())
{
Packer packer = Packer.Create(mem);
data.PackToMessage(packer, null);
bytes = mem.ToArray();
}
// unpack
DataClass unpackData = new DataClass();
using (MemoryStream mem = new MemoryStream(bytes))
{
Unpacker unpacker = Unpacker.Create(mem);
unpackData.UnpackFromMessage(unpacker);
}
Debug.Log(unpackData.count);
Debug.Log(unpackData.data);
}
}
@harmonius
Copy link

Thank you for making a Unity version for MsgPack. I will test it out.
Cheers. Arigato.

@tsubaki
Copy link
Author

tsubaki commented Dec 21, 2013

You're welcome. douitashimashite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment