0
\$\begingroup\$

I have a game with almost 160 sprites in my resources folder. I need 15 randomly selected sprites on each level (there are 7 levels).

I load those sprites which are required e.g. my random generator scripts generate 15 random numbers (completely unique from previously generated 15 numbers) and based on those number I load the sprites (sprites are numbered from 1-158 so I can easily load them).

My levels take some significant time to load, I think that pre-loading the sprites at the start of each level is taking time. There are actually two folders, one for the questions and another for the answers. Each question has 5 possible answers so I actually load 15 * 5 sprites on each level and save them in an array.

Should I load all the sprites once the game is loaded? Or Should I pre-load all the Sprites in advance?

Thanks in advance I am really looking forward for answers.

NOTE: There are almost thousands of tiles(100px by 100px in size) on each level, I know this also has some impact on performance.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Load all of your sprites before your game starts, in a static class. This way, you only have to load your sprites once, and you can use them for the entirety of your game instance. Something like this:

public static class SpriteData 
{
    public static Sprite[] Sprites;

    public static void Load()
    {
        if (Sprites == null) {
            Sprites = Resources.LoadAll<Sprite> ("sprites");
        }
    }
}

Then in your game logic, call the load function:

SpriteData.Load();

Then use your sprites where you need them:

Sprite sprite = SpriteData.Sprites[1];
\$\endgroup\$
2
  • \$\begingroup\$ Thanks for the answer I will try that and update you about the performance. \$\endgroup\$ Apr 1, 2016 at 11:23
  • \$\begingroup\$ Hi! I implemented the solution but unfortunately it didn't work for me the game still takes time to load each level, anyway I am really thankful of you taking time to answer my question. Although there is one thing I want to know(I hope I am not going off-topic), the game is for website and I am building a webGL app so should I stick with my previous sprite loading technique or should I use yours? Since the preloading thing would take almost 800 sized array and loading on each level would take only 90 sized array each level. \$\endgroup\$ Apr 2, 2016 at 13:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .