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 ShadyProductions · Jul 29, 2018 at 08:27 PM · 2dtilemaptilesgroup

2D: Best way to group a type of tile based on their neighbors similar type?

Hello,

I currently have a tilemap structure setup using a multi-dimensional array.

 TDTile[,] _tiles = new TDTile[50,50];

On this map I have a bunch of trees, and I wanna group them all by pairs of 4. This is working, but it is leaving residue trees behind, like you can clearly see nice groups of 4 or bigger tree clusters, but occasionally there are a couple tree's spread around the map that are not grouped, so in a pair, or just a single tree every here and there..

Is there a better way I can group my tiles?

Example view:

alt text


This is my current approach:

 private List<TDTile> GetNeighbors(int x, int y)
 {
     Vector2Int[] positions = new [] 
     {
         new Vector2Int(x-1, y),
         new Vector2Int(x+1, y),
         new Vector2Int(x, y - 1),
         new Vector2Int(x, y + 1),
         new Vector2Int(x + 1, y + 1),
         new Vector2Int(x - 1, y + 1),
         new Vector2Int(x + 1, y - 1),
         new Vector2Int(x - 1, y - 1),
     };
 
     List<TDTile> neighbors = new List<TDTile>();
     foreach (var pos in positions)
     {
         if (IsInRangeOfMap(pos.x, pos.y))
         {
             neighbors.Add(_tiles[pos.x, pos.y]);
         }
     }
 
     return neighbors;
 }
 
 private void GroupTilesBy(string type, int minAlive)
 {
     for (int x = 0; x < Width; x++)
     {
         for (int y = 0; y < Height; y++)
         {
             if (_tiles[x, y].Type == type)
             {
                 var neighbors = GetNeighbors(x, y)
                     .Count(f => f.Type == type)
 
                 neighbors++; // Count our own tile in aswel
 
                 if (neighbors < minAlive)
                 {
                     _tiles[x, y] = _db.GetDefaultTile();
                 }
             }
         }
     }
 }

I could do something cheeky like this after grouping, to solve the problem:

 for (int x = 0; x < Width; x++)
 {
     for (int y = 0; y < Height; y++)
     {
         if (_tiles[x, y].Type == type)
         {
             var neighbors = GetNeighbors(x, y)
                 .Count(f => f.Type == type)
 
             if (neighbors == 0)
             {
                 _tiles[x, y] = _db.GetDefaultTile();
             }
         }
     }
 }

But I feel like I'm just missing something, that I'm doing wrong in the actual algorithm.

Open to suggestions about improvement.

Thanks in advance..

Kr, Shady.

screenshot-1.png (24.0 kB)
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
1
Best Answer

Answer by SirCrazyNugget · Jul 30, 2018 at 10:37 PM

Take a look at Procedural Map generation tutorial, https://unity3d.com/learn/tutorials/projects/procedural-cave-generation-tutorial/detecting-regions?playlist=17153 this video focuses on the Flood Fill algorithm which will help you identify the tiles and their neighbours for adding/removing these

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

181 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 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

How to programmatically switch tile palettes in Unity2D 0 Answers

Fix for offset tiles in tilemap and palette 1 Answer

Confused about ITilemap interface and concrete tilemap classes 0 Answers

2D Tiles and sprites 1 Answer

Beginner tutorial for 2D tiles/tilemaps? 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