x


change terrain texture and tree at runtime

hello, I checked the documentation but I have not found anything that I serve. how do I change at runtime the texture of the grass and the trees of the terrain?

more ▼

asked Jul 17 '12 at 05:44 PM

leonida gravatar image

leonida
2 5 8 11

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

edit 19.07.2012

use this script. to see how it works do the following:

  1. make a terrain and paint it with 3 any textures
  2. add this script to any GameObject
  3. assign terrain to script (Terrain field)
  4. run the game
  5. press 'space' button and check that on button pressing textures 1 and 2 are changing
  6. read script's comments for more details
using UnityEngine;
using System.Collections;
public class TerrainTextureChanger : MonoBehaviour
{
    public Terrain terrain;
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //switch all painted in texture 1 to texture 2
            UpdateTerrainTexture(terrain.terrainData, 1, 2);
        }
        if (Input.GetKeyUp(KeyCode.Space))
        {
            //switch all painted in texture 2 to texture 1
            UpdateTerrainTexture(terrain.terrainData, 2, 1);
        }
    }
    static void UpdateTerrainTexture(TerrainData terrainData, int textureNumberFrom, int textureNumberTo)
    {
        //get current paint mask
        float[, ,] alphas = terrainData.GetAlphamaps(0, 0, terrainData.alphamapWidth, terrainData.alphamapHeight);
        // make sure every grid on the terrain is modified
        for (int i = 0; i < terrainData.alphamapWidth; i++)
        {
            for (int j = 0; j < terrainData.alphamapHeight; j++)
            {
                //for each point of mask do:
                //paint all from old texture to new texture (saving already painted in new texture)
                alphas[i, j, textureNumberTo] = Mathf.Max(alphas[i, j, textureNumberFrom], alphas[i, j, textureNumberTo]);
                //set old texture mask to zero
                alphas[i, j, textureNumberFrom] = 0f;
            }
        }
        // apply the new alpha
        terrainData.SetAlphamaps(0, 0, alphas);
    }
}

wrong answer below!

http://docs.unity3d.com/Documentation/ScriptReference/Terrain.html

look for

Terrain.renderer.material.SetTexture

should work. write here if you stuck somewhere on this way 8)

more ▼

answered Jul 17 '12 at 07:23 PM

ScroodgeM gravatar image

ScroodgeM
7.6k 2 6

thanks for the reply .. unfortunately does not work, it returns this error:

An instance of type 'UnityEngine.Component' is required to access non static member 'renderer'.

this is my code

var TextureIn: Texture; function Update(){

Terrain.renderer.material.SetTexture = TextureIn;

}

or

var TextureIn: Texture; function Update(){

Terrain.renderer.material.maintexture = TextureIn;

}

Jul 19 '12 at 08:07 AM leonida

"Terrain" word must be replaced with a link to your instance of Terrain

Jul 19 '12 at 08:18 AM ScroodgeM

oops I forgot this line

var Terrain: GameObject;

but still does not work

  There is no 'Renderer' attached to the "Terrain" game object, but a script is trying to access it.

Jul 19 '12 at 08:41 AM leonida

i edited the answer. check it again.

Jul 19 '12 at 06:09 PM ScroodgeM

I Had the same issue, and this code works fine for me! Thanks!!

Sep 15 '12 at 01:37 AM tadeu
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2207
x1478
x380
x374

asked: Jul 17 '12 at 05:44 PM

Seen: 1955 times

Last Updated: Sep 15 '12 at 01:37 AM