Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by leonida · Jul 17, 2012 at 05:44 PM · textureterrainruntimechange

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by ScroodgeM · Jul 17, 2012 at 07:23 PM

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)

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image leonida · Jul 19, 2012 at 08:07 AM 0
Share

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;

}

avatar image ScroodgeM · Jul 19, 2012 at 08:18 AM 0
Share

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

avatar image leonida · Jul 19, 2012 at 08:41 AM 0
Share

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.

avatar image ScroodgeM · Jul 19, 2012 at 06:09 PM 0
Share

i edited the answer. check it again.

avatar image tadeu · Sep 15, 2012 at 01:37 AM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Texture change on multi material character 1 Answer

Change and add Terrain Texture During Runtime 1 Answer

How to change texture on parts of terrain 3 Answers

Texturing terrain at runtime 1 Answer

Changing the color/look of a grass textures/grass. Making it look clean. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges