/// <summary> /// TDData反序列化 /// </summary> private TDData TDDataDeserialize(byte[] buffer) { ProtocolBytes protocol = new ProtocolBytes(buffer); TDData tDData = new TDData { name = protocol.GetString(), terrainPos = protocol.GetVector3(), heightMap = protocol.GetFloatArray2() }; int detailCount = protocol.GetInt32(); for (int i = 0; i < detailCount; i++) { protocol.GetIntArray2(); } tDData.alphaMap = protocol.GetFloatArray3(); int treePrototypeCount = protocol.GetInt32(); Vector3[][] treePoses = new Vector3[treePrototypeCount][]; for (int i = 0; i < treePrototypeCount; i++) { int treeCount = protocol.GetInt32(); Vector3[] poses = new Vector3[treeCount]; for (int j = 0; j < treeCount; j++) { poses[j] = protocol.GetVector3(); } treePoses[i] = poses; } tDData.treePoses = treePoses; return(tDData); }
/// <summary> /// TDData序列化 /// </summary> private byte[] TDDataSerialize(TDData tDData) { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString(tDData.name); protocol.AddVector3(tDData.terrainPos); protocol.AddFloatArray2(tDData.heightMap); protocol.AddInt32(tDData.detailMap.Length); for (int i = 0; i < tDData.detailMap.Length; i++) { protocol.AddIntArray2(tDData.detailMap[i]); } protocol.AddFloatArray3(tDData.alphaMap); protocol.AddInt32(tDData.treePoses.Length); for (int i = 0; i < tDData.treePoses.Length; i++) { protocol.AddInt32(tDData.treePoses[i].Length); for (int j = 0; j < tDData.treePoses[i].Length; j++) { protocol.AddVector3(tDData.treePoses[i][j]); } } return(protocol.Encode()); }
/// <summary> /// 读取一张地图的数据 /// </summary> private void ReadTDData(string fileName) { byte[] data = File.ReadAllBytes(fileName); data = IOUtility.Decompress(data); runedCount += 0.2f; TDData tDData = TDDataDeserialize(data); lock (tDDatas) { tDDatas.Enqueue(tDData); } runedCount += 0.8f; }
/// <summary> /// 保存单个地形数据 /// </summary> private void SaveTerrainData(Terrain terrain, string folderName, string fileName) { TerrainData terrainData = terrain.terrainData; // 新建一个TDData并赋值 TDData tDData = new TDData(); tDData.name = terrain.name; tDData.terrainPos = terrain.GetPosition(); tDData.heightMap = terrainData.GetHeights(0, 0, terrainData.heightmapResolution, terrainData.heightmapResolution); int[][,] detailMap = new int[terrainData.detailPrototypes.Length][, ]; for (int i = 0; i < detailMap.Length; i++) { detailMap[i] = terrainData.GetDetailLayer(0, 0, terrainData.detailResolution, terrainData.detailResolution, i); } tDData.detailMap = detailMap; tDData.alphaMap = terrainData.GetAlphamaps(0, 0, terrainData.alphamapResolution, terrainData.alphamapResolution); Vector3[][] treePoses = new Vector3[terrainData.treePrototypes.Length][]; List <Vector3>[] temp = new List <Vector3> [treePoses.Length]; for (int i = 0; i < treePoses.Length; i++) { temp[i] = new List <Vector3>(); } foreach (var item in terrainData.treeInstances) { temp[item.prototypeIndex].Add(item.position); } for (int i = 0; i < treePoses.Length; i++) { treePoses[i] = temp[i].ToArray(); } tDData.treePoses = treePoses; runedCount += 0.2f; //序列化TDDate并保存 Task.Run(() => { runedCount += 0.2f; byte[] data = null; data = TDDataSerialize(tDData); runedCount += 0.4f; data = IOUtility.Compress(data); File.WriteAllBytes(savePath + "/" + folderName + "/" + fileName + ".terrainData", data); runedCount += 0.2f; }); }
/// <summary> /// 创建单块地图 /// </summary> /// <param name="tdData"></param> private void CreateTerrain(TDData tdData) { // 设置TerrainData的基础参数 TerrainData terrainData = new TerrainData(); terrainData.heightmapResolution = head.ResolutionSize + 1; terrainData.SetDetailResolution(head.ResolutionSize, 8); terrainData.alphamapResolution = head.ResolutionSize * 2; terrainData.baseMapResolution = head.ResolutionSize; terrainData.size = head.terrainSize; terrainData.detailPrototypes = details; terrainData.treePrototypes = trees; terrainData.terrainLayers = splats; terrainData.RefreshPrototypes(); // 高度,贴图,细节设置 if (tdData.heightMap != null) { terrainData.SetHeights(0, 0, tdData.heightMap); } if (tdData.detailMap != null) { for (int i = 0; i < tdData.detailMap.Length; i++) { terrainData.SetDetailLayer(0, 0, i, tdData.detailMap[i]); } } if (tdData.alphaMap != null) { terrainData.SetAlphamaps(0, 0, tdData.alphaMap); } // 在场景中创建Terrain实体并设置实体的参数 GameObject newTerrainGameObject = Terrain.CreateTerrainGameObject(terrainData); newTerrainGameObject.name = terrainData.name; newTerrainGameObject.isStatic = false; newTerrainGameObject.transform.position = tdData.terrainPos; newTerrainGameObject.layer = LayerMask.NameToLayer("Terrain"); // 设置Terrain类的参数 Terrain terrain = newTerrainGameObject.GetComponent <Terrain>(); terrain.heightmapPixelError = head.heightmapPixelError; terrain.basemapDistance = head.basemapDistance; terrain.drawHeightmap = head.drawHeightmap; terrain.name = tdData.name; if (tdData.treePoses != null) { // 为地形添加树木实体 for (int index = 0; index < tdData.treePoses.Length; index++) { for (int i = 0; i < tdData.treePoses[index].Length; i++) { TreeInstance instance = TerrainUtility.GetTreeInstance(index); instance.position = tdData.treePoses[index][i]; terrain.AddTreeInstance(instance); } } } newTerrainGameObject.transform.SetParent(terrainParent); }
private void InitGridTypeLibs(string girdWeights) { GridTypes gridTypes = mCore.GridTypes; gridTypes.NormalGridType = m_NormalGridType; TDData td = DataParser.ParseParamToTD(ref girdWeights); int weightCount = td.Source.Count; KeyValueList <int, int> weightMapper = new KeyValueList <int, int>(); for (int i = 0; i < weightCount; i++) { int gridType = td.GetIntValue(i, 0); int weight = td.GetIntValue(i, 1); weightMapper[gridType] = weight;//定义权重 } GridTypeItem value; int max = m_GridTypeItems.Count, key; for (int i = 0; i < max; i++) { value = m_GridTypeItems[i]; key = value.id; string abName = value.prefabABName; string assetName = value.prefabResName; GameObject creater() { AssetBundles abs = Framework.Instance.GetUnit <AssetBundles>(Framework.UNIT_AB); GameObject result = abs.Get <GameObject>(abName, assetName); result = Instantiate(result); return(result); }; if (weightCount > 0) { int weight = 1; if (key == gridTypes.NormalGridType) { weight = weightCount > 0 ? 0 : 1;//如果包含消除格类型的权重配置则不使用自动的普通消除格类型值 gridTypes.SetGridCreater(key, value.isSpriteAsset, creater, weight); "log:Grid type is {0}, ab name is {1}, res name is {2}, asset static is ({3})".Log(key.ToString(), abName, assetName, value.isSpriteAsset.ToString()); int m = weightCount; for (int n = 0; n < m; n++) { key = weightMapper.Keys[n]; weight = weightMapper[key]; gridTypes.SetGridCreater(key, value.isSpriteAsset, creater, weight, gridTypes.NormalGridType);//根据权重配置向全局类型库添加普通消除格类型 "log:Grid type is {0}, ab name is {1}, res name is {2}, asset static is ({3})".Log(key.ToString(), abName, assetName, value.isSpriteAsset.ToString()); } } else { gridTypes.SetGridCreater(key, value.isSpriteAsset, creater, 0); } } else { int weight = value.isInvalidRandom ? 0 : 1; gridTypes.SetGridCreater(key, value.isSpriteAsset, creater, weight); //gridTypes.AddInvalidWeightGrid(0); //gridTypes.AddInvalidWeightGrid(2); "log:Grid type is {0}, ab name is {1}, res name is {2}, asset static is ({3})".Log(key.ToString(), abName, assetName, value.isSpriteAsset.ToString()); } } }