unity 使用protobuf-net的学习记录

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ProtoBuf;
using System.IO;

[ProtoContract]
public class Address
{
    [ProtoMember(1)]
    public string Line1;
    [ProtoMember(2)]
    public string Line2;
}

[ProtoContract]
public class Person
{
    [ProtoMember(1)]
    public int Id;
    [ProtoMember(2)]
    public string Name;
    [ProtoMember(3)]
    public Address Addr;
}

public class Test : MonoBehaviour {

    string path;
    void Start () {
        path = Application.dataPath + @"\t.bin";


        Person person = new Person();
        person.Id = 1;
        person.Name = "First";
        person.Addr = new Address { Line1 = "line1", Line2 = "line2" };

        // ProtoBuf序列化
        using (var file = System.IO.File.Create(path))
        {
            ProtoBuf.Serializer.Serialize(file, person);
        }

        // ProtoBuf反序列化
        Person binPerson = null;
        using (var file = System.IO.File.OpenRead(path))
        {
            binPerson = ProtoBuf.Serializer.Deserialize(file);
        }
        Debug.Log(binPerson.Name);

    }
}

 

unity 使用protobuf-net的学习记录_第1张图片

 

 

unity 使用protobuf-net的学习记录_第2张图片

你可能感兴趣的:(unity,学习记录,C#,protobuf)