- Home /
Changes made to terrain alpha maps are persistant.
Hi!
When I make changes to the terrains textures during runtime by changing the alphamaps, the changes are permanently saved onto the terrain (in the scene, not(!) only during runtime). I can't image that this is supposed to work this way, but anyway, whether it's a bug or a "feature", is there a way that changes made to the terrains textures are reverted at the end of runtime?
Thanks in advance for any help!
EDIT:
Here's the code, which simply retrieves the alphamap for a specific position and re-weights the first texture to 100%. The Flush in the end doesn't seem to do anything, maybe deprecated!?
Terrain terrain = GameObject.Find("Terrain").GetComponent<Terrain>();
TerrainData terrainData = terrain.terrainData;
Vector3 terrainPos = terrain.transform.position;
// calculate which splat map cell the worldPos falls within (ignoring y)
int mapX = (int)(((worldPos.x - terrainPos.x) / terrainData.size.x) * terrainData.alphamapWidth);
int mapZ = (int)(((worldPos.z - terrainPos.z) / terrainData.size.z) * terrainData.alphamapHeight);
// get the splat data for this cell as a 1x1xN 3d array (where N = number of textures)
float[, ,] splatmapData = terrainData.GetAlphamaps(mapX, mapZ, 1, 1);
splatmapData[0, 0, 0] = 1;
splatmapData[0, 0, 1] = 0;
splatmapData[0, 0, 2] = 0;
splatmapData[0, 0, 3] = 0;
terrainData.SetAlphamaps(mapX, mapZ, splatmapData);
terrain.Flush();
Answer by Kirbyrawr · Jun 28, 2013 at 05:23 PM
You can do a script for putting the same textures in the terrain when you press stop.
I thought about this for a quick workaround as well, but it's not really a long term solution. If the user forces the game to quit abruptly, or it crashes, the script won't be executed.
I don't know for sure, but i haven't seen any docs on it, and tho it can suck it doesn't necessarily surprise me. I thought you were doing this another way (decals). There are other solutions, one save all original data and restore at game start (probably easiest way), or don't mess with the terrain, use decals. (CryEngine for example projects decals onto an area, relatively expensive (adds a few draw calls) but can be removed anytime, and essentially what you are trying to do i believe). If that's not possible in unity, i know you could generate a mesh from the part of the ground affected and slap a texture on it. This would be deleted for certain at game start and is more flexible (i.e. you can remove it during runtime if ever necessary)
Interesting ideas. Having a duplicate of the data is how I would except it to work. The terrain SHOULD create a temporary copy at game start which is used and edited during runtime, and simply overwritten by the original data on the next game start. Doing this manually should work, but still feels like a huge hack. Wondering if they are planning to do something about that.
It'd be nice, I'd like to know as well for certain. I haven't been using unity more than a couple of months, I notice this before for something that didn't restore, but i couldn't say what atm.
You may want to opt for the decal system, I was messing with GL stuff today and you can project textures this way relatively cheaply (as in, a single draw call kind of cheap), and onto more than just terrain surfaces. GL is quite easy for this, and may be a good solution for what you need.
Your answer
![](https://koobas.hobune.stream/wayback/20220613105040im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Modifying terrain texture not updating 1 Answer
Terrain in an RTS style game 1 Answer
Why is directional light screwing up my textures? 2 Answers
Generate Alpha Map Procedurally... 1 Answer