Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 AR_Rizvi · Aug 23, 2013 at 05:10 AM · textureterrain

Can i change the Texture of terrain

Hi Guys i need to change the texture of the terrain by a butt0n press and i want to call seprate textures i know i can do it with predefined terrain tool kit but i want to do it in Editor mode with scripting i am building a script for various functionalities of terrain and i want to add and change texture of terrain as well Nw My Queastion is that how can i do it i dnt knw any thing about maps and how to obtain them and i ave searched but i am not geting the idea of Spalt maps and alpha maps like what r thy and how to use them

P.s i am real confussed plz help

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Ian-McCleary · Aug 23, 2013 at 06:05 AM

I think this would work..there are some things you would need to do to make it ok for your situation but this should be a good outline or start. If im wrong someone please correct me.

 var TerrainOne : Terrain;
 var TerrainTwo :Terrain;
 
 
 function Start () {
 Terrain = TerrainOne;
 }
 
 function Update () {
     if(!Input.GetButtonDown(TerrainTwo)){
     }
         if(Input.GetButtonDown(TerrainOne)){
     }
 }
Comment
Add comment · Show 1 · 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 Ian-McCleary · Aug 23, 2013 at 06:11 AM 0
Share

FYI: i was using Javascript

avatar image
0

Answer by sooncat · Aug 23, 2013 at 09:44 AM

 SplatPrototype[] splats = yourTerrainGameObjct.GetComponent<Terrain>().terrainData.splatPrototypes;
 
 for(int i=0;i<splats.Length;i++)
 {
    splats[i].texture = (Texture2D)yourTexture;
 }
Comment
Add comment · Show 3 · 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 AR_Rizvi · Aug 23, 2013 at 11:06 AM 0
Share

thanks guys fr ur replies i ave tested this code but it giving error that

Assets/_Scripts/TerrainTextureChanger.cs(28,26): error CS1061: Type UnityEngine.SplatPrototype[]' does not contain a definition for count' and no extension method count' of type UnityEngine.SplatPrototype[]' could be found (are you missing a using directive or an assembly reference?)

and what can i use in the place of your texture coz i am using the texture variable but it gives me error

like

Assets/_Scripts/TerrainTextureChanger.cs(30,15): error CS0266: Cannot implicitly convert type UnityEngine.Texture' to UnityEngine.Texture2D'. An explicit conversion exists (are you missing a cast?)

avatar image sooncat · Aug 26, 2013 at 10:23 AM 0
Share

the first error: splats.count ==> splats.Length the second error : splats[i].texture = (Texture2D)yourtexture;

avatar image sooncat · Aug 26, 2013 at 10:26 AM 0
Share

I reEdit the answer. "(Texture2D)" is not necessary if you define "yourTexture" as "Texture2D".

avatar image
0

Answer by AR_Rizvi · Aug 27, 2013 at 09:48 AM

so far i achive this my texture swap is working but i want the whole terrain texture to change at once and the scond function is not working i dnt knw thier are no errors in code but its not working texture swap is only working on the painted areas can i repaint the texture of the terrian throug scripting

here is my code

 using UnityEngine;
 using System.Collections;
 
 public class TerrainTextureChanger : MonoBehaviour {
 
  public Terrain terrain;
  public Texture2D MyTexture;
     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);
         }
         if (Input.GetKeyDown(KeyCode.LeftControl))
         {
             textureChange();
         }
     }
      void textureChange(){
         Debug.Log ("i am hapnig");
     SplatPrototype[] splats = terrain.GetComponent<Terrain>().terrainData.splatPrototypes;
      
     for(int i=0;i<splats.Length;i++)
     {
     splats[i].texture = MyTexture;
 //                (Texture2D)
     }
     }
     
     
     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
         Debug.Log (alphas);
         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);
     }
 }
 
Comment
Add comment · Show 2 · 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 sooncat · Aug 27, 2013 at 10:03 AM 0
Share

What's the purpose of "UpdateTerrainTexture()"?

avatar image AR_Rizvi · Aug 27, 2013 at 10:42 AM 0
Share

it is for swap the texture on painted areas to another texture in the terrain texture slot

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

17 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Modifying Terrain Splat Texture at Runtime 2 Answers

How can I perform some action based on the terrain texture currently under my player? 4 Answers

How to automatically apply different textures on terrain based on height? 5 Answers

How can I automatically place grass and other details on my terrain to correspond with the splatmap? 5 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