public static void Save(MapHeightInfos info, string fullPath) { float[] heights = info.HeightInEngineOrder(); using (BinaryWriter writer = new BinaryWriter(File.Open(fullPath, FileMode.OpenOrCreate))) { foreach (float v in heights) { writer.Write(v); } } Debug.Log(string.Format(".hmap saved. {0}", fullPath)); }
Vector3[][] CalPosition(MapHeightInfos info, Vector3 offset) { xCount = info.heights.Length; zCount = xCount != 0 ? info.heights[0].Length : 0; Vector3[][] result = new Vector3[xCount][]; float xStep = info.terrainSize.x / (xCount - 1); float zStep = info.terrainSize.z / (zCount - 1); for (int i = 0; i < xCount; i++) { result[i] = new Vector3[zCount]; } for (int z = 0; z < zCount; z++) { for (int x = 0; x < xCount; x++) { result[x][z] = new Vector3(x * xStep, info.heights[x][z], z * zStep) + offset; } } return result; }
public void Load(string levelName) { string path = GetSavePath(levelName); string fileName = GetSaveFileName(levelName); string fullPath = System.IO.Path.Combine(path, fileName); if (!System.IO.File.Exists(fullPath)) { Debug.LogError(string.Format("File not exists. {0}", fullPath)); return; } try { heightInfos = ChpLibXML.Load<MapHeightInfos>(fullPath); } catch { Debug.LogError("Load File Fail."); return; } reachableInfo = new MapReachableInfos(heightInfos.reachables); navigableInfo = new MapReachableInfos(heightInfos.navigable); frees = locType == SampleLocatorType.Reachable ? reachableInfo.reachables : navigableInfo.reachables; hitPositions = CalPosition(heightInfos, Terrain.activeTerrain.GetPosition()); centerPositions = CenterPositionsFromHitPositions(hitPositions); sampleSize = heightInfos.sampleSize; Debug.Log(string.Format("File loaded. {0}", fullPath)); }
public void Save(string levelName, string fullPath = "") { string hFullPath; if (string.IsNullOrEmpty(fullPath)) { string path = GetSavePath(levelName); string fileName = GetSaveFileName(levelName); fullPath = System.IO.Path.Combine(path, fileName); hFullPath = System.IO.Path.Combine(path, GetHFileName(levelName)); } else { Debug.LogError("empty fullPath"); return; } if (reachableInfo == null || reachableInfo.reachables.Length == 0) { Debug.LogWarning("Reachable has not be set."); return; } if (navigableInfo == null || navigableInfo.reachables.Length == 0) { Debug.LogWarning("Navigable has not be set."); return; } float[][] ys = GenYFromLocator(); heightInfos = new MapHeightInfos(Application.loadedLevelName, Terrain.activeTerrain.terrainData.size , sampleSize, ys, reachableInfo.reachables, navigableInfo.reachables, null); ChpLibXML.Save<MapHeightInfos>(heightInfos, fullPath); HMapSaver.Save(heightInfos, hFullPath); Debug.Log(string.Format("File saved. {0}", fullPath)); }
public void Load(string levelName, bool skipHeights = false) { string path = GetSavePath(levelName); string fileName = GetSaveFileName(levelName); string fullPath = System.IO.Path.Combine(path, fileName); if (!System.IO.File.Exists(fullPath)) { Debug.LogError(string.Format("File not exists. {0}", fullPath)); return; } float[][] oriHeights = null ; if (skipHeights) { if (hitPositions != null) { oriHeights = new float[hitPositions.Length][]; for ( int i = 0; i < hitPositions.Length; i ++){ oriHeights[i] = new float[hitPositions[i].Length]; } for (int z = 0; z < hitPositions.Length; z++) { for (int x = 0; x < hitPositions[z].Length; x++) { oriHeights[x][z] = hitPositions[x][z].y; } } } else { Debug.Log("先Load,再Sample,最后LoadOnlyReachable"); } } try { heightInfos = ChpLibXML.Load<MapHeightInfos>(fullPath); if (skipHeights) { heightInfos.heights = oriHeights; } } catch { Debug.LogError("Load File Fail."); return; } reachableInfo = new MapReachableInfos(heightInfos.reachables); navigableInfo = new MapReachableInfos(heightInfos.navigable); frees = locType == SampleLocatorType.Reachable ? reachableInfo.reachables : navigableInfo.reachables; hitPositions = CalPosition(heightInfos, Terrain.activeTerrain.GetPosition()); centerPositions = CenterPositionsFromHitPositions(hitPositions); sampleSize = heightInfos.sampleSize; isWayPoints = heightInfos.isWayPoints; Debug.Log(string.Format("File loaded. {0}", fullPath)); }