Creating an sprite from jpg in a directory - Performance

Hi, I’m trying to create something similar to a file browser and for the most part it’s working as intended. Then only problem is that it’s very slow loading images.
For example ill have a Button that gets created and the button will have an image on it set from a file in a directory. The image shows but it takes a while to load. Especially when there are multiple images to load.

My current code that creates the image sprite from a jpg.

            byte[] bytes = File.ReadAllBytes(info [i].FullName);//Location is set before hand
            Texture2D imgTexture = new Texture2D(900, 900, TextureFormat.DXT1, false);
            imgTexture.filterMode = FilterMode.Bilinear;
            imgTexture.LoadImage(bytes);
            Sprite sprite = Sprite.Create(imgTexture, new Rect(0,0,900, 900), new Vector2(0f,0f), 1.0f);
            sprite.name = ("Button Image: " + info [i].Name);
            //This sets the sprite on the button
            justSpawned.GetComponent<ImageButtonInfo> ().imageSprite.sprite = sprite;

Is there a faster way creating a sprite from a jpg image on the fly?

Many thanks!

Probably the DXT compression; use uncompressed instead.

–Eric

Hi, Thanks for getting back to me.
The following doesn’t set it is to DXT but it still takes a fair while to load:

Texture2D imgTexture = new Texture2D(900, 900);

Unless there is an actual way of setting it to uncompressed that I can’t find.

I’ve change the filterMode to point with the following But still the performance issues remain:

imgTexture.filterMode = FilterMode.Point;

Am I missing something?

Thanks!

It seams to be this line that’s making it slow down. I’ve commented out each line until it starts taking an age to load:

Sprite sprite = Sprite.Create (imgTexture, new Rect (0, 0, 900, 900), new Vector2 (0f, 0f), 1.0f);

Could it be because the width and height are set to 900 and 900? It’s odd.
It seams that the higher number the smaller the image. It doesn’t matter what I set the pixels per unit to.

Any ideas what could be causing this behaviour? Why does the higher number make the image smaller?

I tried with an empty 900x900 texture and it’s taking .02 seconds.

–Eric

Yeah it takes about that long on my PC as well.
The issue is with mobile devices. The images that are loading only need to be thumbnail size. But for what ever reason when I set the width and height to a lower value than 900 the image gets bigger. If I set it to 128 the image gets huge and you can only see the top left corner of it. Changing the pixels per unit has no effect.

If I set it to 128x128 it loads the images a lot faster but they are displayed bigger.

Any ideas what could be causing this?

Thanks

Try different sprite settings in the editor to get what you want, then replicate those settings in code.

–Eric

Yeah that’s what I’ve been doing. The width and height of the image that I’m applying the newly created sprite to is set to 128 x 128.
When I use the following the Image doesn’t display properly:

Sprite sprite = Sprite.Create (imgTexture, new Rect (0, 0, 128, 128), new Vector2 (0f, 0f), 100f);

When I use this the image does display how it should but why?:

Sprite sprite = Sprite.Create (imgTexture, new Rect (0, 0, 900, 900), new Vector2 (0f, 0f), 100f);

Something is definitely not doing what it should be doing.

I’ll post some screen shots soon. Just have to nip out.

Here are the images.

This is default sprite without having a new image applied:

This is the image when it’s set to 900, 900:

And finally this is the image when it’s set to 128, 128:

As you can see by the image it’s as if its only showing the top left of the image I would assume as that’s where the anchors are.