Unity3d protobuf-net的使用

官网地址:http://code.google.com/p/protobuf-net
有时候会无法访问,所以提供了下面的下载
http://pan.baidu.com/s/1cqB1t8

一、简单使用

加压文件之后,会得到如下目录

protobuf目录

只做简单使用的话,只需要将Full/unity/protobuf-net.dll导入到Unity3d项目的Assets/Plugins目录下。

protubuf-net 目录
导入到unity工程

编写需要序列化的类Person.cs

using ProtoBuf;
using System.Collections.Generic;

[ProtoContract]     //需要序列化的类必须加上这个标识
public class Person {
    [ProtoMember(1)] // 成员变量需要加上ProtoMenber标识,而且括号里数字不能重复
    public int ID { get; set; }
    [ProtoMember(2)]
    public string Name { get; set; }
    [ProtoMember(3)]
    public string Age { get; set; }
    [ProtoMember(4)]
    public List<string> ClothColors { get; set; }
    [ProtoMember(5)]
    public Dictionary<int, string> Books { get; set; } 
}

编写测试的用例Test.cs

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

public class Test : MonoBehaviour {

    // Use this for initialization
    void Start () {

        //Serialize();
        Deserialize();
    }

    public void Serialize()
    {
        Person test = new Person();
        test.ID = 1;
        test.Age = 23;
        test.Name = "xianglin";
        test.ClothColors=new List<string>() {"red","blue","while","black"};
        test.Books=new Dictionary<int, string>() { {1,"Red And Black"}, {2,"Future"}, {3,"The Sun"} };

        using (FileStream fs = File.OpenWrite(Application.dataPath + "/Person.bin"))
        {
            Serializer.Serialize(fs, test);
        }
    }

    public void Deserialize()
    {
        Person test = null;
        using (FileStream stream = File.OpenRead(Application.dataPath + "/Person.bin"))
        {
            test = Serializer.Deserialize<Person>(stream);
        }

        Debug.Log(test.ID+" "+ test.Age+" "+test.Name);
        foreach (string color in test.ClothColors)
        {
            Debug.Log(color);
        }
        foreach (int book in test.Books.Keys)
        {
            string bookName = "";
            if (test.Books.TryGetValue(book, out bookName))
            {
                Debug.Log(bookName);
            }
        }
    }

}

jieguo
jieguo

二、使用动态库

在(一)中我们下载了官方protobuf-net包,解压后如下

protobuf目录

为了能够使用动态库,主要进行下面三个步骤

A、ProtoGen文件夹主要用来将.proto格式的信息格式文件生成.cs文件,然后我们用这个.cs文件去生成类库;

现在进入ProtoGen文件夹中创建一个Person.proto的文件,并添加内容如下:

package com.xianglin.model;

message Person
{
    required int32 id=1;
    required string name=2;
    optional int32 age=3;
    optional Sex sex=4;
    repeated string clothcolors=5;
}

enum Sex
{
    man=0;
    female=1;
}

然后在该目录下创建一个Person.bat批处理文件,用于将message转化成C#文件,内容如下:

@echo off
set tool=.

rem Support

set proto=Person
%tool%\protogen.exe -i:%proto%.proto -o:%proto%.cs -q

pause

保存后,双击Person.bat生成Person.cs。(-i输入文件-o输出文件-q退出)

创建Protobuf-netTest类库工程,将自动生成的Class1.cs删除,添加上一步生成的Person.cs,再添加protobuf-net.dll的引用。
这里写图片描述

这里写图片描述

这里写图片描述

最后,点击生成,就可以得到Protobuf-netTest.dll

这里写图片描述

B、Precompile文件夹主要是,用上一步生成的类库,生成序列化和反序列化工具;

进入生成目录,编写批处理文件Project.bat,内容如下:

@echo off
E:\360data\重要数据\桌面\protobuf-net\protobuf-net\Precompile\precompile.exe -i:Probuf-netTest.dll -o:ProtoSerializer.dll -t:com.xianglin.model.ProtoSerializer
pause

双击生成序列化和反序列化工具库(precompile.exe是Precompile文件中的可执行文件,编写的时候根据自己文件所在目录来定)

这里写图片描述

C、导入Unity工程中使用;

通过上面的步骤,得到了和消息定义相关的库文件Probuf-netTest.dll和序列化反序列化工具ProtoSerializer.dll。

现在,将这两个库文件和protobuf-net.dll,导入到Unity项目目录Assets/Plugins/Protobuf-net。

这里写图片描述

然后创建一个Protobuf.cs用于测试。

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using com.xianglin.model;

public class ProtobufTest : MonoBehaviour
{
    public ProtoSerializer Serializer;
    Person person = new Person();
    public string path = "";

    // Use this for initialization
    void Start ()
    {
        Serializer = new ProtoSerializer();
        path = Application.dataPath + "/protobuf.bin";

        //Serialize();
        Deserialize();

    }

    void Serialize()
    {
        person.id = 1;
        person.age = 18;
        person.name = "xianlgin";
        person.sex = Sex.man;
        person.clothcolors.Add("red"); /*= new List<string>() {"red","blue","black","white"};*/
        person.clothcolors.Add("white");

        using (FileStream fs = File.OpenWrite(path))
        {
            Serializer.Serialize(fs,person);
        }

    }

    void Deserialize()
    {
        Person person = new Person();
        using (FileStream fs = File.OpenRead(path))
        {
            person = Serializer.Deserialize(fs, null, typeof (Person)) as Person;
        }

        Debug.Log(person.id.ToString() + person.age.ToString() + person.name + person.sex);

        foreach (string clothcolor in person.clothcolors)
        {
            Debug.Log(clothcolor);
        }
    }
}

在场景中创建一个空物体,将脚本挂载上去,调试运行成功!

这里写图片描述

参考:
http://blog.csdn.net/jk823394954/article/details/47791371

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值