Search Unity

Accessing terrain settings through code

Discussion in 'World Building' started by jessee03, Dec 29, 2020.

  1. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    I'm trying to find a couple of terrain settings through code but have been unable to find anything that has worked yet. Here's my list

    • _terraindata.SetHeight.height ( Unable to find a way to access the brush tool to use the SetHeight and flatten options )
    • Paint Texture -> Create Layer ( So I can assign a base texture when I create my terrain )

    My code so far
    Code (CSharp):
    1.                 //Create Terrain
    2.                 GameObject terrain = new GameObject();
    3.                 TerrainData _terraindata = new TerrainData();
    4.  
    5.                 //Assign terrain settings
    6.                 _terraindata.heightmapResolution = 65;
    7.                 _terraindata.size = new Vector3(1000, 1800, 1000);
    8.  
    9.                 //Apply settings to terrain
    10.                 terrain = Terrain.CreateTerrainGameObject(_terraindata);
     
  2. Sevardos

    Sevardos

    Joined:
    Jan 12, 2018
    Posts:
    8
    setting heights:
    with an array: https://docs.unity3d.com/ScriptReference/TerrainData.SetHeights.html
    with a rendertexture:
    https://docs.unity3d.com/ScriptReference/TerrainData.CopyActiveRenderTextureToHeightmap.html

    painting textures:
    https://docs.unity3d.com/ScriptReference/TerrainData.SetAlphamaps.html
    and setting the terrain layers:
    Code (CSharp):
    1. List <TerrainLayer> tl = new List <TerrainLayer>();
    2. //the layer must be available in the Assets/Resources/ folder
    3. tl.Add(Resources.Load<TerrainLayer>("TerrainLayer/Water"));
    4. tl.Add(Resources.Load<TerrainLayer>("TerrainLayer/Grass"));
    5. terrain.terrainData.terrainLayers = tl.ToArray();
    6.  
     
    wyattt_ and jessee03 like this.
  3. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    Thanks for the help! Managed to get texture layers adding but have a slight issue with the heights still. Managed to get it working but it only applies the height to a corner of my terrain. Do i need a for loop for each part of my terrain? Or is there a way to apply it to the entire terrain?

    Code (CSharp):
    1.                 //Set terrain height
    2.                 float[,] heights = new float[65, 65];
    3.                 heights[x, y] = 0.25f; //Sets height
    4.                 _terraindata.SetHeights(0,0, heights);
     
  4. Sevardos

    Sevardos

    Joined:
    Jan 12, 2018
    Posts:
    8
    each entry in the heights array corresponds to one point on the terrain. So in your code you will have all at zero except one at x,y which will be at 0.25.
    You need to set all values of the array to the correct height.

    If you want to change all heights of the complete terrain by the same value you could just change the transform of the gameObject. This would be a lot faster.