Search Unity

Sprite Atlas cannot reduce draw call

Discussion in '2D' started by yvesyiu, Jul 17, 2019.

  1. yvesyiu

    yvesyiu

    Joined:
    Dec 23, 2016
    Posts:
    10
    I have create a Sprite Atlas to batch the sprites
    when i try to load the sprite Atlas from the resources folder
    And save them into a dictionary

    Then set the sprite of my spriteRenderers to the relevant sprite
    one draw call is taken by each sprite

    Then I tried to check from the frame debugger
    It return object have different MaterialPropertyBlock set

    Does anyone know why I got this?
    Thank you
     

    Attached Files:

  2. yvesyiu

    yvesyiu

    Joined:
    Dec 23, 2016
    Posts:
    10
    Here is my code
    Code (CSharp):
    1. SpritesDictionary = new Dictionary<Id, Sprite>();
    2.         SpriteAtlas = Resources.Load<SpriteAtlas>("MyPath");
    3.         foreach (Id id in IdList)
    4.         {
    5.             SpritesDictionary .Add(id, SpriteAtlas .GetSprite(id.ToString()));
    6.         }
    Code (CSharp):
    1. MySpriteRenderer.sprite = SpritesDictionary [myid]
     
    Last edited: Jul 17, 2019
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    GetSprite should still reference the atlas so I'm not sure what is breaking your batching.

    You should be able to reference the individual sprites as you normally would, and if they are assigned to the spriteatlas, they will be packed together and their materials will use the same texture. Is this dictionary fetching necessary?
     
  4. yvesyiu

    yvesyiu

    Joined:
    Dec 23, 2016
    Posts:
    10
    Thanks for your response

    I have tried to assign all the sprites to SpriteRenderer

    The frame debug shows that they are in the same texture
    but it still returns objects have different MaterialPropertyBlock set
     

    Attached Files:

  5. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Would it be possible to share a screenshot of the previous or following Draw Dynamic step of the Frame Debugger? This would make it easier to determine what in the MaterialPropertyBlock has changed between each step. Thanks!

    Edit (did not see new post):
    The atlased texture names look different from the first post and your new post. Are they both steps which are one after the other?
     
  6. yvesyiu

    yvesyiu

    Joined:
    Dec 23, 2016
    Posts:
    10
    Do you mean screenshot like this?
    The draw dynamic steps above these screenshots are other sprites

    I have created 3 Sprite Atlas for 3 different kinds of sprites
    So the name is different, sorry for if that misleading you.
     

    Attached Files:

  7. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Hi, yes, I do mean screenshots that are in order of rendering for the FrameDebugger, which you have provided. Thanks!

    Based on the screenshots, each Draw Dynamic step does have a different MaterialPropertyBlock set where the _MainTex property has changed each step:
    1. GreenPoint
    2. Foods-8
    3. FrogPor
    4. Foods-8
    5. FrogPor
    It seems that each DrawCall is switching between two atlases, Foods-8 and FrogPor, for its texture? Is that the correct setup for your Sprites and SpriteAtlases?
     
  8. yvesyiu

    yvesyiu

    Joined:
    Dec 23, 2016
    Posts:
    10
    These steps are only part of it or do you need me to show more screenshot?
    There is another sprite atlas I didn't show called LandMa
    In the Frame Debugger, 3 of them are switching each other

    Why is that happen?
    Do I have to make sure that unity draw all Foods first
    then draw the FrogPor and LandMa ?
     
  9. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Without knowing much about the Scene, it seems that the Sprites in the Scene are sorted in a way that the rendering is alternating between these two atlases. Dynamic draw call batching will not work if this is the case.

    If you could show a screenshot of the Game View and identify the Sprites being rendered in the order shown in the Frame Debugger, that would be helpful. Alternatively, if it is easier, you could share your project as well!
     
  10. yvesyiu

    yvesyiu

    Joined:
    Dec 23, 2016
    Posts:
    10
    Here it is
     

    Attached Files:

  11. yvesyiu

    yvesyiu

    Joined:
    Dec 23, 2016
    Posts:
    10
    .
     

    Attached Files:

  12. yvesyiu

    yvesyiu

    Joined:
    Dec 23, 2016
    Posts:
    10
    It seems that screenshot_7 and screenshot_10 should batch together
    screenshot_9 and screenshot_11 should batch together
     

    Attached Files:

  13. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Could you share the Inspectors of the SpriteRenderers of the items shown in the screenshots? If they have the same sorting properties, like sorting order and layer, they should be sorted and ordered according to their material and be batched together. This would be as you have mentioned where 7 and 10 would batch together.

    If you could share the version of Unity you are using, that would be great too.
     
  14. yvesyiu

    yvesyiu

    Joined:
    Dec 23, 2016
    Posts:
    10
    Sorry for reply late
    I am using Unity 2019.1.1f1

    I have tried to open another scene
    Load the sprite Atlas from the resources folder
    Create lots of Sprite Renderer and
    get the sprite from the dictionary the same way as you can see above
    the draw call is only one

    I have no idea why:(
     

    Attached Files:

  15. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    Maybe it would be easier if you could post a (stripped down) project that Unity Technologies can use to reproduce the issue?
     
  16. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    I think we would need to take a deeper look at your project in order to help you for this.

    You can share your project by filing a bug with the Unity Bug Reporter with your project. Do post the case number here too, thanks!

    If possible, you should have only the related elements in your project (Sprites, Sprite Atlas, the Sprite Atlas script etc).
     
  17. yvesyiu

    yvesyiu

    Joined:
    Dec 23, 2016
    Posts:
    10
    Thank you for your help
    I finally find out the problem from this post
    the sprites shouldn't be ordered in the same sorting order
    forum.unity.com/threads/dynamic-batching-break-with-sorting-layer-and-order-in-layer.428046
     
  18. StrawberryStatement

    StrawberryStatement

    Joined:
    Mar 5, 2020
    Posts:
    5
    2019.4.16f1c1
    I have the same problem, and finally I resolve it by changing different sort order for each spriteatlas
     
    Last edited: Dec 2, 2021