在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如下:
u3d使用c#实现的flash as3中ByteArray字节数组的常用的API,比较特殊的比如readObject这是AMF的,暂时没用到,所以未封装进去。
使用中,如有什么问题,可以在下面给我留言。
默认对应的是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中用C#封装一个类似AS3的ByteArray类(二)--升级版
- Unity3D开发和总结:用C#封装类似苹果NSNotificationCenter消息通信机制(KVO)
- C#一个封装的加密解密类
- C#一个封装的加密解密类
- [C#] 自己封装的一个数据库访问类 让ado.net 用起来和ado一样
- C#一个封装的加密解密类
- [C#] 自己封装的一个数据库访问类 让ado.net 用起来和ado一样
- 如何用c#来制作一个activeX控件或类似的东西
- 一个C#版的类似迅雷下载显示控件
- C#封装的一个串口操作类(可用于Win CE)
- 如何用c#来制作一个activeX控件或类似的东西
- 用C#创建一个封装Google Map API的GoogleMap Web服务器控件(二)
- 收藏——在C#使用SHGetFileInfo获取(管理)文件或者文件夹图标(C#封装Win32函数的一个例子)
- C#中一个类似inputBox的输入框(转)
- C#封装的一个串口操作类(可用于Win CE)
- [C#] 自己封装的一个数据库访问类 让ado.net 用起来和ado一样
- C#一个封装的加密解密类
- 写了一个ASP.NET + C# + xmlhttp做的无刷新Tree(类似CSDN)
- C#中一个操作数据库的类的封装,以前写的,被同学改过,很不完善
- [C#] 自己封装的一个数据库访问类 让ado.net 用起来和ado一样