Question by
jeh_dias18 · Mar 09, 2016 at 03:46 PM ·
unity 5json
How to reduce the number of WWW requests?
private GameObject[] Photos;
private static int size = Gallery.getSizeImagesPerGallery();
private WWW www2;
private WWW www;
private IEnumerable<String> recebe;
// Use this for initialization
void Start ()
{
string LinkJsonFile = "...";
/*www = new WWW(LinkJsonFile);
yield return www.text;
if(www.isDone){
string aux = www.text;
Debug.Log("Baixou uma imagem -- 1");
TreatmentString(aux);
ClearWWW(www);
}*/
StartCoroutine (UpLoadImages(LinkJsonFile));
}
IEnumerator UpLoadImages (string url)
{
www = new WWW (url);
yield return www;
string aux = www.text;
TreatmentString(aux);
//Debug.Log("text" + www.text);
Debug.Log("Baixou uma imagem -- 1");
ClearWWW(www);
}
void TreatmentString (string urlJson)
{
string aux = urlJson.Replace ("\\", "");
PopulateLinksAndTitles(aux);
}
public GameObject prefab;
void PopulateLinksAndTitles (string content)
{
string aux = "";
string aux2 = "";
List<string> LinksImages = new List<string> ();
List<string> TitlesImages = new List<string> ();
JSONNode json = JSON.Parse (content);
Photos = GameObject.FindGameObjectsWithTag("Photos");
for (int i = 0; i < size; i++) {
aux = json ["content"] ["videos"] [i] ["image_url"].Value;
www2 = new WWW(aux);
aux2 = json ["content"] ["videos"] [i] ["title"].Value;
Photos = GameObject.FindGameObjectsWithTag("Photos");
StartCoroutine (CarriesThumbs (aux, aux2, www2,Photos[i]));
}
}
IEnumerator CarriesThumbs (string url, string TitleImage, WWW www2,GameObject Photo)
{
www2.threadPriority = ThreadPriority.High;
yield return www2;
Debug.Log("Baixou uma imagem -- 2");
Photo.name = TitleImage;
Photo.GetComponent<MeshRenderer>().material.name = url;
MeshRenderer render = Photo.GetComponent<MeshRenderer> ();
Texture2D texture = www2.texture;
render.material.mainTexture = texture;
if (www2 == null) {
Debug.LogError ("Failed to get the payload.");
}
if (www2.error != null) {
Debug.LogError ("Error occurred during download " + www2.error);
}
ClearWWW(www2);
}
public void ClearWWW (WWW www)
{
if (www != null) {
www.Dispose();
www = null;
System.GC.Collect();
}
}
I step to the WWW a URL that contains a json file and this file contains 60 urls images.
But when I call the method updateImage I have 13 calls to www.
And after the call of the other methods, there are 430 requests. But I need a request to get the text from the remote json file and another 60 requests for each image.
How can I handle this problem?
Because the occurrence of escessivas requests I get the following errors:
Error occurred during download Recv failure: Connection was reset
You are trying to load data from a www stream which had the following error when downloading. Recv failure: Connection was reset
Comment
Your answer