Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Saving and Loading Terrain information at runtime

Discussion in 'World Building' started by Tempus_G, May 2, 2021 at 1:05 PM.

  1. Tempus_G

    Tempus_G

    Joined:
    May 12, 2015
    Posts:
    16
    I am making a game for which I need to be able to edit my terrain and save it...... then load it again later to play on or edit again.
    To that end I am successful.
    My problem is that it takes WAY too long to save and load the terrain data. Namely the splat maps
    All works perfectly, except for the long save and load times.

    The approach that I took is this, with a similar load function.

    Code (CSharp):
    1.    
    2. private void SaveTerrainAlpha(string filename)
    3.    {
    4.        Directory.CreateDirectory(new FileInfo(Path.Combine(Application.persistentDataPath, filename)).DirectoryName);
    5.        float[,,] numArray = terrainData.GetAlphamaps(0, 0, terrainData.alphamapWidth, terrainData.alphamapHeight);
    6.        BinaryWriter writer = new BinaryWriter(new FileStream(Path.Combine(Application.persistentDataPath, filename), FileMode.Create, FileAccess.ReadWrite, FileShare.None));
    7.        int num = 0;
    8.        while (num < terrainData.alphamapWidth)
    9.        {
    10.            int num2 = 0;
    11.            while (true)
    12.            {
    13.                if (num2 >= terrainData.alphamapHeight)
    14.                {
    15.                    num++;
    16.                    break;
    17.                }
    18.                float[] numArray2 = new float[targetTerrain.terrainData.alphamapLayers];
    19.                int index = 0;
    20.                while (true)
    21.                {
    22.                    if (index >= numArray.GetLength(2))
    23.                    {
    24.                        num2++;
    25.                        break;
    26.                    }
    27.                    numArray2[index] = numArray[num, num2, index];
    28.                    writer.Write(numArray[num, num2, index]);
    29.                    index++;
    30.                }
    31.            }
    32.        }
    33.        writer.Close();
    34. }
    35.  

    What I would like to know is if there is a batter way to improve the speed.
    Now the texture sizes will probably have an impact, as well as how many textures are in use. But unless I am willing to suffer on quality quite a bit, that is not going to help much.
     
unityunity