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.
4 Answers
4
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;
system
March 17, 2011, 2:35pm
2
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);</p>
```
terrain.terrainData.splatPrototypes = test;
```
<p>
system
March 30, 2012, 10:11am
4
What is the declaration of test ?
muzboz
December 2, 2018, 8:08am
5
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;
Yep, it works.
– DMCH