Protocol Buffers
using System.Collections; using System.Collections.Generic; using UnityEngine; using ProtoBuf; [ProtoContract] public class User { [ProtoMember(1)] public int ID { get; set; } [ProtoMember(2)] public string username { get; set; } [ProtoMember(3)] public string password { get; set; } [ProtoMember(4)] public HeroType heroType { get; set; } public enum HeroType { Master, Warrior } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using ProtoBuf; using System.IO; public class TestProtoBuffer : MonoBehaviour { void Start() { #region ProtoBuffer生成二进制 //User user = new User(); //user.ID = 1233; //user.username = "李忠彬"; //user.password = "sncn188299"; //user.heroType = User.HeroType.Master; //FileStream dataFileStream = File.Create(Application.dataPath + @"/data.bin"); //print(Application.dataPath + @"/data.bin"); //Serializer.Serialize<User>(dataFileStream, user); //dataFileStream.Close(); //using (FileStream dataFileStream = File.Create(Application.dataPath + @"/data.bin")) //{ // Serializer.Serialize<User>(dataFileStream, user); //} #endregion #region ProtoBuffer解析二进制文件 User user; using (FileStream datafilestream= File.Open(Application.dataPath + @"/data.bin", FileMode.Open)) { user=Serializer.Deserialize<User>(datafilestream); } print(user.ID+user.password+user.username+user.heroType.ToString()); #endregion } }