Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Resolved prevent runtime Terrain editing to change terrain file permanent on disk

Discussion in 'Scripting' started by ClaudiaTheDev, Feb 28, 2024.

  1. ClaudiaTheDev

    ClaudiaTheDev

    Joined:
    Jan 28, 2018
    Posts:
    331
    I edit the terrain details(grass+flowers+co) via runtime.

    (Use case: player can build stuff+place objects dynamic and i remove grass under specific objects).

    Here is how i do it in code (abstract example):

    Code (CSharp):
    1. terrain.terrainData.SetDetailLayer(0, 0, 0, details)
    What happens is that the terrain data is permanent (not onyl runtime!) changed and the terrain file is also marked as changed in src control (as expected).

    I found a "solution" with using 2 terrains: an "original"/"preset" never changed which detail data is copied into the changed one as preset, so i dont lose the initial detail values after changing them.

    But it's annoying the terrain changes permanent and the game also writes then to the file system which i want to avoid especially for consoles.

    Is there a way to do this in runtime only? Maybe i can create a copy of the preset terrain so it only exists in memory in runtime?

    Does someone know a way?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    This will only happen in editor. In built games the assets are immutable on disk, but their in-memory representations might still be changeable.

    That's what you want. The TerrainData structure is complicated to fully clone and there are some open source things out there to help you. I cloned TerrainData in my destructible terrain example but I forget what all I copied exactly. It certainly wasn't 100% of the TerrainData contents, like I didn't do anything with trees or details.

    My terrain damager in MakeGeo... look for the TerrainDataCloner class.

    https://forum.unity.com/threads/simple-runtime-terrain-editor.502650/#post-5859157
     
    ClaudiaTheDev likes this.
  3. ClaudiaTheDev

    ClaudiaTheDev

    Joined:
    Jan 28, 2018
    Posts:
    331
    Thank you!

    When it's only in editor i can live with it:) rather than implementing a complex solution which maybe cause unexpected bugs.

    I tested i now for PC and the terrain is changeable at runtime in the build version and works like expected. I went trough all game file change dates and it indeed also didn't change a file so the files seem to be immutable like you said.

    I need to test on consoles if it also works like expected (changebale in runtime but does not change game files on disk). When it works, i will probably just swallow the pill and undo every time the terrain in src control after play testing in editor.