Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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:
    91
    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,847
    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:
    91
    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:
    194
    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:
    91
    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:
    91
    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!
     
  7. ttran07

    ttran07

    Joined:
    Mar 9, 2019
    Posts:
    9
    I did the same thing but for Android, I am running into an issue: "Vulkan Out of Memory"
    I have no idea what is going on.

    ======================

    EDIT: Solved it! Use SpriteAtlas!
    Having thousands of texture, Unity loading them all takes up lots of resources.
    Having SpriteAtlas, Unity combines all thousands textures into a single file for it to load and read the texture from, saving it a lot of resources
     
    Last edited: Jul 27, 2022