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<Person>(file);
}
Debug.Log(binPerson.Name);
}
}