- Home /
Adding a texture to a code-generated terrain, through code.
Hey all. I've been struggling with code-generated terrain for now and I'm having a hard time figuring out how to add a pre-made texture, to a code-generated terrain, while the game is running.
I know that I can generate the terrain and then add the texture from the inspector, but how can I do this on the fly when I generate the terrain.
Here is my code at the moment:
TerrainData _terrainData = new TerrainData(); GameObject _terrain = Terrain.CreateTerrainGameObject(_terrainData);
float[,,] splatMapData = _terrainData.GetAlphamaps(0, 0, 1, 1); splatMapData[0, 0, 0] = 1; splatMapData[0,0,1] = 0; splatMapData[0,0,2] = 0; splatMapData[0,0,3] = 0;
Now I know that there's now an array for my textures, but how can I add the texture in there?
Thanks in advance.
Answer by flaviusxvii · Mar 17, 2011 at 03:02 PM
I'm just reformatting the answer from Slayer, I have no idea if it works or not. The formatting was just freaking me out.
To add texture on terrain, simple assign it. For example:
test[0] = new SplatPrototype(); test[0].texture = (Texture2D)Resources.Load("grydirt2",typeof(Texture2D)); test[0].tileOffset = new Vector2(0, 0); test[0].tileSize = new Vector2(15, 15);
test[1] = new SplatPrototype(); test[1].texture = (Texture2D)Resources.Load("Balmoral",typeof(Texture2D)); test[1].tileOffset = new Vector2(0, 0); test[1].tileSize = new Vector2(15, 15); test[1].texture.Apply(true);
test[2] = new SplatPrototype(); test[2].texture = (Texture2D)Resources.Load("grass",typeof(Texture2D)); test[2].tileOffset = new Vector2(0, 0); test[2].tileSize = new Vector2(15, 15); test[2].texture.Apply(true);
terrain.terrainData.splatPrototypes = test;
Thanks for this mate. Following what you wrote, I have my own contribution to this. Wrap this into a single file named 'TerrainExtensions'.
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
public static class TerrainExtensions
{
public static void AddTexture(this Terrain t, Texture2D tex)
{
List<SplatPrototype> protos = t.terrainData.splatPrototypes.ToList();
SplatPrototype newSplatProto = new SplatPrototype();
newSplatProto.texture = tex;
newSplatProto.tileOffset = Vector2.zero;
newSplatProto.tileSize = Vector2.one;
newSplatProto.texture.Apply(true);
protos.Add(newSplatProto);
t.terrainData.splatPrototypes = protos.ToArray();
}
}
Later on, this code allows me to simply write this in my code :
myTerrain.AddTexture(myTex);
WARNINGS:
Your texture must have "Read/Write Enabled" checked in its Import Settings.
As this code modifies the terrain data itself, the textures added to the terrain using this code will remain once you left the Editor/quit the app! To be able to modify a terrain's data in runtime as if it were a "normal" GameObject, please read this topic
Answer by Slayer · Mar 17, 2011 at 02:35 PM
To add texture on terrain, simple assign it. For example: [CODE] test[0] = new SplatPrototype(); test[0].texture = (Texture2D)Resources.Load("grydirt2", typeof(Texture2D)); test[0].tileOffset = new Vector2(0, 0); test[0].tileSize = new Vector2(15, 15); test[1] = new SplatPrototype(); test[1].texture = (Texture2D)Resources.Load("Balmoral", typeof(Texture2D)); test[1].tileOffset = new Vector2(0, 0); test[1].tileSize = new Vector2(15, 15); test[1].texture.Apply(true); test[2] = new SplatPrototype(); test[2].texture = (Texture2D)Resources.Load("grass", typeof(Texture2D)); test[2].tileOffset = new Vector2(0, 0); test[2].tileSize = new Vector2(15, 15); test[2].texture.Apply(true);
terrain.terrainData.splatPrototypes = test;
[/CODE]
Answer by muzboz · Dec 02, 2018 at 08:08 AM
Just trying to reformat the "Best Answer" here, so I can see it properly. :)
QUOTING:
To add texture on terrain, simply assign it. For example:
test[0] = new SplatPrototype();
test[0].texture = (Texture2D)Resources.Load("grydirt2", typeof(Texture2D));
test[0].tileOffset = new Vector2(0, 0);
test[0].tileSize = new Vector2(15, 15);
test[1] = new SplatPrototype();
test[1].texture = (Texture2D)Resources.Load("Balmoral", typeof(Texture2D));
test[1].tileOffset = new Vector2(0, 0);
test[1].tileSize = new Vector2(15, 15);
test[1].texture.Apply(true);
test[2] = new SplatPrototype();
test[2].texture = (Texture2D)Resources.Load("grass", typeof(Texture2D));
test[2].tileOffset = new Vector2(0, 0);
test[2].tileSize = new Vector2(15, 15);
test[2].texture.Apply(true);
terrain.terrainData.splatPrototypes = test;
Your answer
![](https://koobas.hobune.stream/wayback/20220613111708im_/https://answers.unity.com/themes/thub/images/avi.jpg)