- Home /
where should i put the heavy code ?
hello every one . so i have a piece of code that take some minutes to finish executing and i've no problem with that . the problem is when i run the game on a mobile device it sometimes crach and quit the application . i have no problem to make the player wait till it finish but i think unity doesn't agree . i' putting the code in the Start() method . is there any other way to do it + i would like to have a progress value to use it in the UI
NOTE : the script is slow because it have a big for loop
Answer by DONlX · Dec 04, 2020 at 04:19 PM
Hi, Unity doesn't have any built in solution for this type of multithreading. You should try to use Coroutines and yield return a WaitForEndOfFrame. This will give back the control for Unity so you can update the UI for progress value and the device won't think this application is in a infinite loop. For example:
     public void Do()
     {
         StartCoroutine(StartTask());
     }
 
     private IEnumerator StartTask()
     {
         int iterations = 10000;
         long l = 0;
         for (int i = 0; i < iterations; i++) 
         {
             for (int y = 0; y < 25; y++)
                 for (int z = 0; z < 25; z++)
                     l++;
             disp.text = $"{i} of {iterations}";
             yield return new WaitForEndOfFrame();
         }
         disp.text = "Done";
     }
Your answer
 
 
             Follow this Question
Related Questions
How does the RigidBody.Transform.rotate method work with degrees at 0? 0 Answers
lvl win & lvl lose windows 1 Answer
Custom Inspector for multiple scripts 1 Answer
Creating a Script at runtime, and adding it to an object... 1 Answer
Problem with connecting between the Walk mode and Idle mode, they are mix 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                