当前位置:   article > 正文

Unity3d网络传输:字节流序列化反序列化_序列化操作方法:public byte[] getbytes()

序列化操作方法:public byte[] getbytes()

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;
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200

使用示例如下

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);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/108344
推荐阅读
相关标签
  

闽ICP备14008679号