Skip to content

Instantly share code, notes, and snippets.

@jdolan
Created April 19, 2012 14:23
Show Gist options
  • Save jdolan/2421271 to your computer and use it in GitHub Desktop.
Save jdolan/2421271 to your computer and use it in GitHub Desktop.
Unity3D deferred WWW loading
public class Example : MonoBehaviour {
// Some other state / variables here
// Load the specified JSON resource as the DataSet
IEnumerator Load(string url) {
WWW json = new WWW(url);
yield return json;
if (json.error == null) {
elements = (IList<IDictionary<string, object>>) JsonFx.Json.JsonReader.Deserialize(json.text);
OnLoad(); // WORKS -- this gets called, \o/
} else {
Debug.LogError("Failed to download " + url + ": " + json.error);
}
}
IEnumerator LoadMultiple(IEnumerable<string> urls) {
foreach (string url in urls) {
WWW json = new WWW(url);
yield return json;
if (json.error == null) {
// append all to the internal list
} else {
Debug.LogError("Failed to download " + serviceUrl + symbol);
}
}
OnLoad(); // THIS IS NOT CALLED, WHY?
}
IEnumerator Start() {
return Load("http://example.com/resource.json");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment