1

我的游戏允许用户在运行时修改地形,但现在我需要保存所述地形。我尝试将地形的高度图直接保存到一个文件中,但是为这个 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 似乎并没有过多地影响节省时间,如果有的话。

4

1 回答 1

2

您正在进行许多小的单独文件 IO 调用。文件 IO 总是耗时且昂贵,因为它包含打开文件、写入文件、保存文件和关闭文件。


相反,我宁愿使用例如 a 生成完整的字符串StringBuilder,这也比使用类似的东西更有效

var someString for(...) { someString += "xyz" }

因为后者总是分配一个新的string.

然后使用例如FileStreamStringWriter.WriteAsync(string)写入异步。

也宁愿使用Path.Combine而不是直接连接字符串 via /Path.Combine根据所使用的操作系统自动使用正确的连接器。

如果目录已经存在,而不是FileInfo.Directory.Create使用Directory.CreateDirectorywhich 不会引发异常。

就像是

using System.IO;

...

public static void Save(string pathraw, TerrainData terrain)
{
    //Get full directory to save to
    var filePath = Path.Combine(Application.persistentDataPath, pathraw);
    var path = new FileInfo(filePath);
    Directory.CreateDirectory(path.DirectoryName);

    // makes no sense to delete 
    // ... rather simply overwrite the file if exists
    //File.Delete(path.FullName);
    Debug.Log(path);

    //Get the width and height of the heightmap, and the heights of the terrain
    var w = terrain.heightmapWidth;
    var h = terrain.heightmapHeight;
    var tData = terrain.GetHeights(0, 0, w, h);

    // put the string together
    // StringBuilder is more efficient then using
    // someString += "xyz" because latter always allocates a new string
    var stringBuilder = new StringBuilder();
    for (var y = 0; y < h; y++)
    {
        for (var x = 0; x < w; x++)
        {
            //                                                         also add the linebreak if needed
            stringBuilder.Append(Mathf.Round(tData[x, y] * 100) / 100).Append(';').Append('\n');
        }
    }

    using (var file = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write))
    {
        using (var streamWriter = new StreamWriter(file, Encoding.UTF8))
        {
            streamWriter.WriteAsync(stringBuilder.ToString());
        }
    }
}

您可能想要指定数字应如何以某种精度打印,例如

(Mathf.Round(tData[x, y] * 100) / 100).ToString("0.00000000");
于 2019-06-27T17:24:19.050 回答