Load large images from file without stuttering/freezing game?

Hi,

I have a UI background image and when a button is pressed the background image is changed to an image found within a folder, the problem is if the image is decent-high quality the game will freeze for a few frames (seen in the profiler).

Is there a better way of doing this? At the moment I’m using LoadImageIntoTexture (image file path)

    // Load the image
    IEnumerator LoadImg()
    {
        yield return 0;
        WWW imgLink = new WWW("file://" + completePath);
        yield return imgLink;
        imgLink.LoadImageIntoTexture(img.mainTexture as Texture2D);
    }

Check out UnityWebRequest. It replaces WWW, as WWW is now obsolete. Unless you are on a really old version use UnityWebRequest.

Edit:
If you need to use WWW use WWW.textureNonReadable instead of .mainTexture. This can help speed things up.

Got it working, thanks a lot!

1 Like