ProtoBuf-net

来源:互联网 发布:csgo笔记本 a卡优化 编辑:程序博客网 时间:2024/06/05 01:11

1.Protobuf-net:用于数据的序列化和反序列化。


201712.22 增加工具类

using UnityEngine;
using ProtoBuf;
using System.IO;
using System;

public class ProtoBufUtil : MonoBehaviour
{
    public static byte[] SaveByte<T>(T t)
    {
        try
        {
            using (MemoryStream ms = new MemoryStream())
            {
                Serializer.Serialize<T>(ms, t);


                byte[] result = new byte[ms.Length];
                ms.Position = 0;
                ms.Write(result, 0, result.Length);


                return result;
            }
        }
        catch (Exception ex)
        {
            Debug.LogError("序列化失败" + ex.ToString());
            return null;
        }
    }


    public static T LoadByte<T>(byte[] msg)
    {
        try
        {
            using (MemoryStream ms = new MemoryStream())
            {
                ms.Read(msg, 0, msg.Length);
                ms.Position = 0;
                return Serializer.Deserialize<T>(ms);
            }
        }
        catch (Exception ex)
        {
            Debug.LogError("反序列化失败" + ex.ToString());
            return default(T);
        }
    }



    public static void SaveFile<T>(T t,string path)
    {
        try
        {
            using (MemoryStream ms = new MemoryStream())
            {
                Serializer.Serialize<T>(ms, t);
                File.WriteAllBytes(path, ms.ToArray());
            }
        }
        catch (Exception ex)
        {
            Debug.LogError("序列化失败" + ex.ToString());
        }
    }


    public static T LoadFile<T>(string path)
    {
        try
        {
            using (MemoryStream ms = new MemoryStream())
            {
                var bytes = File.ReadAllBytes(path);
                ms.Write(bytes, 0, bytes.Length);


                return Serializer.Deserialize<T>(ms);
            }
        }
        catch (Exception ex)
        {
            Debug.LogError("反序列化失败" + ex.ToString());
            return default(T);
        }
    }

}


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


public class ProtoBufTest : MonoBehaviour {

    private void Start()
    {
        ProtoInfo info = new ProtoInfo();
        var bs = ProtoBufUtil.SaveByte<ProtoInfo>(info);
        var result = ProtoBufUtil.LoadByte<ProtoInfo>(bs);
        Debug.LogError(result.number);
    }
}

[ProtoContract]
public class ProtoInfo
{
    [ProtoMember(1)]
    public int number = 1;
}




2.基本使用方法:

[ProtoContract]

class PlayerInfo

{

[ProtoMember(1)]

public int age;

[ProtoMember(2)]

public string name;

[ProtoMember(3)]

public TestInfo testInfo;

 

public override string ToString()

{

return string.Format("age:{0},name:{1},TestInfo.index:{2},TestInfo.name:{3}", age, name, testInfo.testIndex, testInfo.testName);

}

}

 

[ProtoContract]

class TestInfo

{

[ProtoMember(1)]

public int testIndex;

[ProtoMember(2)]

public string testName;

}

 

static void Main(string[] args)

{

SaveData();

Console.WriteLine("saved");

Console.ReadLine();

LoadData();

Console.ReadKey();

 

}

 

static void SaveData()

{

PlayerInfo info = new PlayerInfo() { age = 10, name = "test", testInfo = new TestInfo() { testIndex = 1, testName = "testName" } };

using (var file = File.Create("C:/Users/SHY/Desktop/PlayerInfo.bin"))

{

Serializer.Serialize(file, info);

}

}

 

static void LoadData()

{

PlayerInfo info;

using (var file = File.OpenRead("C:/Users/SHY/Desktop/PlayerInfo.bin"))

{

info = Serializer.Deserialize<PlayerInfo>(file);

Console.WriteLine(info.ToString());

}

}