- Home /
Is there a way to multithread a unity application so my it does not freeze when loading lots of images
I am currently loading a bunch of UI elements, no problem so far but for each of these I am loading images aswell from the streamingdata folder.
Code for loading these images (potentially irrelevant):
[RequireComponent(typeof(SpriteRenderer))]
public class StoreImage : Image
{
private readonly string storeImagePath = Application.streamingAssetsPath + "/Store/";
public void LoadStoreImage(int id)
{
try
{
string url = Path.Combine(storeImagePath, $"{id}.png");
byte[] imgData;
Texture2D tex = new Texture2D(2, 2);
imgData = File.ReadAllBytes(url);
tex.LoadImage(imgData);
Vector2 pivot = new Vector2(0.5f, 0.5f);
Sprite sprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), pivot, 100.0f);
SpriteRenderer renderer = GetComponent<SpriteRenderer>();
renderer.sprite = sprite;
this.sprite = renderer.sprite;
}
catch { }
}
}
However, when loading the images the entire application freezes and nothing is responsive until it is done, is there a workaround for this like loading the images on the background and serving them once done?
Answer by digipaul · May 31, 2019 at 07:41 PM
You could try this function https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequestTexture.GetTexture.html It works perfectly well with local files as long as you put the special file prefix on it (file:///)
Your answer

Follow this Question
Related Questions
UI Canvas Image parented to main camera only moves in x and z 0 Answers
Importing Stripes from Photoshop to Unity 0 Answers
Get mouse click world coordinates "through" UI Raw Image with Render Texture from second camera 2 Answers
Picture loaded from resource corrupted. 0 Answers
I can't see the all of UI in the phone 0 Answers