- Home /
(C#) coroutines keep blocking GUI :?
I have a problem... I need to use WWW class to fetch some data from the server and then it should be displayed for user in the GUI.
What I though of is this:
static method that
...does some initialization,
var www = new WWW(url);
// do stupid busy waiting till the server replies or an error occurs
while (www.isDone != true)
{}
... parses returned json or fails gracefully
then from GUI,
public IList<ListEntity> entities = null;
void OnGUI()
{
if (entities != null)
{
// traverse the list and display data
}
if (GUI.Button(new Rect(140, 10, 250, 30), "Test "))
{
StartCoroutine(TestStuff());
}
}
and the fetcher part that sets the entities list so it could be displayed :
IEnumerator TestStuff()
{
Debug.Log("Starting coroutine");
entities = <call to static method>;
Debug.Log("finished");
yield return null;
}
The problem is that, as far as I understand coroutines, it should be something similar to creating a thread. But unity does not behave this way. It halts the button press till the data is returned from the server.
Any ideas what I am doing wrong?
And is it possible to use normal threads in unity?
UPDATE --
Hmm, just tried moving the WWW part inside the GUI class. It does not block GUI now, when I can dovar www = new WWW(url); yield return www;
I guess that will have to do...
That's how coroutines work - you need to yield the thing that takes the time - in this case the WWW
Your answer
![](https://koobas.hobune.stream/wayback/20220613074737im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Terminal-like GUI, wait for input 1 Answer
Stopping a Coroutine and drawing OnGUI 1 Answer
Why isn't my simple coroutine working? (and how can I make it infinite?) 2 Answers
Multiple Cars not working 1 Answer