Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Creating sprite atlas at runtime?

Discussion in 'Editor & General Support' started by pep_dj, Jun 14, 2019.

  1. pep_dj

    pep_dj

    Joined:
    Nov 7, 2014
    Posts:
    178
    Our game will have a lot of sprites that will be used across different scenes. Let's say we will have 500 sprites in total, and each scene will use 50 of them, but most sprites will be used in several scenes.

    So having all the sprites in a single sprite atlas is not a good idea, because it would be too big.
    Having a sprite atlas for each scene will be a waste of space, because the same sprite will be in several atlases.
    And not using atlas at all will increase draw calls and affect performance.

    The perfect solution will be generating a new atlas at run time when a new scene is loaded, with the sprites that will be used. Is this possible with Unity?
     
    DragonCoder likes this.
  2. pep_dj

    pep_dj

    Joined:
    Nov 7, 2014
    Posts:
    178
  3. phandrana_unity

    phandrana_unity

    Joined:
    Apr 24, 2019
    Posts:
    8
    Hi, I'm also interested with generating sprite atlas at runtime, i've been looking at the link you sent.
    Did you try it ? I'm interrested to have some feeback about this lib :)

    I'm a bit worried with the size of the texture, since, correct me if i'm wrong, it's putting every asset according to there bounding box, instead of the "tight" version of unity atlas.
    But even if there was a "tight" filling it wouldn't be great to use it at runtime, it would take a lot of time to generate, so it's not an option.

    I've been thinking of another option: auto generate sprite atlas for unity editor, all your assets would have an information about there location in the game, you make a python script or else, that creates all the atlas you will need. it won't use memory, an atlas just takes the reference of the images, it will be long to generate in unity everytime you have a change, but I think it's viable.
     
  4. pep_dj

    pep_dj

    Joined:
    Nov 7, 2014
    Posts:
    178
    I abandoned the idea to create texture atlas at runtime: the approaches I tried were slow and didn't work well. Anyway, if you find a reliable way to do it, please let us know.
     
  5. phandrana_unity

    phandrana_unity

    Joined:
    Apr 24, 2019
    Posts:
    8
    Oh ok, can your share your approach ? I'm gonna tell you about mine, maybe you will see a problem in it that can save me times :)

    For the context: it's a 2D game with parallaxe, there's several levels in wich there's several assets that I want to put in atlas to limit draw calls.

    I want to have a main atlas that has every asset I need for the current background, this one will be created at initialisation.
    And, asynchronously generate the atlas for the next level.
    when we go the next level we swap the atlasses, the generated one becomes the main, and the main becomes the new buffer for the next atlas etc..

    Note: when i say "Atlas" it can be several, if my texture is more than 8192 x 8192 it will create another atlas, and everything will be stored depending on the depth on the screen (to optimise draw call)
     
  6. pep_dj

    pep_dj

    Joined:
    Nov 7, 2014
    Posts:
    178
    The approach I tried is the link you saw in my post: https://github.com/DaVikingCode/UnityRuntimeSpriteSheetsGenerator

    Instead of using dynamic texture atlas, I think it's better to use Unity as it's supposed to... you will have less problems. You can use Unity built-in tile system for the backgrounds, so you will save memory.