Unity封装MemoryStream进行内存读写

Unity封装MemoryStream进行内存读写

MMO_MemoryStream类


using System;
using System.IO;
using System.Text;

public class MMO_MemoryStream : MemoryStream
{
    public MMO_MemoryStream()
    {

    }
    public MMO_MemoryStream(byte[] buffer) :base(buffer)
    {

    }
    #region Short
    public short ReadShort()
    {
        byte[] arr = new byte[2];
        base.Read(arr, 0, arr.Length);
        return BitConverter.ToInt16(arr, 0);
    }
    public void WriteShort(short value)
    {
        byte[] arr = BitConverter.GetBytes(value);
        base.Write(arr,0,arr.Length);
    }
    #endregion
    #region UShort
    public ushort ReadUShort()
    {
        byte[] arr = new byte[2];
        base.Read(arr, 0, arr.Length);
        return BitConverter.ToUInt16(arr, 0);
    }
    public void WriteUShort(ushort value)
    {
        byte[] arr = BitConverter.GetBytes(value);
        base.Write(arr, 0, arr.Length);
    }
    #endregion
    #region Int
    public int ReadInt()
    {
        byte[] arr = new byte[4];
        base.Read(arr, 0, arr.Length);
        return BitConverter.ToInt32(arr, 0);
    }
    public void WriteInt(int value)
    {
        byte[] arr = BitConverter.GetBytes(value);
        base.Write(arr, 0, arr.Length);
    }
    #endregion
    #region UInt
    public uint ReadUInt()
    {
        byte[] arr = new byte[4];
        base.Read(arr, 0, arr.Length);
        return BitConverter.ToUInt32(arr, 0);
    }
    public void WriteUInt(uint value)
    {
        byte[] arr = BitConverter.GetBytes(value);
        base.Write(arr, 0, arr.Length);
    }
    #endregion
    #region Float
    public float ReadFloat()
    {
        byte[] arr = new byte[4];
        base.Read(arr, 0, arr.Length);
        return BitConverter.ToSingle(arr, 0);
    }
    public void WriteFloat(float value)
    {
        byte[] arr = BitConverter.GetBytes(value);
        base.Write(arr, 0, arr.Length);
    }
    #endregion
    #region Double
    public double ReadDouble()
    {
        byte[] arr = new byte[8];
        base.Read(arr, 0, arr.Length);
        return BitConverter.ToDouble(arr, 0);
    }
    public void WriteDouble(double value)
    {
        byte[] arr = BitConverter.GetBytes(value);
        base.Write(arr, 0, arr.Length);
    }
    #endregion
    #region Bool
    public bool ReadBool()
    {
        return base.ReadByte() == 1;
    }
    public void WriteBool(bool value)
    {
        base.WriteByte((byte)(value == true ? 1 : 0));
    }
    #endregion
    #region String
    public string ReadString()
    {
        ushort len = this.ReadUShort();
        byte[] arr = new byte[len];
        base.Read(arr, 0, len);
        return Encoding.UTF8.GetString(arr, 0, len);
    }
    public void WriteString(string value)
    {
        byte[] arr = Encoding.UTF8.GetBytes(value);
        if (arr.Length> 65535)
        {
            throw new Exception("长度超出范围");
        }
        this.WriteUShort((ushort)value.Length);
        base.Write(arr, 0, arr.Length);
    }
    #endregion
}

Test测试


using UnityEngine;

public class Test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Item item = new Item();
        item.age = 18;
        item.name = "zzs";
        item.score = 100;
        byte[] arr = null;
        using (MMO_MemoryStream ms = new MMO_MemoryStream())
        {
            ms.WriteInt(item.age);
            ms.WriteString(item.name);
            ms.WriteInt(item.score);
            arr = ms.ToArray();
        }
        for (int i = 0; i < arr.Length; i++)
        {
            Debug.Log(string.Format("{0}:{1}", "arr" + i, arr[i]));
        }
        Item item2 = new Item();
        using (MMO_MemoryStream ms = new MMO_MemoryStream(arr))
        {
            item2.age = ms.ReadInt();
            item2.name = ms.ReadString();
            item2.score = ms.ReadInt();
        }
        Debug.Log(item2.age);
        Debug.Log(item2.name);
        Debug.Log(item2.score);
    }
}
public class Item
{
    public int age;
    public string name;
    public int score;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值