Unity的数据序列化与反序列化用于网络传输

本文介绍了一个用于Unity客户端与服务器通信的Packet类实现方法,通过BitConverter进行数据的序列化与反序列化,支持int、float、string等类型的变量封装成byte数组进行传输。同时提供了使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Unity客户端与服务器通信,通过传输byte数组实现。这里使用BitConverter进行数据的序列化与反序列化把int,float,string各种变量封装成一个byte进行通信。

Packet类如下

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Text;

/// <summary>
/// 用来序列化string
/// </summary>
public class MyBitConverter
{
    static Encoding m_codePage = Encoding.UTF8;
    static public byte[] GetBytes(string sTmp)
    {
        if (sTmp == null)
        {
            return null;
        }
        return m_codePage.GetBytes(sTmp);
    }

    static public string GetString(byte[] bufByte, int idx, int iLen)
    {
        return m_codePage.GetString(bufByte, idx, iLen);
    }
}
public class Packet : System.Object
{
    public byte[] m_buf;
    public int m_iPos = 0;
    public int m_iSize = 0;

    /// <summary>
    /// 重新设置buf的size
    /// </summary>
    /// <param name="iNewSize"></param>
    public void sizeSet(int iNewSize)
    {
        if (iNewSize < m_iSize)
            return;
        byte[] tmp = new byte[iNewSize];//产生一个新大小的buff
        m_iSize = iNewSize;
        if (m_iPos > 0)
            m_buf.CopyTo(tmp, 0);
        m_buf = tmp;
    }

    /// <summary>
    /// 复制操作把一个数组给m_buf
    /// </summary>
    /// <param name="bufByte"></param>
    /// <param name="iLen"></param>
    /// <returns></returns>
    public int write(byte[] bufByte,int iLen)
    {
        int iNewPos = m_iPos + iLen;
        //当新产生的位置大于老的size时,扩容
        if (iNewPos > m_iSize)
            sizeSet(iNewPos);

        if(iLen < bufByte.Length)
        {
            Array.Copy(bufByte, 0, m_buf, m_iPos, iLen);//只复制当前部分的数组
        }
        else if (iLen > bufByte.Length)
        {
            bufByte.CopyTo(m_buf, m_iPos);
            for (int i = 0; i < iLen - bufByte.Length; i++)
            {
                m_buf[m_iPos + bufByte.Length + i] = 0;
            }
        }
        else if (iLen == bufByte.Length)
        {
            bufByte.CopyTo(m_buf, m_iPos);
        }
        m_iPos = iNewPos;
        return m_iPos;
    }

    static public Packet operator+(Packet self,int tmp)
    {
        byte[] bufByte = BitConverter.GetBytes(tmp);
        self.write(bufByte, bufByte.Length);
        return self;
    }

    static public Packet operator +(Packet self, float tmp)
    {
        byte[] bufByte = BitConverter.GetBytes(tmp);
        self.write(bufByte, bufByte.Length);
        return self;
    }

    static public Packet operator +(Packet self, ushort tmp)
    {
        byte[] bufByte = BitConverter.GetBytes(tmp);
        self.write(bufByte, bufByte.Length);
        return self;
    }

    static public Packet operator +(Packet self, string tmp)
    {
        if ( tmp == null)
        {
            byte[] bufByte = BitConverter.GetBytes(0);
            self.write(bufByte, sizeof(int));
        }

        if(tmp.Length > 0)
        {
            byte[] bufByte = MyBitConverter.GetBytes(tmp);
            byte[] bufLen = BitConverter.GetBytes(tmp.Length);
            self.write(bufLen, sizeof(int));
            self.write(bufByte, bufByte.Length);
        }
        else if (tmp.Length == 0)
        {
            byte[] bufByte = BitConverter.GetBytes(0);
            self.write(bufByte, sizeof(int));
        }
        return self;
    }

