我的游戏允许用户在运行时修改地形,但现在我需要保存所述地形。我尝试将地形的高度图直接保存到一个文件中,但是为这个 513x513 高度图编写几乎需要两分钟。
什么是解决这个问题的好方法?有什么方法可以优化写入速度,还是我以错误的方式接近这个?
public static void Save(string pathraw, TerrainData terrain)
{
//Get full directory to save to
System.IO.FileInfo path = new System.IO.FileInfo(Application.persistentDataPath + "/" + pathraw);
path.Directory.Create();
System.IO.File.Delete(path.FullName);
Debug.Log(path);
//Get the width and height of the heightmap, and the heights of the terrain
int w = terrain.heightmapWidth;
int h = terrain.heightmapHeight;
float[,] tData = terrain.GetHeights(0, 0, w, h);
//Write the heights of the terrain to a file
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
//Mathf.Round is to round up the floats to decrease file size, where something like 5.2362534 becomes 5.24
System.IO.File.AppendAllText(path.FullName, (Mathf.Round(tData[x, y] * 100) / 100) + ";");
}
}
}
作为旁注,Mathf.Round 似乎并没有过多地影响节省时间,如果有的话。