Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Ninjatrombonist · Apr 29, 2019 at 07:14 PM · terrainterraindataterraintexture

Efficiently change many terrains' textures at once?

I have many terrain tiles in my scene that I want to be able to toggle between different styles of textures. Initially I was just looping through the terrains and setting the diffusetexture of the terrainlayer. This caused a hitch that was unacceptable for a VR application. I tried a few other things, unsuccessfully. Right now I have it running the loop through a coroutine to spread out the load over a few frames. This works, but I would like to have all of the textures change at once. Any tips?

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
0
Best Answer

Answer by dan_wipf · May 02, 2019 at 05:59 AM

you do not need to loop through the terrains. Just put your TerrainLayers from the Asset Folder in to an Array and change the Textures via Code.


for Example =>


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TerrainLayerTexturesChange : MonoBehaviour
 {
     public TerrainLayer[] TerrainLayers;
     public Texture2D[] diffuseTextures;
     public Texture2D[] normalMapTextures;
     public void ChangeTextures(){
         if(TerrainLayers.Length == diffuseTextures.Length && TerrainLayers.Length == normalMapTextures.Length){
             for(int i = 0; i<TerrainLayers.Length;i++){
                 TerrainLayers[i].diffuseTexture = diffuseTextures[i];
                 TerrainLayers[i].normalMapTexture = normalMapTextures[i];
             }
         }
     }
 }


Or working with Structs


 public class TerrainLayerTexturesChangeWithStructs : MonoBehaviour{
     [System.Serializable] public struct CustomTerrainLayers{
         [SerializeField] public TerrainLayer MyTerrainLayer;
         [SerializeField] public LayerTexture[] LayerTextures;
         [SerializeField] [HideInInspector] public LayerTexture OriginalTextures;
     }
     [System.Serializable] public struct LayerTexture{
         [SerializeField] public Texture2D DiffuseTexture;
         [SerializeField] public Texture2D NormalMapTexture;
     }
 
     public CustomTerrainLayers[] TerrainLayers;
     public int TextureIndex = 0;
     void OnEnable(){
         for(int i = 0; i<TerrainLayers.Length;i++){
             TerrainLayers[i].OriginalTextures.DiffuseTexture = TerrainLayers[i].MyTerrainLayer.diffuseTexture;
             TerrainLayers[i].OriginalTextures.NormalMapTexture = TerrainLayers[i].MyTerrainLayer.normalMapTexture;
         }
     }
     public void ChangeTextures(){
         for(int i = 0; i<TerrainLayers.Length;i++){
             TerrainLayers[i].MyTerrainLayer.diffuseTexture = TerrainLayers[i].LayerTextures[TextureIndex].DiffuseTexture;
             TerrainLayers[i].MyTerrainLayer.normalMapTexture = TerrainLayers[i].LayerTextures[TextureIndex].NormalMapTexture;
         }
     }
     public void ResetLayersToOriginal(){
         for(int i = 0; i<TerrainLayers.Length;i++){
             TerrainLayers[i].MyTerrainLayer.diffuseTexture = TerrainLayers[i].OriginalTextures.DiffuseTexture;
             TerrainLayers[i].MyTerrainLayer.normalMapTexture = TerrainLayers[i].OriginalTextures.NormalMapTexture;
         }
     }
 }


if you fire those functions as a Single Event, this should keep you going quite fast

Comment
Add comment · 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

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

127 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

TerrainData is missing splat texture 3. 1 Answer

offset and misbehaviour Issue with the 'Paint Texture' tool in the Terrain Editor 0 Answers

Why does my .raw heightmap create ugly errors when loaded into a terrain? 1 Answer

Spherical Terrain or screenshot to texture 0 Answers

Terrain Texture (applied programatically) Disappears When Entering Play Mode 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