您的位置:首页 > 移动开发 > Unity3D

在Unity3D中用C#封装一个类似AS3的ByteArray类

2017-06-19 16:58 309 查看
由于习惯了AS3的ByteArray类。在操作Unity3d时,就想起搞这个小玩意,C#的实现现在分享给需要的享用!

默认对应的是bytes.endian=Endian.LITTLE_ENDIAN;

ByteArray在u3d中使用方法和flash as3一样。

//test=================ByteArray=======================
ByteArray bytes=new ByteArray();
bytes.WriteBoolean(true);
bytes.WriteByte(6);
bytes.WriteDouble(3.14159);
bytes.WriteFloat(777.888f);
bytes.WriteInt(65535);
bytes.WriteShort(15);
bytes.WriteUTF("我是中国人");

bytes.Position=0;
Debug.Log(bytes.ReadBoolean());
Debug.Log(bytes.ReadByte());
Debug.Log(bytes.ReadDouble());
Debug.Log(bytes.ReadFloat());
Debug.Log(bytes.ReadInt());
Debug.Log(bytes.ReadShort());
Debug.Log(bytes.ReadUTF());

写出二进制文件后,在AS3中的读取:

bytes.endian=Endian.LITTLE_ENDIAN;
trace(bytes.readBoolean());
trace(bytes.readByte());
trace(bytes.readDouble());
trace(bytes.readFloat());
trace(bytes.readInt());
trace(bytes.readShort());
trace(bytes.readUTF());
trace(bytes.bytesAvailable);

unity3d中新建C#代码类ByteArray如下:

public class ByteArray
{
private MemoryStream _memoryStream;

private const byte BooleanFalse = 2;
private const byte BooleanTrue = 3;

public ByteArray ()
{
_memoryStream = new MemoryStream ();
}

public ByteArray (MemoryStream ms)
{
_memoryStream = ms;
}

public ByteArray (byte[] buffer)
{
_memoryStream = new MemoryStream ();
_memoryStream.Write (buffer, 0, buffer.Length);
_memoryStream.Position = 0;
}
public void dispose(){
if (_memoryStream != null) {
_memoryStream.Close ();
_memoryStream.Dispose ();
}
_memoryStream = null;
}

public uint Length
{
get
{
return (uint)_memoryStream.Length;
}
}

public uint Position
{
get { return (uint)_memoryStream.Position; }
set{ _memoryStream.Position = value; }
}

public uint BytesAvailable
{
get { return Length - Position; }
}

public byte[] GetBuffer ()
{
return _memoryStream.GetBuffer ();
}

public byte[] ToArray ()
{
return _memoryStream.ToArray ();
}

public MemoryStream MemoryStream{
get {
return _memoryStream;
}
}

public bool ReadBoolean ()
{
return _memoryStream.ReadByte()==BooleanTrue;
}

public byte ReadByte ()
{
return (byte)_memoryStream.ReadByte ();
}

public void ReadBytes (byte[] bytes, uint offset, uint length)
{
_memoryStream.Read (bytes, (int)offset, (int)length);
}

public void ReadBytes (ByteArray bytes, uint offset, uint length)
{
uint tmp = bytes.Position;
int count = (int)(length != 0 ? length : BytesAvailable);
for (int i = 0; i < count; i++)
{
bytes._memoryStream.Position = i + offset;
bytes._memoryStream.WriteByte (ReadByte ());
}
bytes.Position = tmp;
}

private byte[] priReadBytes (uint c)
{
byte[] a = new byte[c];
for (uint i = 0; i < c; i++)
{
a [i] = (byte)_memoryStream.ReadByte ();
}
return a;
}

public double ReadDouble ()
{
byte[] bytes = priReadBytes (8);
byte[] reverse = new byte[8];
//Grab the bytes in reverse order
for (int i = 7, j = 0; i >= 0; i--, j++)
{
reverse [j] = bytes [i];
}
double value = System.BitConverter.ToDouble (reverse, 0);
return value;
}

public float ReadFloat ()
{
byte[] bytes = priReadBytes (4);
byte[] invertedBytes = new byte[4];
//Grab the bytes in reverse order from the backwards index
for (int i = 3, j = 0; i >= 0; i--, j++)
{
invertedBytes [j] = bytes [i];
}
float value = System.BitConverter.ToSingle (invertedBytes, 0);
return value;
}

public int ReadInt ()
{
byte[] bytes = priReadBytes (4);
int value = (int)((bytes [0] << 24) | (bytes [1] << 16) | (bytes [2] << 8) | bytes [3]);
return value;
}

public short ReadShort ()
{
byte[] bytes = priReadBytes (2);
return (short)((bytes [0] << 8) | bytes [1]);
}

public string ReadUTF ()
{
byte[] bytes = priReadBytes (2);
uint length =  (ushort)(((bytes [0] & 0xff) << 8) | (bytes [1] & 0xff));
return ReadUTFBytes (length);
}

public string ReadUTFBytes (uint length)
{
if (length == 0)
return string.Empty;
UTF8Encoding utf8 = new UTF8Encoding (false, true);
byte[] encodedBytes = priReadBytes ((uint)length);
string decodedString = utf8.GetString (encodedBytes, 0, encodedBytes.Length);
return decodedString;
}

//=========================================

public void WriteBoolean (bool value)
{
WriteByte ((byte)(value ? BooleanTrue : BooleanFalse));
}

public void WriteByte (byte value)
{
_memoryStream.WriteByte (value);
}

public void WriteBytes (byte[] bytes, int offset, int length)
{
for (int i = offset; i < offset + length; i++)
_memoryStream.WriteByte (bytes [i]);
}

public void WriteDouble (double value)
{
byte[] bytes = System.BitConverter.GetBytes (value);
WriteBigEndian (bytes);
}

private void WriteBigEndian (byte[] bytes)
{
if (bytes == null)
return;
for (int i = bytes.Length - 1; i >= 0; i--)
{
WriteByte (bytes [i]);
}
}

public void WriteFloat (float value)
{
byte[] bytes = System.BitConverter.GetBytes (value);
WriteBigEndian (bytes);
}

public void WriteInt (int value)
{
WriteInt32 (value);
}

private void WriteInt32 (int value)
{
byte[] bytes = System.BitConverter.GetBytes (value);
WriteBigEndian (bytes);
}

public void WriteShort (short value)
{
byte[] bytes = System.BitConverter.GetBytes ((ushort)value);
WriteBigEndian (bytes);
}

public void WriteUTF (string value)
{
UTF8Encoding utf8Encoding = new UTF8Encoding ();
int byteCount = utf8Encoding.GetByteCount (value);
byte[] buffer = utf8Encoding.GetBytes (value);
this.WriteShort ((short)byteCount);
if (buffer != null && buffer.Length > 0)
{
this.WriteBytes (buffer, 0, buffer.Length);
}
}
}


u3d使用c#实现的flash as3中ByteArray字节数组的常用的API,比较特殊的比如readObject这是AMF的,暂时没用到,所以未封装进去。

使用中,如有什么问题,可以在下面给我留言。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity3d as3 c# ByteArray