- Home /
The question is answered, right answer was accepted
Set generated Texture2D on terrain in unity3d
I created a simple Texture2D in Unity3D are as follows(simplified example):
Texture2D texture2d = new Texture2D(256,256);
for (int h = 0; h < texture2d.height; h++) {
for (int w = 0; w < texture2d.width ; w++) {
texture2d.SetPixel(w,h, Color.green);
}
}
How can I set this created texture2d at a existing terrain, as ground texture?
Answer by _dns_ · Aug 28, 2015 at 02:24 PM
Hi, the terrain object you use contains a reference to a TerrainData object. This TerrainData object contains a table of SplatPrototype (TerrainData.splatPrototypes). Each SplatPrototype can be assigned a texture and some other parameters (the same that are editable in the editor when you assign a texture to a terrain)
To only change a texture, just assign a variable: SplatPrototype[] MySplats = MyTerrain.terraindata.splatPrototypes.
Change the texture: MySplat[0].texture = texture2d.
Assign back MyTerrain.terraindata.splatPrototypes = MySplats;
And don't forget to use Texture.Apply after modifying the texture ;-)
Thanks a lot, it seems to work the biggest problem was the missing Texture.Apply :-) !
Follow this Question
Related Questions
Instantiate a gameObject with a dynamically generated sprite? 0 Answers
SetPixel() on Terrain 0 Answers
Paint Grid Texture 1 Answer
How to smooth out the values? 1 Answer
Terrain Grass Detail Texture Atlas 0 Answers