Loading external images without stutter
Hi,
For our project we need to load images from streaming assets or a webservice. We can't load them as ressources because images can change, and there is about 100.000 images. We don't need to load them all, but we need to load a bunch of images while an other previously loaded bunch of images are moving.
How can i do this without freezing the app? : When i load images, the moving ones seems to stutter
I tried sevaral things, like coroutines or tasks, but unfortunnaly nothing works, always stuttering. Does anybody knows how to accomplish that? Is there a way to load external images on a different thread?
Thank you for your help,Hi,
For our project we need to load images from streamingassets or a webservice. We can't load them as ressources because images can change, and there is about 100.000 images.
We need to load images while other images are moving (others loaded previously).
How can i do this without freezing the app? : When i load images, the moving ones seems to stutter
I try sevaral things, like coroutines or tasks, but unfortunnaly nothing works. Does anybody knows how to accomplish that? Is there a way to load external images on a different thread?
Thank you for your help
Hi,
Can you show a little of how you're currently doing this?
At what point is the stutter happening?
Best regards,
Thom.
Answer by davidopix · May 20, 2019 at 08:47 AM
Hi, Certainly @thomh_unity ! : First, this is a try to see how Unity manage loading external file for my project. I use UI raw images, to avoid use of sprite creation. So, i use a scene with:
A red square with a looping animation going from left to right and back
10 raw images.
Each raw image has a ImageTextureLoader.cs (joint file ImageTextureLoader.txt), loading behavior with coroutine using UnityWebRequestTexture. In my main cs, i load each texture one after the other. Pictures are jpg, from 4.8 Mo to 8.9 Mo
The stutter occurs on loading file, or while affecting texture to raw image, i don't know precisely. But i can see the stuttering on the animated red square.
Best regards David link text
Hi @davidopix,
There could be many reasons you're seeing this kind of issue.
$$anonymous$$nowing precisely where the stutter occurs will help to understand the problem.
Can you please provide some of the code you're using in the co-routine?
Thanks,
Thom.
Hi, this is the code used in ImageTextureLoader.cs :
using System; using System.Linq; using System.Threading.Tasks; using UnityEngine; using UnityEngine.UI; using UnityEngine.Networking; using System.Collections; using CielaSpike;
public class ImageTextureLoader : $$anonymous$$onoBehaviour {
public event Action OnComplete;
public string url;
protected string _loadUrl;
protected RawImage _image;
protected Texture2D _tex;
protected float _texWidth;
protected float _texHeight;
protected Sprite _sprite;
protected bool _loaded = false;
private void Start()
{
_image = GetComponent<RawImage>();
}
public void Load()
{
_tex = new Texture2D(4096, 4096);
_loadUrl = "file:///" + Application.strea$$anonymous$$gAssetsPath + "/" + url;
StartCoroutine(LoadTexture());
}
IEnumerator LoadTexture()
{
UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(_loadUrl);
yield return new WaitForEndOfFrame();
yield return uwr.SendWebRequest();
yield return new WaitForEndOfFrame();
if (uwr.isNetworkError || uwr.isHttpError)
{
Debug.Log(uwr.error);
}
else
{
yield return new WaitForEndOfFrame();
_tex = ((DownloadHandlerTexture)uwr.downloadHandler).texture;
yield return new WaitForEndOfFrame();
uwr.Dispose();
yield return new WaitForEndOfFrame();
_loaded = true;
}
}
void LateUpdate()
{
if (!_loaded) return;
_loaded = false;
_image.texture = _tex;
if (OnComplete != null) OnComplete();
}
}
Hi,
Am I understanding correctly that you have one of these scripts for each image you are trying to load? This will mean each image is trying to load at the same time.
Have you tried loading the images one at a time?
Thanks,
Thom.
Your answer
![](https://koobas.hobune.stream/wayback/20220612200640im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Download images and apply to sprite 0 Answers
Unity can not access StreamingAssets on IOS 1 Answer
Wrong JPEG library version 0 Answers
Dynamic load of Image target - ARKit 0 Answers
Make several buttons out of a single image (basic question) 0 Answers