Search Unity

Optimization using the new 2D tools

Discussion in '2D' started by Mr-Oliv, Nov 14, 2013.

  1. Mr-Oliv

    Mr-Oliv

    Joined:
    Sep 14, 2012
    Posts:
    33
    I have a couple of questions regarding optimization using the new 2D tools.

    - Rendering. I'm curious if using the Sprite Renderer has any performance gains compared to using regular GameObjects. It looks like all the Sprite Renderers in a scene are baked together somehow (since a sprite with a higher "Order in Layer" ends up in front of other sprites even if it's further away on the z-axis), but at the same time, I get the same amount of draw calls as if I would've used GameObjects with plane-shapes. It would be interesting to know how this works under the hood.

    - Static batching. From my experience, static batching is one of the biggest cpu savers when targeting mobile platforms. Any thoughts on how to use static batching with objects created using the Sprite Renderer?

    Thanks!
     
  2. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
    Hi,

    1. Static batching for Sprites is not in 4.3 - only dynamic batching is. And it now works with non-uniformly scaled Sprites.
    2. SpriteRenderer is a lightweight version of the MeshRenderer, so there are some performance benefits to using it.
    3. SortingLayers are checked at the beginning of Unity's sorting algorithm. It now looks like this: Opaque/Transparent queue -> Sorting Layer -> Depth in Layer -> Old Logic (Z, material queue, etc.)
     
  3. Mr-Oliv

    Mr-Oliv

    Joined:
    Sep 14, 2012
    Posts:
    33
    Very helpful, Thanks!
     
  4. eviltenchi_84

    eviltenchi_84

    Joined:
    Feb 18, 2010
    Posts:
    99
    is there an eta on static batching for sprites?
     
  5. esalczynski

    esalczynski

    Joined:
    Nov 21, 2012
    Posts:
    5
    I'm using the Sprite Renderer component in 4.3 for two different characters on screen. Each character has a different set of animations. All of the animations are on the same Texture with the Texture Type set to "Sprite." Both Sprite Renderers have the "Sprites-Default" material.

    When I view the stats in the Game window, I see"Saved by batching: 0."

    I expected that number to be greater than zero, since all of my sprites are on the same sprite sheet.

    Is this number inaccurate during debugging? Am I doing something wrong? Or does dynamic batching not actually work with the Sprite Renderer?
     
  6. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    Are your draw calls already pretty low? It sounds like you already have things optimized if everything is already on a single texture.
     
  7. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Anything between 10-30 on a modern mobile is unmeasurable IMHO.
     
  8. esalczynski

    esalczynski

    Joined:
    Nov 21, 2012
    Posts:
    5
    My draw call count is pretty low, but that's because this is just a test scene to see how some of the 2D tools work.

    Based on what I'm seeing in the statistics overlay - compared to using a 3rd party plugin for sprite management in previous versions - it appears as though Unity doesn't dynamically batch the draw the calls for the Sprite Renderer. I could be reading it wrong or totally misunderstanding what happens with the Sprite Renderer behind the scenes. If I'm not, and there is no batching, once I do start putting together an actual scene, I may have to switch back to those 3rd party plugins.
     
  9. autonomous411

    autonomous411

    Joined:
    May 22, 2012
    Posts:
    9
    I've been working on a 2d game. I like the sprite system...but they need to optimize the draw calls. It should only be 1 draw call per sprite sheet or atlas. I had a level with 4 sprites from 4 different images...I could understand 4 draw calls, but there were 1300 draw calls! Now I'm scrambling to find a system that can display a tiled background using a single draw call...orthello is nice, but it's not setup to work with 4 cameras (it's a 4 player couch game). Any ideas?
     
  10. shopguy

    shopguy

    Joined:
    Jan 12, 2013
    Posts:
    282
    Seeing something also, but in my case it is pretty weird. I think maybe it has to do with layers or "order in layer" setting. I have 3 different sprite textures (each texture has 4 slices/sprites), and several objects using each of those textures. I'm just testing, so I have empty game objects setup in my Hierarchy view, labeled "1", "2", "3". Under each one I have 20-100 sprites (game objects with sprite renderer + transform only), everything under "1" uses sprite sheet 1, "2" uses sprite sheet 2, and same for "3".

    If I just have everything visible/enabled and run my project the Draw Calls are 184 (saved by batching = 337). If I disable "1", "2", "3" so that none of my sprites are visible, my Draw Calls are 1 (I have one GUIText to show FPS). If I then re-enable those 3 items (just by checking them in the editor not via scripts) while my game is running, my Draw Calls don't go back up to 184 like I'd expect, they go up to 5 (saved by batching = 516). Each on I check adds 1 more Draw Call, with the last one that I check (with the most objects) adds 2 Draw Calls (I assume it hits some batching limit so needs 2 draw calls for the larger group).

    How can I get the 5 Draw Calls when I start my project, not have to disable/re-enable everything?

    Or is Unity just auto-optimizing a specific way because I'm running on a PC? If I run on a mobile device (my main target) will it use less when needed? If so, kind of makes the Stats window lame, since I can't view that info on mobile so have no idea what Unity is doing behind the scenes and don't know what I need to optimize/change? (Using indie version, so profiler not an option)

    Edit: I haven't set anything for layers or "order in layer" setting, they are all on Default layer and 0 order.
     
  11. shopguy

    shopguy

    Joined:
    Jan 12, 2013
    Posts:
    282
    I think I answered part of my question. Does Unity just optimize differently for Mobile? Answer: No.

    If I use this code:

    public GameObject Obj1;
    public GameObject Obj2;
    public GameObject Obj3;
    void Start()
    {
    Obj1.active = false;
    Obj2.active = false;
    Obj3.active = false;
    Obj1.active = true;
    Obj2.active = true;
    Obj3.active = true;
    }

    And Obj1 - Obj3 are set to the 3 empty game objects that contain my 3 different sets of sprites. If I build and run on my Win8 tablet, it runs at 60 FPS. If I just comment out that code (just the 6 lines in Start), and rebuild, it runs at 30 FPS.

    The above code also "fixes" the issue with the Stats display when running in the editor, it starts out with only 5 Draw Calls. So possibly this is an acceptable fix for me, or maybe will not even matter once I write my real code, since I'll be using GameObject.Instantiate() to create the world... but if the optimization/batching is this "picky" that worries me. Should I be worried?

    Edit: Found Solution - It was the "Order in Layer" causing the problem, and/or the order the objects are added to the scene. Once I changed to some script for creating my objects, instead of copy/paste, this was much easier to play around with and obvious. If I created the objects in a mixed order, 1st using sheet 1, 2nd using sheet 2, 3rd using sheet 3, so on.. the Draw Calls count would be huge. If I created all that use sheet 1 first, then all that use sheet 2 2nd, then sheet 3 -- the Draw Calls are only 1 per sprite sheet as I would expect.

    Finally I set "Order in Layer" values, 1 for all objects using sheet 1, 2 for all objects using sheet 2, etc -- and that fixed the problem no matter what order I added them to my scene. Sadly, that isn't practical in my game, to order things based on what texture they are using, but at least now I know how the system works and can maybe design around it.
     
    Last edited: Mar 30, 2014
  12. DalerHakimov

    DalerHakimov

    Joined:
    Mar 14, 2014
    Posts:
    302
    shopguy,

    Thanks for sharing your experience.. Did you try the built-in Sprite Packer in Unity to see changes in performance? or anyone else??
     
  13. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    The reason for this is that sorting will require another draw call. Try to keep the same atlas contents on the same depth where possible. If apples need to be drawn all in front, that's usually one call. But if some apples are behind and in front that might be more draw calls due to sorting. If you can use a larger atlas with everything in it - this problem can mostly vanish.
     
  14. griden

    griden

    Joined:
    Apr 8, 2013
    Posts:
    33
    @shopguy:

    • I don't think the object creation order matters, but I might be wrong.
    • Creating a Sorting Layer for each atlas worked good for me at least.
    • Sprites from the same atlas don't need to use the same "Order in Layer"as long as they share the same Sorting Layer. You can control the drawing order by using as many "Order in Layer"'s as you want, as long as you use subsequent numbers - 0, 1, 2, 3, etc. They will be batched together in one draw call.

    In the end, you should be able to get 1 draw call for each Atlas/Sorting Layer.