    public Packet to(ref int tmp)
    {
        int iOffset = 4;
        if (m_iPos + iOffset <= m_buf.Length)
        {
            tmp = BitConverter.ToInt32(m_buf, m_iPos);
            m_iPos += iOffset;
        }
        return this;
    }

    public Packet to(ref uint tmp)
    {
        int iOffset = 4;
        if (m_iPos + iOffset <= m_buf.Length)
        {
            tmp = BitConverter.ToUInt32(m_buf, m_iPos);
            m_iPos += iOffset;
        }
        return this;
    }

    public Packet to(ref float tmp)
    {
        int iOffset = 4;
        if (m_iPos + iOffset <= m_buf.Length)
        {
            tmp = BitConverter.ToSingle(m_buf, m_iPos);
            m_iPos += iOffset;
        }
        return this;
    }

    public Packet to(ref ushort tmp)
    {
        int iOffset = 2;
        if (m_iPos + iOffset <= m_buf.Length)
        {
            tmp = BitConverter.ToUInt16(m_buf, m_iPos);
            m_iPos += iOffset;
        }
        return this;
    }

    public Packet to(ref string tmp)
    {
        if (tmp == null)
            tmp = "";
        int iLen = 0;
        this.to(ref iLen);
        if (iLen == 0)
        {
            tmp = "";
            return this;
        }
        tmp = MyBitConverter.GetString(m_buf, m_iPos, iLen);
        m_iPos += iLen;
        return this;
    }

    public void posSet(int tmp)
    {
        m_iPos = tmp;
    }

    static public Packet bufByteToPkt(byte[] bufByte,int iLen)
    {
        Packet pkt = new Packet();
        pkt.sizeSet(iLen);
        System.Array.Copy(bufByte, 0, pkt.m_buf, 0, iLen);
        pkt.m_iPos = 0;
        return pkt;
    }

}

使用示例如下

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

    int m_iCnt = 0;
    public int m_recv = 0;
    public UILabel m_labShow;
    // Use this for initialization
    void Start () {
        Packet pkt = new Packet();
        int i1 = 1;
        int i2 = 2;
        float f3 = 3.0f;
        string str = "luoyikun";
        pkt = pkt + i1 + i2 + f3 + str;
        pkt.posSet(0);
        pktTest(pkt);

    }

    void pktTest(Packet pkt)
    {
        int i1 = 0;
        int i2 = 0;
        float i3 = 0.0f;
        string str = "";
        pkt.to(ref i1).to(ref i2).to(ref i3).to(ref str);
    }
}

using System; //需要用到MemoryStream using System.IO; using UnityEngine; //引入ProtoBuf命名空间 using ProtoBuf; /// /// 测试类 /// public class TestProtobuf : MonoBehaviour { /// /// 用于测试的数据类 /// [ProtoContract] //声明这个类能被序列化 public class UserData { //声明每一个需要被序列化的成员,编号从1开始 [ProtoMember(1)] public int id; [ProtoMember(2)] public string name; [ProtoMember(3)] public int level; } //测试代码 void Start() { //将要被序列化的UserData示例 UserData user1 = new UserData (); user1.id = 1; user1.name = "User1"; user1.level = 10; //打印user1 Debug.Log (string.Format ("user1-> id:{0}, name:{1}, level:{2}", user1.id, user1.name, user1.level)); //序列化 byte[] buff = null; using (MemoryStream ms = new MemoryStream ()) { Serializer.Serialize (ms, user1); ms.Position = 0; int length = (int)ms.Length; buff = new byte[length]; ms.Read (buff, 0, length); } //输出字节数组 Debug.Log (string.Format("Serialized data-> {0}", BitConverter.ToString(buff))); //反序列化 UserData user2 = default(UserData); using (MemoryStream ms = new MemoryStream (buff)) { user2 = Serializer.Deserialize (ms); } //打印反序列化生成的user2 Debug.Log (string.Format ("user2-> id:{0}, name:{1}, level:{2}", user2.id, user2.name, user2.level)); } } 作者:qufangliu 链接:https://www.jianshu.com/p/d9be1b3d2446 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值