Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

Performance question loading 1000's of sprites

Discussion in 'Scripting' started by Valorware, May 1, 2020.

  1. Valorware

    Valorware

    Joined:
    Aug 25, 2016
    Posts:
    79
    Hi,

    I need to load 1000's of sprites for my item, entity, mapitem etc. databases when the app launches. Currently, I do this by putting them all in a spriteAtlas (for faster rendering), then calling SpriteAtlas.GetSprite("spriteName"); 1000's of times during the initialisation of the databases.

    This works, and seems very fast on a high end PC, but obviously a lot slower for mobile devices.

    I assume "GetSprite" is a bit of an exhaustive search, taking a lot time to find each one.

    I had an idea to populate a GameObject with an array of the sprites (in the correct order), and then just retrieve the sprite from the gameobject by index, which should be a lot faster, right?

    My quesiton is that although the retrieval may be faster, would the very initial boot time of the app be longer, as that big gameobject needs to load? Are sprites stored in a game object stored by a reference, or when the gameobject instantiates does it have to search for those sprites anyway?

    Thank you, sorry I'm really bad at explaining things
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,821
    My understanding is when a scene loads, anything any scene object references, anything those references reference, etc, etc, are all loaded into memory up front. Whether this would be unacceptably slow in your case would require actually testing it.
     
    Valorware likes this.
  3. Valorware

    Valorware

    Joined:
    Aug 25, 2016
    Posts:
    79
    I needed to load the big spriteatlasses anyway (which I think loads the sprites?), so I'm not too conerned about the gameobject loading all the sprites~ I suppose I just need to do some testing.

    Does anyone know if there's any benefit between using a ScriptableObject vs normal GameObject stored as a prefab to just store a bunch of sprites in?

    Thank you
     
  4. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    189
    If you don't need the GO and transform component, then SO is the way its meant to be. It's cleaner and lighter.
     
    Valorware likes this.
  5. Valorware

    Valorware

    Joined:
    Aug 25, 2016
    Posts:
    79
    Thanks a lot, I'll check it out!

    I've just made a script to place all my item sprites into a gameobject with a sprite array, in the picture. There's 1400+ unique items in my game. Does anyone see this being problematic before I get too far into it?

    spriteBank.jpg
     
  6. Valorware

    Valorware

    Joined:
    Aug 25, 2016
    Posts:
    79
    Just incase anyone's wondering, using a ScriptableObject to store my sprites instead of using SpriteAtlas.GetSprite("name") saved a huge amount of time~

    My database loading ona super high end PC went from 2.6 seconds down to 0.4seconds when applying this to ~3500 sprites, I still have maybe a couple more 1000 to more to do. So obviously this will save a lot more time on mobile too!
     
unityunity