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. Dismiss Notice

Question How can i reduce draw calls in 2d urp?

Discussion in '2D' started by dhshin05, May 20, 2023.

  1. dhshin05

    dhshin05

    Joined:
    Jul 5, 2017
    Posts:
    4
    Hello. After getting a lot of information through the Unity forum, I started developing it. thank you

    Searching at first I saw a simple way to lower the draw call through a sprite atlas. However, when I use mat.SetFloat as a script, the material becomes an instance, and the draw call increases.

    What I've found since then is GPU Instancing. But it still feels difficult to me! The original shader was created through shader graph, but was rewritten as shader code to enable GPU Instancing.

    And it says that you can use Mpb, but I don't understand how it should be connected.

    Code (CSharp):
    1.  MaterialPropertyBlock Mpb = new MaterialPropertyBlock();
    2. Mpb. SetFloat(Flash, 0f);
    3. GetComponent<SpriteRenderer>.SetPropertyBlock(Mpb);
    Is the same way correct? But it doesn't reduce draw calls.
    As a result of checking with the Frame Debugger, it was rendered from the top.

    11.png


    Is it because of the settings for 2d sort?

    It may sound strange by turning the translator, but thank you for reading.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Do you have an ACTUAL performance problem or is this just speculative optimization?

    DO NOT OPTIMIZE "JUST BECAUSE..." If you don't have a problem, DO NOT OPTIMIZE!

    If you DO have a problem, there is only ONE way to find out. Always start by using the profiler:

    Window -> Analysis -> Profiler

    Failure to use the profiler first means you're just guessing, making a mess of your code for no good reason.

    Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.

    https://forum.unity.com/threads/is-...ng-square-roots-in-2021.1111063/#post-7148770

    Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.

    Notes on optimizing UnityEngine.UI setups:

    https://forum.unity.com/threads/how...form-data-into-an-array.1134520/#post-7289413

    At a minimum you want to clearly understand what performance issues you are having:

    - running too slowly?
    - loading too slowly?
    - using too much runtime memory?
    - final bundle too large?
    - too much network traffic?
    - something else?

    If you are unable to engage the profiler, then your next solution is gross guessing changes, such as "reimport all textures as 32x32 tiny textures" or "replace some complex 3D objects with cubes/capsules" to try and figure out what is bogging you down.

    Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.

    This sort of speculative optimization assumes you're properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.
     
  3. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    The SRP batcher batches at the shader level not the material level when rendering in 3D however it doesn't with the 2D renderer.

    In 2023.1 it does though.
    • 2D: Added SRP Batching for 2D Renderers and Particle Renderer to support URP.
     
  4. dhshin05

    dhshin05

    Joined:
    Jul 5, 2017
    Posts:
    4
    Thank you for those kind words.

    My game resembles Vampire Survivors.

    Drawcalls exceeded 1000 when there were a lot of monsters, which led to frame drops.

    I thought this part had to be optimized, and that method was to reduce drawcalls.
     
  5. dhshin05

    dhshin05

    Joined:
    Jul 5, 2017
    Posts:
    4
    thank you

    Does this mean that the way I do it is the right way?
    (It can be solved by fixing only that part because the shader code or script is not written well)
     
  6. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    Are you already using sprite atlassing?
     
  7. dhshin05

    dhshin05

    Joined:
    Jul 5, 2017
    Posts:
    4
    Yes, the sprite atlas is being applied.

    If you delete the code that accesses the material in the script with sprite-default, drawcalls will spawn about 2 even if you spawn 500.

    Here, I thought that 2 renders the top and bottom of the character separately due to 2d sorting.