- Home /
Saving game data and not user data
I'm currently using the Application.persistentDataPath (with BinaryFormatter) in order to save some data. The problem is, this data is NOT user data (like highscores etc), it is data that is actually needed for all my scenes. (Basically, i'm just saving the terrain details (terrain.GetDetailLayer), i'm saving it because these details may be removed while playing the level, and so i must bring the details to its initial state once the level is repeated. Unity Editor is not capable of doing this (at least that i'm aware of, after removing details in runtime, it doesn't go back to its original state when i stop the game). So, the problem is, according to this page: https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html it says that "The files can still be erased by users directly", and since this data is FUNDAMENTAL to my levels, i cannot let users delete it and i must export this data in the build itself. So how can i save PRIVATE game data, that is fundamental for any of my game levels? Thanks in advance.
Answer by Serge144 · Dec 15, 2019 at 11:06 PM
I guess what i could do is to save these files for each level in some folder (while in development stage), then, when i'm happy with it, i'll paste those files into the Assets folder, which the game will access to bring to the inicial state (Application.dataPath) Any pro that could aprove this? ^
Answer by Bunny83 · Dec 16, 2019 at 12:35 AM
There's no real need to manually store that data in a file. In a built game you can not possible alter the assetfiles. You can alter the loaded assets in memory but the actual serialized / packed files in the build will never change. When the game is restarted all assets will have their state as the game was build. However if you need to reset the state during a single session you should be able to just Instantiate the original TerrainData object and use / alter that one. Of course you should keep a reference to the original asset. That means whenever you start your game you would first instantiate the original TerrainData asset and replace the original with the instance. The easiest way would be a script like this:
public TerrainData data;
public Terrain terrain;
void Start()
{
var inst = Instantiate(data);
terrain.terrainData = inst;
var col = terrain.GetComponent<TerrainCollider>();
if (col != null)
col.terrainData = inst;
}
So you just assign your Terrain object to the terrain variable and your original TerrainData asset to the data variable. Whenever Start is called it will instantiate the original TerrainData asset and replace it in the Terrain script and the TerrainCollider that is attached to the Terrain. This way you never alter your original asset. I barely use Terrains and I have never actually tried this with the TertainData. Maybe just instantiating isn't enough (but it should). If it doesn't work you can essentially do the same thing, but in addition copy over all the individual data in case it's not properly instantiated.
Ok so basically i should do what i was saying, i must have that data in the assets folder in order to restore the details of the terrain. Basically i'll do this: design my level and draw details onto the terrain, then when i'm happy with it, i'll save that in some file, which i will need to put in the assets folder, then in script i tell it to load always from assets folder in order to always have the same details. I didn't quite understand what you said, because first you say that "There's no real need to manually store that data in a file" but then you say "Whenever Start is called it will instantiate the original TerrainData asset and replace it in the Terrain script and the TerrainCollider that is attached to the Terrain.". The first sentence you say there's no need to save, but in the second you're implying that there is some asset file.. so i got a bit confused on how i should do it.
Your answer
Follow this Question
Related Questions
How to get terrain data? 0 Answers
Terrrain Texturing with solid colors,Terrain texturing with solid color tile 0 Answers
Best Practice for loading variable Data for Different Forms of same Prefab 0 Answers
How do you make the material in the Terrain detail prototype properties update after runtime? 1 Answer
How to get a terrain's maximum height 3 Answers