Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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
1
Question by Bablo · Mar 07, 2011 at 11:04 AM · textureterraingenerated

Adding a texture to a code-generated terrain, through code.

Hey all. I've been struggling with code-generated terrain for now and I'm having a hard time figuring out how to add a pre-made texture, to a code-generated terrain, while the game is running.

I know that I can generate the terrain and then add the texture from the inspector, but how can I do this on the fly when I generate the terrain.

Here is my code at the moment:

TerrainData _terrainData = new TerrainData(); GameObject _terrain = Terrain.CreateTerrainGameObject(_terrainData);

float[,,] splatMapData = _terrainData.GetAlphamaps(0, 0, 1, 1); splatMapData[0, 0, 0] = 1; splatMapData[0,0,1] = 0; splatMapData[0,0,2] = 0; splatMapData[0,0,3] = 0;

Now I know that there's now an array for my textures, but how can I add the texture in there?

Thanks in advance.

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

4 Replies

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

Answer by flaviusxvii · Mar 17, 2011 at 03:02 PM

I'm just reformatting the answer from Slayer, I have no idea if it works or not. The formatting was just freaking me out.

To add texture on terrain, simple assign it. For example:

test[0] = new SplatPrototype(); test[0].texture = (Texture2D)Resources.Load("grydirt2",typeof(Texture2D)); test[0].tileOffset = new Vector2(0, 0); test[0].tileSize = new Vector2(15, 15);

test[1] = new SplatPrototype(); test[1].texture = (Texture2D)Resources.Load("Balmoral",typeof(Texture2D)); test[1].tileOffset = new Vector2(0, 0); test[1].tileSize = new Vector2(15, 15); test[1].texture.Apply(true);

test[2] = new SplatPrototype(); test[2].texture = (Texture2D)Resources.Load("grass",typeof(Texture2D)); test[2].tileOffset = new Vector2(0, 0); test[2].tileSize = new Vector2(15, 15); test[2].texture.Apply(true);

terrain.terrainData.splatPrototypes = test;

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 DMCH · Feb 18, 2014 at 04:06 PM 0
Share

Yep, it works.

avatar image Eldoir · Aug 22, 2017 at 01:13 PM 0
Share

Thanks for this mate. Following what you wrote, I have my own contribution to this. Wrap this into a single file named 'TerrainExtensions'.

 using UnityEngine;
 using System.Collections.Generic;
 using System.Linq;
 
 public static class TerrainExtensions
 {
     public static void AddTexture(this Terrain t, Texture2D tex)
     {
         List<SplatPrototype> protos = t.terrainData.splatPrototypes.ToList();
 
         SplatPrototype newSplatProto = new SplatPrototype();
         newSplatProto.texture = tex;
         newSplatProto.tileOffset = Vector2.zero;
         newSplatProto.tileSize = Vector2.one;
         newSplatProto.texture.Apply(true);
 
         protos.Add(newSplatProto);
 
         t.terrainData.splatPrototypes = protos.ToArray();
     }
 }

Later on, this code allows me to simply write this in my code :

 myTerrain.AddTexture(myTex);

WARNINGS:

  • Your texture must have "Read/Write Enabled" checked in its Import Settings.

  • As this code modifies the terrain data itself, the textures added to the terrain using this code will remain once you left the Editor/quit the app! To be able to modify a terrain's data in runtime as if it were a "normal" GameObject, please read this topic

avatar image
3
Best Answer

Answer by Slayer · Mar 17, 2011 at 02:35 PM

To add texture on terrain, simple assign it. For example: [CODE] test[0] = new SplatPrototype(); test[0].texture = (Texture2D)Resources.Load("grydirt2", typeof(Texture2D)); test[0].tileOffset = new Vector2(0, 0); test[0].tileSize = new Vector2(15, 15); test[1] = new SplatPrototype(); test[1].texture = (Texture2D)Resources.Load("Balmoral", typeof(Texture2D)); test[1].tileOffset = new Vector2(0, 0); test[1].tileSize = new Vector2(15, 15); test[1].texture.Apply(true); test[2] = new SplatPrototype(); test[2].texture = (Texture2D)Resources.Load("grass", typeof(Texture2D)); test[2].tileOffset = new Vector2(0, 0); test[2].tileSize = new Vector2(15, 15); test[2].texture.Apply(true);

    terrain.terrainData.splatPrototypes = test;

[/CODE]

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

Answer by xvause · Mar 30, 2012 at 10:11 AM

What is the declaration of test ?

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 Arielgenesis · Jan 10, 2013 at 07:39 PM 0
Share

SplatPrototype[] test= new SplatPrototype[3];

avatar image
0

Answer by muzboz · Dec 02, 2018 at 08:08 AM

Just trying to reformat the "Best Answer" here, so I can see it properly. :)

QUOTING:

To add texture on terrain, simply assign it. For example:

 test[0] = new SplatPrototype(); 
 test[0].texture = (Texture2D)Resources.Load("grydirt2", typeof(Texture2D)); 
 test[0].tileOffset = new Vector2(0, 0); 
 test[0].tileSize = new Vector2(15, 15); 
 test[1] = new SplatPrototype(); 
 test[1].texture = (Texture2D)Resources.Load("Balmoral", typeof(Texture2D)); 
 test[1].tileOffset = new Vector2(0, 0); 
 test[1].tileSize = new Vector2(15, 15); 
 test[1].texture.Apply(true); 
 test[2] = new SplatPrototype(); 
 test[2].texture = (Texture2D)Resources.Load("grass", typeof(Texture2D)); 
 test[2].tileOffset = new Vector2(0, 0); 
 test[2].tileSize = new Vector2(15, 15); 
 test[2].texture.Apply(true);

 terrain.terrainData.splatPrototypes = test;

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating a Cube Projector 2 Answers

Incorporating Unity textures, models, and terrain into Eclipse 0 Answers

How to Apply Very Large Texture to Terrain 0 Answers

Can i change the Texture of terrain 3 Answers

Access terrains "Alphamap 0" from within code 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