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?

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 :wink: