• 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 /
  • Help Room /
avatar image
0
Question by ametkly · Nov 29, 2021 at 04:00 PM · algorithmfill

How can I fill empty and closed areas with the flood fill algorithm?

I want the floor to fill with the color I choose when the object creates any shape with space in the middle. It should automatically fill the gaps between the lines\boxes. How can I do that?

s2 s1

This is my flood fill code:

 public class FloodFill : MonoBehaviour
 {
     [SerializeField] private float fillDelay = 0.01f;
     private GridManager gridManager;
     void Start()
     {
         gridManager = this.GetComponent<GridManager>();
     }
 
     public IEnumerator Flood(int x, int y, Color oldColor, Color newColor)
     {
         WaitForSeconds wait = new WaitForSeconds(fillDelay);
         if (x >= 0 && x < gridManager.xSize && y >= 0 && y < gridManager.ySize)
         {
             yield return wait;
             if (gridManager.grid[x, y].GetComponent<SpriteRenderer>().color == oldColor)
             {
                 gridManager.grid[x, y].GetComponent<SpriteRenderer>().color = newColor;
                 StartCoroutine(Flood(x + 1, y, oldColor, newColor));
                 StartCoroutine(Flood(x - 1, y, oldColor, newColor));
                 StartCoroutine(Flood(x, y + 1, oldColor, newColor));
                 StartCoroutine(Flood(x, y - 1, oldColor, newColor));
             }
         }
     }
 }

and this is fill for just borders:

 if (hit.collider != null)
         {
             mov = false;
             if (Mathf.RoundToInt(this.transform.position.x) >= gridManager.xSize / 2 && Mathf.RoundToInt(this.transform.position.y) >= gridManager.ySize / 2)
             {
                 if (currentDirection == up)
                 {
                     StartCoroutine(floodFill.Flood(Mathf.RoundToInt(this.transform.position.x + 1), Mathf.RoundToInt(this.transform.position.y), Color.white, Color.green));
                 }
 
                 if (currentDirection == right)
                 {
                     StartCoroutine(floodFill.Flood(Mathf.RoundToInt(this.transform.position.x), Mathf.RoundToInt(this.transform.position.y + 1), Color.white, Color.green));
                 }
             }
 
             if (Mathf.RoundToInt(this.transform.position.x) <= gridManager.xSize / 2 && Mathf.RoundToInt(this.transform.position.y) <= gridManager.ySize / 2)
             {
                 if (currentDirection == down)
                 {
                     StartCoroutine(floodFill.Flood(Mathf.RoundToInt(this.transform.position.x - 1), Mathf.RoundToInt(this.transform.position.y), Color.white, Color.green));
                 }
 
                 if (currentDirection == left)
                 {
                     StartCoroutine(floodFill.Flood(Mathf.RoundToInt(this.transform.position.x), Mathf.RoundToInt(this.transform.position.y - 1), Color.white, Color.green));
                 }
             }
 
             if (Mathf.RoundToInt(this.transform.position.x) < gridManager.xSize / 2 && Mathf.RoundToInt(this.transform.position.y) > gridManager.ySize / 2)
             {
                 if (currentDirection == up)
                 {
                     StartCoroutine(floodFill.Flood(Mathf.RoundToInt(this.transform.position.x - 1), Mathf.RoundToInt(this.transform.position.y), Color.white, Color.green));
                 }
 
                 if (currentDirection == left)
                 {
                     StartCoroutine(floodFill.Flood(Mathf.RoundToInt(this.transform.position.x), Mathf.RoundToInt(this.transform.position.y + 1), Color.white, Color.green));
                 }
             }
 
             if (Mathf.RoundToInt(this.transform.position.x) > gridManager.xSize / 2 && Mathf.RoundToInt(this.transform.position.y) < gridManager.ySize / 2)
             {
                 if (currentDirection == down)
                 {
                     StartCoroutine(floodFill.Flood(Mathf.RoundToInt(this.transform.position.x - 1), Mathf.RoundToInt(this.transform.position.y), Color.white, Color.green));
                 }
 
                 if (currentDirection == right)
                 {
                     StartCoroutine(floodFill.Flood(Mathf.RoundToInt(this.transform.position.x), Mathf.RoundToInt(this.transform.position.y - 1), Color.white, Color.green));
                 }
             }
         }
         else
         {
             mov = true;
         }


screenshot-6.png (6.4 kB)
screenshot-5.png (5.5 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

0 Replies

· Add your reply
  • Sort: 

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

171 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

Related Questions

Approach for filling 6x6 matrix with shapes 2 Answers

Slow flood fill of sprite c# 2 Answers

Make the health bar fill smoothly over time 0 Answers

Graph Library recomendation for Unity C#? 0 Answers

Remove random objects if amount dealt in a time frame equals or is greater than the health 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