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 Luponius · Mar 28, 2016 at 12:56 AM · c#texturemap-generation

Draw 2D array of Tiles to texture

I'm not sure if this is the right approach to it but I'm generating a map procedurally. The map is essentially a 2d array of Tiles

 private Tile[,] islandMap;

Each tile has a few different details such as whether the tile is a border (edge of map), ocean or land. I'm using a similar philosophy to Amitp's polygonal map generation - but restricted to a tiled grid in this case.

So what I'd like to accomplish here is represent my tile array of tiles and draw it as a single texture which is then applied to a Quad. I've done effectively everything but I'm stuck on how to draw the tiles to the texture - maybe there are more efficient, fast, and easy to present methods out there and my current approach is poor, but I tried to follow vaguely what AmitP did in his using textures.

 public class MapTexture
 {
     int textureScale;
     public MapTexture(int ts)
     {
         textureScale = ts;
     }
 
     public void AttachTexture(GameObject plane, Map map)
     {
 
         int textureWidth = (int)map.mapWidth * textureScale;
         int textureHeight = (int)map.mapHeight * textureScale;
 
         Texture2D texture = new Texture2D (textureWidth, textureHeight);
         texture.SetPixels (Enumerable.Repeat (Color.magenta, textureWidth * textureHeight).ToArray ());
 
         // TEXTURE EDITS HERE
         Color[] colors = new Color[3];
         colors [0] = Color.red;
         colors [1] = Color.green;
         colors [2] = Color.blue;
 
         for (int x = 0; x < map.mapWidth; x++) {
             for (int y = 0; y < map.mapHeight; y++) {
                 texture.SetPixels (x * textureScale, y * textureScale, textureScale, textureScale, colors);
             }
         }
 
         texture.Apply ();
 
         plane.GetComponent<Renderer> ().material.mainTexture = texture;
         plane.transform.localPosition = new Vector3 (map.mapWidth / 2, map.mapHeight / 2, 1);
     }
 }

This is sort of my code at the moment, I obviously know that SetPixels is being used completely wrong, but despite reading documentation I still have no idea what it's supposed to be doing - the initial IEnumerable.Repeat was effectively recycled code which I didn't understand all that well either.

But the for loop is clearly not working and I ain't sure what the right approach should be here. I just want to neatly represent these tiles next to each other to see what I'm doing and keep the generation and drawing process fast and simple since that's not my focus at the moment

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 Luponius · Mar 28, 2016 at 02:46 PM

     Texture2D texture = new Texture2D (textureWidth, textureHeight);
     texture.SetPixels (Enumerable.Repeat (Color.magenta, textureWidth * textureHeight).ToArray ());

     // Create color block to paint 


     for (int x = 0; x < map.mapWidth; x++) {
         for (int y = 0; y < map.mapHeight; y++) {

             Color[] colorBlock;

             if (map.islandMap[x,y].ocean)
                 colorBlock = Enumerable.Repeat(Color.blue,textureScale*textureScale).ToArray();
             else if (map.islandMap[x,y].coast)
                 colorBlock = Enumerable.Repeat(Color.yellow,textureScale*textureScale).ToArray();
             else 
                 colorBlock = Enumerable.Repeat(Color.grey,textureScale*textureScale).ToArray();

             texture.SetPixels (x * textureScale, y * textureScale, textureScale, textureScale, colorBlock);
         }
     }

To answer my own question, this was basically what I ended up going with in the end. It's nothing special but it gets the job done. Takes a while to generate but it's a one time thing anyways.

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 Luponius · Mar 28, 2016 at 10:06 PM 0
Share

This was later modified to be adapted to use with lists of Tiles, each referencing its own Vector2D "point" containing the x and y coordinates ins$$anonymous$$d of the 2d array values since it was easier to work with lists than arrays.

avatar image rakkarage · Mar 28, 2016 at 11:54 PM 0
Share

https://github.com/mizipzor/UnityTile$$anonymous$$ap

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Can you change the Texture Import Settings via C# Script? 1 Answer

How to assign texture from url 1 Answer

How to fill texture that would be avaliable to ither shaders? 1 Answer


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