Load a lot of images from server to app without the app freezing when applied! (500+ Images)
So i am currently developing an app for a national park in Africa and have currently hit a snag. I download the images to the users device if they do not exist locally to be used by the app. The problem is with my current approach.
I go through all the animals data and apply all of this to the animals page then also apply the image from local storage. When i do this the app freezes and currently on my PC it takes over 8 seconds to load the pictures into the sprite. Again remembering there are over 100 animals being loaded here.
How would you load all these images and remove the hang of the app? This is the code i use to load the images to sprites. My biggest concern here is that in the apps "Birds section" there are over 250 images
Stopwatch timer = new Stopwatch();
timer.Start();
using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture("File:///"+Application.persistentDataPath + "/" + FilePath + ".jpg"))
{
yield return uwr.SendWebRequest();
if (uwr.isNetworkError || uwr.isHttpError)
{
UnityEngine.Debug.Log(uwr.error);
}
else
{
//// Get downloaded asset bundle
var texture = DownloadHandlerTexture.GetContent(uwr);
var NewSprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0, 0), PixelsPerUnit, 0, spriteType);
UnityEngine.Debug.Log("NS:::::::::::::::::" + NewSprite);
onComplete(NewSprite);
timer.Stop();
TimeSpan timeTaken = timer.Elapsed;
UnityEngine.Debug.Log("Time Taken To Load Images " + timeTaken);
}
}
you are calling that coroutine per image right? how are you starting the coroutine? all at the same time?
Yes, I go through each entry in the class and start a coroutine for it like this. The method Download is just a container for the Coroutine.
public void DownloadAllFiles()
{
foreach (var animal in mammalData.allSpecies)
{
foreach (var img in animal.ImageName)
{
if (!File.Exists(Application.persistentDataPath + "/" + img + ".jpg"))
{
Debug.Log("The File " + img + ".jpg did not exist on our machine, Start the download process from Firebase \n Full file location is " + Application.persistentDataPath + "/" + img + ".jpg - If this is " +
"a Low res image make sure it is named the same it is on firebase to stop the needed for changes in the app control panel");
Download(img, animal.animalGroup.ToString());
}
}
}
foreach (var animal in mammalData.multiSpecies)
{
foreach (var img in animal.subSpecies)
{
foreach (var imgName in img.ImageName)
{
//Debug.Log("Data Path is :: " + Application.persistentDataPath + "/" + imgName + ".jpg");
if (!File.Exists(Application.persistentDataPath + "/" + imgName + ".jpg"))
{
Debug.Log("The File " + imgName + ".jpg did not exist on our machine, Start the download process from Firebase \n Full file location is " + Application.persistentDataPath + "/" + imgName + ".jpg - If this is " +
"a Low res image make sure it is named the same it is on firebase to stop the needed for changes in the app control panel");
Download(imgName, animal.animalGroup.ToString());
}
}
}
}
}
can you try changing the var texture line in the corroutine for this and check if it getsany better?
Texture myTexture = ((DownloadHandlerTexture)uwr.downloadHandler).texture;
Answer by john-essy · Mar 21, 2019 at 11:10 AM
I know im answering my own question here but i got some valuable help from unity forums. - Check here for the solution
Your answer

Follow this Question
Related Questions
SetPixels edge pixels 0 Answers
Unity seems to be in 2D, but I want it in 3D 1 Answer
Using bitmaps for Image-processing with Unity 1 Answer
LoadRawTextureData - how to use? 0 Answers