【发布时间】:2019-06-27 17:04:50
【问题描述】:
我的游戏允许用户在运行时修改地形,但现在我需要保存所述地形。我尝试将地形的高度图直接保存到一个文件中,但是对于这个 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 似乎不会对节省时间产生太大影响,如果有的话。
【问题讨论】:
标签: unity3d terrain unity3d-terrain