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());
}
}
- ProtoBuf-net
- protobuf-net
- Protobuf-net
- ProtoBuf-net
- protobuf-net
- protobuf-net 简介
- protobuf-net学习笔记
- protobuf-net简单示例
- Unity3D 中 protobuf-net
- Unity 使用 Protobuf-net
- Protobuf-net学习笔记
- protobuf-net 的应用
- C# Protobuf-Net 序列化
- Unity中使用Protobuf-net
- Protobuf-Net 下载安装使用
- Protobuf-net ProtoGen的使用
- Unity3d protobuf-net的使用
- unity中简单实用protobuf(Protobuf-net)
- TCP三次握手四次挥手详解
- echarts多表联动,及更正
- 安卓系统之旅——安卓基本架构
- 深度学习驱动数据中心功率密度
- 递推 一只小蜜蜂
- ProtoBuf-net
- 编译出现:test4.5.java:1: 错误: 需要'{'public class test4.5问题的解决方法
- PostgreSQL教程(二):SQL语言
- 队列实例(全)
- 使用Bootstrap和Web API创建一个ASP.NET web Forms 应用程序
- 错误提示
- jsp获取当前地址栏的地址
- 冒泡排序
- Git常用命令和操作