Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 woals3850 · Jun 29, 2021 at 11:03 AM · unity 5multithreadingthread

How does unity thread works?

 public class MazeCreator : MonoBehaviour
 {
 public int width;
 public int col;
 public int row;
 public Vector3 startPos;
 public Material mat;
 public GameObject cell;
 public Stack<Cell> stack = new Stack<Cell>();
 public NavMeshSurface NavMesh;
 
 public List<List<Cell>> rowList = new List<List<Cell>>();


 public Cell currentCell;

 public bool isSleep = true;
 // Start is called before the first frame update
 void Start()
 {
     for(int i=0; i< row; i++)
     {
         //Step1 : fill the columns
         List<Cell> columnList = new List<Cell>();
         for (int j=0; j<col; j++)
         {
             //step 2 create and init the cell.
             Cell newCell = new Cell();
             newCell.cell = Instantiate(cell, startPos, cell.transform.rotation);
             columnList.Add(newCell);
             newCell.InitCell(i,j,this);
             
             //step3 move to next pos.
             startPos.x += width;
         }

         //step4 fill the rows with the columns.
         rowList.Add(columnList);

         //step5 move to the next row and go to the first pos of a column. 
         startPos.x -= col * width;
         startPos.z += width;
     }

     isSleep = true;
     //next, set the currentcell and draw it recursively.
     currentCell = rowList[0][0];
     currentCell.isVisited = true;
     currentCell.show();
     currentCell.ColorNext(currentCell);
     stack.Push(currentCell);

     StartCoroutine(BuildNavMesh());
     
     

 }

 public IEnumerator BuildNavMesh()
 {
    
     

     foreach (List<Cell> clist in rowList)
     {
         foreach (Cell c in clist)
         {
             c.ground.SetActive(false);
         }
     }


     NavMesh.BuildNavMesh();
     Debug.Log("Builded");

     yield return new WaitForSeconds(0.1f);
 }
 //method for measuring a time.
 public void Wait( Cell next)
 {
     StartCoroutine(wait(next));
 }
 IEnumerator wait( Cell next)
 {
     yield return new WaitForSeconds(0.2f);
     next.ColorNext(next);
 }

 // Update is called once per frame
 void Update()
     {
     
     }
 }

 public class Cell
 {
 public int i;
 public int j;
 public bool bottom;
 public bool top;
 public bool left;
 public bool right;

 public Material matSelected;

 public GameObject leftWall;
 public GameObject rightWall;
 public GameObject topWall;
 public GameObject bottomWall;
 public GameObject ground;

 public bool isVisited = false;
 public Cell current;

 public GameObject cell;
 public MazeCreator maze;
 public Material mat;


 public List<Cell> neighbors = new List<Cell>();
 public Cell checkNeighbors(Cell cell)
 {
     
     // first, init the position of a cell.
     i = cell.i;
     j = cell.j;
     
     // case for exceeding a left margin.
     if (j - 1 >= 0)
     {
         Cell left = maze.rowList[i][j - 1];
         if (!left.isVisited) neighbors.Add(left);
     }


     //case for exceeding a right margin.
     if (j + 1 < maze.col)
     {
         Cell right = maze.rowList[i][j + 1];
         if (!right.isVisited) neighbors.Add(right);

     }

     //case for exceeding a top margin.
     if (i - 1 >= 0)
     {
         Cell top = maze.rowList[i - 1][j];
         if (!top.isVisited) neighbors.Add(top);
     }

     //case for exceeding a bottom margin.
     if (i + 1 < maze.row)
     {
         Cell bottom = maze.rowList[i + 1][j];
         if (!bottom.isVisited) neighbors.Add(bottom);
     }

     //check the cell has any neighbors.
     if (neighbors.Count > 0)
     {
         Cell neighbor = neighbors[Random.Range(0, neighbors.Count)];
        
         return neighbor;
     }

     return null;

     




 }
 public void InitCell(int i, int j, MazeCreator maze)
 {
     this.i = i;
     this.j = j;

     //set the maze ,current cell and walls.
     this.maze = maze;
     this.current = this;
     leftWall = cell.transform.GetChild(0).gameObject;
     rightWall = cell.transform.GetChild(1).gameObject;
     topWall = cell.transform.GetChild(2).gameObject;
     bottomWall = cell.transform.GetChild(3).gameObject;
     ground = cell.transform.GetChild(4).gameObject;

 }
 public void show()
 {
     //draw the cell 
     if (isVisited)
     {
         leftWall.GetComponent<MeshRenderer>().materials[0].color = Color.blue;
         rightWall.GetComponent<MeshRenderer>().materials[0].color = Color.blue;
         topWall.GetComponent<MeshRenderer>().materials[0].color = Color.blue;
         bottomWall.GetComponent<MeshRenderer>().materials[0].color = Color.blue;
         

     }
     if (left)
     {
         leftWall.SetActive(true);
     }

     if (right)
     {
         rightWall.SetActive(true);
     }

     if (top)
     {
         topWall.SetActive(true);
     }

     if (bottom)
     {
         bottomWall.SetActive(true);
     }
 }

 public void ColorNext(Cell cell)
 {

     ground.GetComponent<MeshRenderer>().materials[0].color = Color.black;
     //get the neighbor cell


     Cell next = checkNeighbors(cell);
    
     if (next != null)
     {
        
         //visit first and show it.
         //step1
         next.isVisited = true;
         next.show();
         next.ground.GetComponent<MeshRenderer>().materials[0].color = Color.yellow;
         maze.stack.Push(next);
         //step3
         removeWalls(cell, next);


         //step4
         maze.Wait(next);
         //next.ColorNext(next);
         

         
     }

    
     
     //case getting stuck : no neighbors exist. + stack is not empty.
     if (next == null && maze.stack.Count > 0)
     {
         
         
         while (next == null)
         {
             //first, pop from the stack, and check its neighbor until it has neighbor.
             current = (Cell)maze.stack.Pop();
             current.ground.GetComponent<MeshRenderer>().materials[0].color = Color.green;
             next  = checkNeighbors(current);
             //next.ground.GetComponent<MeshRenderer>().materials[0].color = Color.yellow;


         }

         next.isVisited = true;
         next.show();
         ColorNext(next);

     }

 }

 public void removeWalls(Cell current, Cell next)
 {
     int h = current.i - next.i;
     int v = current.j - next.j; 

     //Right
     if(v < 0)
     {
         current.rightWall.SetActive(false);
         next.leftWall.SetActive(false);
     }

     //Left
     else if(v > 0)
     {
         current.leftWall.SetActive(false);
         next.rightWall.SetActive(false);
     }

     //Bottom
     else if(h > 0)
     {
         current.bottomWall.SetActive(false);
         next.topWall.SetActive(false);
     }

     //Top
     else if ( h < 0)
     {
         current.topWall.SetActive(false);
         next.bottomWall.SetActive(false);
     }
 }



 }


Hello, I'm making a maze generator. After Start(), A System generates the cells recursively. I want a system to implement a BuildNavMesh for the last time. but when Cell.ColorNext() is called, the thread doesn't wait there and keeps going. How do I let the system implement the method when a recursive function is done? or How do I know when the recursive method is done? ( I tried the stack, but it doesn't be zero.)

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

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

"...can only be called from the main thread" - MultiThreading 1 Answer

GPU Multithreading in Unity 3D 5 [NVIDIA TESLA K80] 0 Answers

Unity Threading Issue: Not able to delegate task 0 Answers

Performing ray casts from multiple threads within a FixedUpdate call 1 Answer

Physics without UnityEngine namespace 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