6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Unity C# MessagePackをいじる

Last updated at Posted at 2015-03-30

こちらのページを参考につくりました。
 http://qiita.com/snaka/items/8da9f89deeef17b1923a

今はAPI作る能力ないので、内部でPackして内部でUnPack

・データクラス側

MessagePackData.cs
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;
}

・Pack・UnbPack側

MsgPackMakeAndLoad.cs
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);
        }
    }
}
6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?