Skip to content

Instantly share code, notes, and snippets.

@st0326s
Last active August 29, 2015 14:18
Show Gist options
  • Save st0326s/c91b916bb680591d303a to your computer and use it in GitHub Desktop.
Save st0326s/c91b916bb680591d303a to your computer and use it in GitHub Desktop.
Unity C# MessagePackをいじる ref: http://qiita.com/satotin/items/497ff4ea33953ef09174
public MessagePackData PackData = new MessagePackData();
public byte[] body;
// Use this for initialization
void Start () {
PersonalData personalData = new PersonalData();
personalData.id = 1;
personalData.name = "あいうえお";
personalData.money = 9876543210;
personalData.weight = 456789.0123f;
personalData.height = 321654987.123456789d;
List<JobData> JobList = new List<JobData>();
for(int i =0; i < 5; i++)
{
JobData jData = new JobData();
jData.company = "株式会社〇○" + i.ToString();
jData.langage = "日本語" + i.ToString();
JobList.Add(jData);
}
PackData.JobDataListl = JobList;
PackData.PersonalData = personalData;
// buffer に何らかのデータを格納する処理...
var packer = new MsgPack.ObjectPacker();
body = packer.Pack(PackData);
}
public void OnClickUnPackApiData()
{
var packer = new MsgPack.ObjectPacker();
MessagePackData result = packer.Unpack<MessagePackData>(body);
Debug.Log(result.PersonalData.id.ToString());
Debug.Log(result.PersonalData.name);
Debug.Log(result.PersonalData.money.ToString());
Debug.Log(result.PersonalData.weight.ToString());
Debug.Log(result.PersonalData.height.ToString());
foreach(JobData jdata in result.JobDataListl)
{
Debug.Log(jdata.company);
Debug.Log(jdata.langage);
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PersonalData
{
public int id;
public string name;
public long money;
public float weight;
public double height;
}
public class JobData
{
public string company;
public string langage;
}
public class MessagePackData
{
public PersonalData PersonalData;
public List<JobData> JobDataListl;
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class MsgPackMakeAndLoad : MonoBehaviour {
public MessagePackData PackData = new MessagePackData();
public byte[] body;
// Use this for initialization
void Start () {
PersonalData personalData = new PersonalData();
personalData.id = 1;
personalData.name = "あいうえお";
personalData.money = 9876543210;
personalData.weight = 1234.567f;
personalData.height = 321654987.123456789d;
List<JobData> JobList = new List<JobData>();
for(int i =0; i < 5; i++)
{
JobData jData = new JobData();
jData.company = "株式会社〇○" + i.ToString();
jData.langage = "日本語" + i.ToString();
JobList.Add(jData);
}
PackData.JobDataListl = JobList;
PackData.PersonalData = personalData;
// buffer に何らかのデータを格納する処理...
var packer = new MsgPack.ObjectPacker();
body = packer.Pack(PackData);
}
public void OnClickUnPackApiData()
{
var packer = new MsgPack.ObjectPacker();
MessagePackData result = packer.Unpack<MessagePackData>(body);
Debug.Log(result.PersonalData.id.ToString());
Debug.Log(result.PersonalData.name);
Debug.Log(result.PersonalData.money.ToString());
Debug.Log(result.PersonalData.weight.ToString());
Debug.Log(result.PersonalData.height.ToString());
foreach(JobData jdata in result.JobDataListl)
{
Debug.Log(jdata.company);
Debug.Log(jdata.langage);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment