Hi there!
I’m kind of noob with unity, but I manage to make already a couple of good stuff with it.
Nowadays, and after reading tons of questions about yield, I couldn’t get the right answers for my problem.
I want to make a query to a web server from my code. To be able to do that, I’m creating the WWW object with the url that I want to use, and wait for it with yield. When I do that in the Start function, everything goes cool and smooth, but here comes the problem. I don’t want to call it from the start function.
I want to have a class called serverConnection, and into there retrieving the data form the webpage. At the moment I reached this stage, but I no longer know what to do.
using UnityEngine;
using System.Collections;
public class serverConnection
{
public string query()
{
string url = "http://www.google.com";
StartCoroutine(queryServer(url));
}
IEnumerator queryServer(string url)
{
WWW server = new WWW(url);
yield return www;
Debug.Log(www.text);
}
}
As you can see, I try to manage to split the code because I read that I should use the StartCoroutine call.
My original idea was to create something like this:
public class serverConnection
{
public string query(string url)
{
WWW www = new WWW(url);
return www.text;
}
}
I don’t know if I explain myself there… I was trying to create an “envelope” around the interaction with the server, and to retrieve only the “web content” inside the string return value.
The main problem of that, is that I’m forced to wait for the WWW object to be completely downloaded, and I found no other solution than yield (or busy waiting, but I really don’t want to go through that ugly way :S)
Does somebody have some ideas?