纳金网

标题: 将服务器发过来的数据解析成事先预定的结构体 [打印本页]

作者: 刀锋狼    时间: 2015-1-29 00:15
标题: 将服务器发过来的数据解析成事先预定的结构体
首先本地要与服务器发送的数据的组织结构一样

csharpcode:
  1. /// <summary>
  2. /// 装备基础信息
  3. /// </summary>
  4. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  5. public struct EquipStruct
  6. {
  7.         public int EquipID;

  8.         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
  9.         public byte[] EquipName;

  10.         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
  11.         public byte[] EquipIntroduce;

  12.         public int EquipEvolveMaxLevel; //强化次数上限
  13.        
  14.         public int EquipReformMaxLevel;//改造次数上限

  15.         public int EquipType;//装备类型

  16.         public int EquipQuality; //品质

  17.         public int EquipPower; //破坏力

  18.         public int EquipShotSpeed; //射速

  19.         public int EquipEnergyDown; //能耗
  20.        
  21.         public int EquipHitRateValue; //命中率

  22.         public int EquipMaxSpeed; //最快速度
  23.        
  24.         public int EquipAcceleration; //加速度
  25.        
  26.         public int EquipHideValue; //自动规避值
  27.        
  28.         public int EquipHoldValue; //姿态维持值

  29.         public int EquipDefense; //防御
  30.        
  31.         public int EquipRecover; //回复
  32.        
  33.         public int EquipRangeValue; //拾取范围
  34.        
  35.         public int EquipRecoverRate; //通行

  36.         public int EquipSuitID;  //套装ID

  37.         public int EquipDurabilityMaxValue; //耐久度上限

  38.         public int EquipPrice; //售价
  39.        
  40.         public int EquipSellPrice; //卖出价
  41. }
复制代码

将byte数组解析成对应的结构体


csharpcode:
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Runtime.InteropServices;
  4. using System;

  5. public class BasicDateMagner {
  6.        
  7.        
  8.         /// <summary>
  9.         /// 结构体转化成字节数组
  10.         /// </summary>
  11.         /// <returns>字节数组</returns>
  12.         /// <param name="obj">结构体对象</param>
  13.         static public byte[] StructToBytes(object obj)
  14.         {
  15.                 int size = Marshal.SizeOf (obj);
  16.                
  17.                 byte[] bytes = new byte[size];
  18.                
  19.                 //分配结构体大小的内存空间
  20.                 IntPtr structPtr = Marshal.AllocHGlobal (size);
  21.                
  22.                 //将结构体拷到分配好的内存空间
  23.                 Marshal.StructureToPtr (obj,structPtr,false);
  24.                
  25.                 //从内存空间拷到byte数组
  26.                 Marshal.Copy (structPtr,bytes,0,size);
  27.                
  28.                 //释放内存空间
  29.                 Marshal.FreeHGlobal(structPtr);
  30.                
  31.                 return bytes;
  32.         }
  33.        
  34.        
  35.         /// <summary>
  36.         /// 字节数组转化成结构体
  37.         /// </summary>
  38.         /// <returns>结构体</returns>
  39.         /// <param name="bytes">字节数组</param>
  40.         /// <param name="type">结构体类型</param>
  41.         static public object BytesToStruct(byte[] bytes, Type type)
  42.         {
  43.                 //得到结构的大小
  44.                 int size = Marshal.SizeOf (type);
  45.                 if(size > bytes.Length)
  46.                 {
  47.                         return null;
  48.                 }
  49.                
  50.                 //分配结构大小的内存空间
  51.                 IntPtr  structPtr = Marshal.AllocHGlobal(size);
  52.                 //将byte数组拷到分配好的内存空间
  53.                 Marshal.Copy(bytes,0,structPtr,size);
  54.                 //将内存空间转换为目标结构
  55.                 object obj = Marshal.PtrToStructure (structPtr,type);
  56.                 //将内存空间转换为目标结构
  57.                 Marshal.FreeHGlobal (structPtr);
  58.                
  59.                 return obj;
  60.         }
  61.        
  62. }
复制代码

作者: huhumark    时间: 2015-1-29 08:06
真强大,早3点就发帖子




欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5