Continuing the delving into terrain - I need to implement seasonal changes to the terrain, and it seems best to be able to switch out the current textures and trees with different ones (say “grass” with “snow” and “summerTree” with “winterTree”). I’ve tried a number of different things but the terrain doesn’t seems to update, even after calling Flush().
Terrain.activeTerrain.terrainData.treePrototypes[0].prefab = GameObject.Find("WinterTree");
Terrain.activeTerrain.terrainData.splatPrototypes[0].texture = Resources.Load("snow", Texture2D);
//I'm not sure if RefreshPrototypes() should be called or not, the terrain doesn't update either way
Terrain.activeTerrain.terrainData.RefreshPrototypes();
//I know this _should_ be called, and it takes a while to run, but the terrain has no obvious change
Terrain.activeTerrain.Flush();
I basically just need to run an asset swap during gameplay, and the change is basically cosmetic (the tree and detail locations aren’t changing, for instance). I’ve trawled the forums and found a fair amount of info on the terrain structure (which has been very handy), but I couldn’t find anything definitive on this particular problem. I’m hoping it’s just a matter of updating the terrain properly in-script, but any advice on how to change around the assets in the scripting would be appreciated as well.
To those who might also be trying this, the changes you make are permanent, unlike other script variables during runtime - I have a big scene that was authored by a separate artist, so I’ll need to be careful I don’t overwrite anything (and rely on source control when I do…). That might seem obvious, but could also come as a surprise if you’re not prepared. I’m going to have to be very careful with name parsing for the next stage…
Hi,
When testing the code above, I got the error below in the second line.
Assets/arvores.cs(25,8): error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject’. An explicit conversion exists (are you missing a cast?)
hello,
Running this code under editor, if I load a prefab in line #2 ( tps[0].prefab=…), ok. If I load an AssetBundle, ok too while running, but when I stop, the editor crashes.
Any idea?
Thanks
I realize this post is very old, but to anyone who may come across this, the changes made are only permanent if you do not cache your original arrays before the changes are made, and revert back to them when the application is ended. For example, here is my code for changing trees in runtime.
using UnityEngine;
using System.Collections;
public class TerrainTreeChanger : MonoBehaviour {
private TreePrototype[ ] cache; //This is where your original trees are going to be saved!
TreePrototype[ ] treeType;
public GameObject swapwith;
void Start()
{
cache = Terrain.activeTerrain.terrainData.treePrototypes;//IMPORTANT, if this cache is not assigned to the original array, then your changes will be permanent.
treeType = Terrain.activeTerrain.terrainData.treePrototypes;
}
void OnApplicationQuit()
{
Terrain.activeTerrain.terrainData.treePrototypes = cache;//Revert to the original array when the application ends. Or simply call this function when you switch scenes.
}