0
votes

I'm currently working on a project in Unity which requires accessing images from a "Resources" folder and loading them as Sprites, for use during runtime.

The sprites in question aren't needed until the user opens a particular window. Only then they are necessary. The thing is, there are 150+ images (resolutions between 120x120 and 512x512) to be accessed, and only one image at a time will be shown (at any moment) during the execution of the game.

So my question is: Which option is better from an optimization standpoint?

  1. To load all images as sprites into variables at the beginning of the game and then use them as they are necessary?

  2. Load the images only when they're necessary?

Possibly important info: I'm using Resources.Load() to access all sprites I need

I don't know if the question has a "linear" answer. If the question is difficult to understand, or isn't answered as easily as I thought, please inform me.

Thank you in advance.

1
You could load them all at once on a background thread when the program is launched. They're done loading by the time the splash screen is done. See System.ComponentModel.BackgroundWorker - Shannon Holsinger
What is the code you use to load the images now? Do you currently load them as an array? @ShannonHolsinger He can't use that in Unity since Unity API are not allowed to be used from another Thread. - Programmer
You can use Threads in Unity but you can't call Unity API from those Thread. You have to call pass the data back to Unity's main Thread. How often do you need the images? For example, when do they load and unload? If you explain what you are doing, maybe there is even a better way to do this. - Programmer
If that's the case, then you should load them all into an array. If you don't want freezing when loading them, use Resources.LoadAsync - Programmer
@Programmer Indeed it would be better if the resources were loaded without freezing. I'll use it. Thank you! - Dayman75

1 Answers

1
votes

I'd say use something like Texture Packer to put them into a single sprite sheet, that way you are loading a small number of large images, which typically runs faster than a large number of small images. Then you can piece it out as needed. You'll see increased performance since you're sending less to the GPU every frame too.

As for the options you gave though, I'd favor pre-loading the images rather than waiting until they are needed. Unless you are super concerned about saving space, better to make the scene-load slightly longer (when everything is frozen anyway, and gamers are familiar with loading screens at this point) than to wait until the player is already in the game, engaged, and suddenly they have to break flow for a second and figure out if the game just froze. They won't know what you're doing and it'll just detract from the experience.