Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Make Graphics.drawMeshInstancedIndirect work when game/editor is paused in SRP

Discussion in 'General Graphics' started by I208iN, Feb 15, 2022.

  1. I208iN


    Mar 25, 2017

    This is a post to help people who have had the same problem I've had, with the keywords I've searched for. I've posted this here and not in the SRP forum because that where you search when you don't know this issue is an SRP issue.

    If you are like me, you have Graphics.drawMeshInstancedIndirect in an Update method on a Monobehaviour script and now you would like to make it work when the game is paused, but obviously Update isn't called.

    Old answers like using Camera.onPreCull don't work in SRP (see this thread).

    The solution I've found is to stop calling Graphics.drawMeshInstancedIndirect in an Update function (like shown in the documentation) and create instead a custom render feature for the scriptable render pipeline, thanks to this example in this example project.

    The means I've had to use CommandBuffer.DrawMeshInstancedIndirect instead which doesn't have the same signature and functionalities, so you may have more digging to do. It was fine in my case.

    I'm not sure it's the right way to do this but hopefully it can help you.

    Here is the code for the renderer feature:
    Code (CSharp):
    1. public class GameSpriteRendererFeature : ScriptableRendererFeature
    2. {
    3.     class CustomRenderPass : ScriptableRenderPass
    4.     {
    5.         public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    6.         {
    7.             CommandBuffer commandBuffer = CommandBufferPool.Get("GameSprite");
    9.             // eventually calls commandBuffer.DrawMeshInstancedIndirect(...) if the layers match
    10.             ElementRenderer.RenderElements(commandBuffer,;
    12.             context.DrawWireOverlay(;
    13.             context.ExecuteCommandBuffer(commandBuffer);
    15.             CommandBufferPool.Release(commandBuffer);
    16.         }
    17.     }
    19.     CustomRenderPass afterRenderingPrepasses;
    21.     public override void Create()
    22.     {
    23.         afterRenderingPrepasses = new CustomRenderPass();
    25.         // I don't fully grasp how these work yet, this one works for me for now
    26.         // there is a list in the docs, you may also need several
    27.         afterRenderingPrepasses.renderPassEvent = RenderPassEvent.AfterRenderingPrePasses;
    28.     }
    30.     public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    31.     {
    32.         renderer.EnqueuePass(afterRenderingPrepasses);
    33.     }
    34. }
    Finally you have to add this feature to your Universal Renderer Data scriptable object (the one created by default in Assets/Settings/UniversalRenderer.asset). I don't know how HDRP works but hopefully it's the same.
    NightElfik likes this.