unity中使用protobuf(兼容ios平台)

来源:互联网 发布:数据融合是数据预处理 编辑:程序博客网 时间:2024/05/22 10:35

导出ios平台的注意:把工程设置为.Net 2.0 subset (否则ios平台不能运行)

下载protobuf-net源码http://download.csdn.net/detail/qwezcl/9510727

建立一个新的文件smcs.rsp  ,内容是-unsafe 

把protobuf-net源码拷贝unity工程中


例子:

using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine.UI;
using AOT;
using ProtoBuf;
public class Test2 : MonoBehaviour {
    
    void Start () {
        Test test=new Test();
        test.id=10;
        test.data="ssssss用户家";
        byte[] data;
        using (MemoryStream ms = new MemoryStream())
        {
            Serializer.Serialize<Test>(ms, test);
            data = new byte[ms.Length];
            ms.Position= 0;
            ms.Read(data, 0, data.Length);
            Debug.Log(data.Length+"-----");
        }
        using(MemoryStream ms = new MemoryStream()){
            ms.Write(data,0,data.Length);
            ms.Position = 0;
            Test chatMsg = Serializer.Deserialize<Test>(ms);
            Debug.Log(chatMsg.data);
           Debug.Log(chatMsg.data+chatMsg.id);
        }
    }

    // Update is called once per frame
    void Update () {

    }
}
[ProtoContract]  
public struct Test {  
    [ProtoMember(1)]  
    public int id;

    [ProtoMember(2)]
    public string data;
    //    public List<String> data  
    //    {  
    //        get;  
    //        set;  
    //    }  
} 



0 0
原创粉丝点击