/// <summary> /// 把自定义的数据格式转换成Flatbuffer /// </summary> /// <param name="launcherVOList"></param> public static void TransMyDataToFlatBuffer(MyLauncher myLauncher) { FlatBufferBuilder builder = new FlatBufferBuilder(1); List <MyLauncherVO> launcherVOList = myLauncher.LauncherVOList; bool haveLauncherVO = launcherVOList != null && launcherVOList.Count > 0; // LauncherVO Offset <LauncherVO>[] launcherVOs = haveLauncherVO ? new Offset <LauncherVO> [launcherVOList.Count] : null; if (haveLauncherVO) { // 这里使用反序的原因是, 服务器读出来的顺序与我写入的顺序是相反的 // FlatBuffer的内部实现没看明白. 先直接写成反序了 for (int iLauncherVO = launcherVOList.Count - 1; iLauncherVO >= 0; iLauncherVO--) { List <MyLauncherMergeVO> mergeList = launcherVOList[iLauncherVO].LauncherMergeVOList; Offset <LauncherMergeVO>[] mergeVOs = new Offset <LauncherMergeVO> [mergeList.Count]; for (int iMerge = mergeList.Count - 1; iMerge >= 0; iMerge--) { List <MyPointInfoVO> pointList = mergeList[iMerge].PointInfoVOList; // pointInfoVo LauncherMergeVO.StartPointInfoArrayVector(builder, pointList.Count); for (int iPoint = pointList.Count - 1; iPoint >= 0; iPoint--) { Vector3 pos = pointList[iPoint].Position; Vector3 dir = pointList[iPoint].Direction; pointInfoVo.CreatepointInfoVo(builder, pos.x, pos.z, pos.y, dir.z, dir.y, dir.x); } VectorOffset pointVector = builder.EndVector(); // MergeVO LauncherMergeVO.StartLauncherMergeVO(builder); LauncherMergeVO.AddPointType(builder, mergeList[iMerge].PointType); LauncherMergeVO.AddPointInfoArray(builder, pointVector); mergeVOs[iMerge] = LauncherMergeVO.EndLauncherMergeVO(builder); } VectorOffset mergeVOVector = LauncherMergeVO.CreateSortedVectorOfLauncherMergeVO(builder, mergeVOs); // LauncherVO LauncherVO.StartLauncherVO(builder); LauncherVO.AddId(builder, launcherVOList[iLauncherVO].ID); LauncherVO.AddMergeArray(builder, mergeVOVector); launcherVOs[iLauncherVO] = LauncherVO.EndLauncherVO(builder); } } // Launcher VectorOffset launcherVOVector = haveLauncherVO ? LauncherVO.CreateSortedVectorOfLauncherVO(builder, launcherVOs) : new VectorOffset(); Launcher.StartLauncher(builder); if (haveLauncherVO) { Launcher.AddData(builder, launcherVOVector); } Offset <Launcher> launcher = Launcher.EndLauncher(builder); // SpacecraftLauncher SpacecraftLauncher.StartSpacecraftLauncher(builder); SpacecraftLauncher.AddLauncher(builder, launcher); Offset <SpacecraftLauncher> spacecraft = SpacecraftLauncher.EndSpacecraftLauncher(builder); SpacecraftLauncher.FinishSpacecraftLauncherBuffer(builder, spacecraft); // Write To File using (MemoryStream stream = new MemoryStream(builder.DataBuffer.ToFullArray(), builder.DataBuffer.Position, builder.Offset)) { byte[] data = stream.ToArray(); string fileName = Application.dataPath + "\\..\\..\\product\\server\\resource\\flatbuffers\\spacecraft_launcher.bytes"; fileName.Replace("/", "\\"); File.WriteAllBytes(fileName, data); } /*using (MemoryStream stream = new MemoryStream(builder.DataBuffer.ToFullArray(), builder.DataBuffer.Position, builder.Offset)) * { * byte[] data = stream.ToArray(); * string fileName = Application.dataPath + "\\..\\..\\data\\output\\flatBuffers\\server\\data\\spacecraft_launcher.bytes"; * fileName.Replace("/", "\\"); * File.WriteAllBytes(fileName, data); * } */ }