Update UI from inside a loop
Hi i have this really slow for-loop which creates prefabs and materials using code. I would like to have a UI text field update and show the loop iteration, Unfortunately the for loop exits only once its finished and the UI text does not update in the UI field although I change its .text property in the for loop.
I suspect that this is happening because the Update() function is not called, because of the blocking inside the loop.
Is there a way to force update the UI from inside the for-loop, in order to have it show the loop iterations.
Thanks Dimi
Unless you run the loop in a separate thread, the update function cannot be called, because the processor is busy running your loop. The only way around this would be to STOP processing the loop after a certain period of time/number of iterations, then resume during the NEXT update cycle. This would give Update a chance to redraw new values, like the last iteration count, to the screen.
Answer by elenzil · Jul 25, 2016 at 11:22 PM
use a coroutine and yield at the end of each iteration.
Answer by Christop · Jul 29, 2016 at 05:03 PM
I used a coroutine and reworked my code for this. Using threads is also a possibility probably but coding a coroutine is faster. Thanks for all the answers.