I
I
ImPuuLsE2015-01-10 16:31:28
Unity
ImPuuLsE, 2015-01-10 16:31:28

How to return yield www in Unity?

Hello! there is a code:

var request = WWW(apiAuthUrl, form);
yield request;
return request.text;

It gives out that generators cannot return values... How can I return the answer in order to parse it in another function?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Basmanov, 2015-01-11
@ImPuuLsE

Option one - call the parser directly from the coroutine. If this is the only scenario for communicating with web forms, then it will do.

private void Start()
{
    StartCoroutine(Request(apiAuthUrl, form));
}

private IEnumerator Request(string apiAuthUrl, byte[] form)
{
    var request = new WWW(apiAuthUrl, form);
    yield return request;
    Parse(request.text);
}

private void Parse(string text)
{
    Debug.Log(text);
}

Option two - give data from the coroutine through a callback. Same eggs, side view, but more flexibility, you can use the same method in different scenarios.
private void Start()
{
    StartCoroutine(Request(apiAuthUrl, form, Parse));
}

private IEnumerator Request(string apiAuthUrl, byte[] form, Action<string> callback)
{
    var request = new WWW(apiAuthUrl, form);
    yield return request;
    callback(request.text);
}

private void Parse(string text)
{
    Debug.Log(text);
}

I gave the code in C #, I can’t imagine how it looks in JS.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question