Search Unity

SpriteManager - draw lots of sprites in a single draw call!

Discussion in 'iOS and tvOS' started by Brady, Jan 15, 2009.

  1. Aberrations

    Aberrations

    Joined:
    Jan 31, 2013
    Posts:
    2
  2. mrs

    mrs

    Joined:
    Feb 6, 2013
    Posts:
    1
    hi, i have 200 sprites and need a single collection of these for my animation. is the number 200 a big one? i cant "commit" it during sprite collection making. can anyone pls help me with this. :)
     
  3. par-002

    par-002

    Joined:
    Jul 4, 2012
    Posts:
    52
    I am actually having the multiple sprite managers rendering issue. I have scoured this thread and tried pretty much everything but I cannot seem to get anything to work.

    I have 2 SpriteManagers. 1 for backgrounds and 1 for items above the backgrounds. Unfortunately, the backgrounds are always showing above the foregrounds in perspective mode (works fine in ortho).

    Details:

    I am using Perspective Camera as we will be mixing 2D sprites w/ 3D models. When I switch to ortho on the main camera, the foreground sprites do render correctly on top of the background sprites. Switch back to perspective and they no longer show.

    My current scene is top down so I have rotated the camera 90 degrees on its X axis so it looks straight down. I am using planes for all of the gameobjects that the sprites attach to. They are correctly oriented so that anything looking down at them from a higher Y position will see them correctly.

    I have changed the Awake() method in the SpriteManager class so that it doesnt change the transform position to Vector3.zero (it keeps the original position).

    I have explicitly set the background manager to a lower Y value than the foreground manager. Doesn't make any difference. Background is always shown where foreground never shows (perspective only).

    I have moved everything around all over the place but no matter what I do, in perspective mode the background images will always render on top of the foreground images.

    It is very frustrating.

    Any advice?

    Thanks!

    PAR
     
  4. par-002

    par-002

    Joined:
    Jul 4, 2012
    Posts:
    52
    Alright I got it.

    SpriteManager backgroundManager;
    SpriteManager foregroundManager;

    backgroundManager.material.renderQueue = 0;
    foregroundManager.material.renderQueue = 1;

    Works exactly as I need it to.

    PAR
     
  5. Rocketballs

    Rocketballs

    Joined:
    Sep 13, 2012
    Posts:
    23
    I'm having a weird issue with my project.
    I have 5 sprites parented to the camera and if I move the camera past y=10.1 the sprites disappear.
    I also have another spritemanager object with a different atlas and those sprites disappear past y=17.9.
    They can still be seen if I lower the camera y value. They are not removed, it's more like they're hidden.

    What could be causing this? :confused:
     
  6. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    Are they disappearing behind something else? It sounds like a depth-sorting issue.
     
  7. Rocketballs

    Rocketballs

    Joined:
    Sep 13, 2012
    Posts:
    23
    I tried it just now without the background and they still get "turned off".
    It's somehow related to depth though.
    The ones in the front disappear first (y 10.1) then the middle ones (y 17.95) and the back sprites (y 25.6)
     
  8. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,928
    I think Sprite´s "Clear" method really should be doing:

    m_hidden___DoNotAccessExternally = false;

    in addition to

    hidden = false;

    That´s because when hiding sprites, then destroying them you can end up with m_hidden___DoNotAccessExternally = true; and hidden = false;
    So, when the sprite gets reused and you do sprite.hidden = true; this statement in the setter:

    if (value == m_hidden___DoNotAccessExternally)
    return;

    prevents the sprite from hiding, because m_hidden___DoNotAccessExternally is still true. And you are left with garbage sprites on the screen, which are pretty hard to debug ;).
     
  9. Android Matt

    Android Matt

    Joined:
    May 24, 2012
    Posts:
    61
    I was wondering if this might be useful for my particular situation...

    I am working on a strategy game that uses a tile map consisting of 2D-hexagons. At the moment each tile is a game object, with it's a material which is determined programatically from a list at the map generation stage. While this works perfectly in terms of the logistics of programming, and the resulting game play, I obviously have a lot of draw calls that I suspect I may need to trim down when I get to finally testing it on an iOS device (~50 draw calls at the moment, without much else going on) and also when I start adding a bit more polish..but I won't be in a position to test it on an iOS device for a few months (long story).

    Is it likely that I might be able to employ this SpriteManager in some way in order to optimize and reduce my draw calls? Given that my tile map is just a series of 2D textured hexagons, it seems like there might be a good way to optimize this.

    For the record, I'm going to post a very similar question in the Scripting section regarding general optimization options in my situation.
     
    Last edited: Apr 18, 2013
  10. G-S-Pratap

    G-S-Pratap

    Joined:
    Apr 29, 2013
    Posts:
    4
    Hi Brady,

    How to generate colliders for the sprites, that are like terrain images. I am developing a game using SM2. Please help me on this

    Thanks for the SM2 its very easy to learn, and gives awesome performance.
     
  11. Hemaolle

    Hemaolle

    Joined:
    Jun 25, 2013
    Posts:
    10
    I noticed a problem with the SpriteManager when creating animations that span multiple rows in the sprite sheet. I fixed that in my project by modifying BuildUVAnimation like this:

    The current BuildUVAnimation:
    Code (csharp):
    1.  
    2. public Vector2[] BuildUVAnim(Vector2 start, Vector2 cellSize, int cols, int rows, int totalCells, float fps)
    3.     {
    4.         int cellCount = 0;
    5.  
    6.         frames = new Vector2[totalCells];
    7.         framerate = fps;
    8.  
    9.         frames[0] = start;
    10.  
    11.         for(int row=0; row < rows; ++row)
    12.         {
    13.             for(int col=0; col<cols  cellCount < totalCells; ++col)
    14.             {
    15.                 frames[cellCount].x = start.x + cellSize.x * ((float)col);
    16.                 frames[cellCount].y = start.y - cellSize.y * ((float)row);
    17.  
    18.                 ++cellCount;
    19.             }
    20.         }
    21.  
    22.         return frames;
    23.     }
    24.  
    My version:
    Code (csharp):
    1.  
    2. public Vector2[] BuildUVAnim(Vector2 start, Vector2 cellSize, int cols, int rows, int totalCells, float fps)
    3.     {
    4.         int cellCount = 0;
    5.  
    6.         frames = new Vector2[totalCells];
    7.         framerate = fps;
    8.  
    9.         frames[0] = start;     
    10.  
    11.         bool firstCell = true;
    12.        
    13.         for(int row=0; row < rows; ++row)
    14.         {          
    15.             for(int col=0; col<cols  cellCount < totalCells; ++col)
    16.             {
    17.                 if (firstCell) {
    18.                     col = (int)(start.x / cellSize.x);
    19.                     firstCell = false;
    20.                 }
    21.                 frames[cellCount].x = cellSize.x * ((float)col);
    22.                 frames[cellCount].y = start.y - cellSize.y * ((float)row);
    23.  
    24.                 ++cellCount;
    25.             }
    26.         }
    27.  
    28.         return frames;
    29.     }
    30.  
     
  12. M1k4

    M1k4

    Joined:
    Jun 8, 2013
    Posts:
    6
    Is there any tutorials out there for this?
     
  13. dav793

    dav793

    Joined:
    Dec 21, 2013
    Posts:
    7
    Has anyone managed to figure this one out? I'm having the same problem. Been trying all I could think of, but it seems hard to debug.

    In my case, the sprites continue showing in the scene view window, but they disappear in the game window (i.e. the camera stops rendering them, but they're there, not hidden, and within the camera's clipping planes).

    Any help would be immensely appreciated. This plugin has improved my projects performance by a whole lot, it'd be a shame to have to discard it because of this problem.

    By the way, the problem only seems to happen when calling RemoveSprite(). If I stop removing sprites, the problem never occurs but the scene becomes cluttered with unnecessary sprites, so this is not an option.
     
    Last edited: Dec 21, 2013
  14. dav793

    dav793

    Joined:
    Dec 21, 2013
    Posts:
    7
    Nevermind. It still happens, only less frequently.
     
  15. SamTheBay

    SamTheBay

    Joined:
    Jan 11, 2014
    Posts:
    14
    Hey Guys,

    Thanks for this free code, it is super useful :). I had a question I was hoping someone could point me in the right direction for. I was hoping I could mix the use of this with regular unity 2d sprite renderers, but when I do that the unity sprites are always hidden behind the mesh that is drawn by the sprite manager. Does anyone know how I could get the sprite renderer to draw the mesh behind the unity sprites?

    Thanks!

    Sam
     
  16. SamTheBay

    SamTheBay

    Joined:
    Jan 11, 2014
    Posts:
    14
    nm, I managed to figure this out :). You can set the SortingLayer on the mesh renderer.