GumBatch
Introduction
GumBatch is an object which supports immediate mode rendering, similar to MonoGame's SpriteBatch. GumBatch can support rendering text with DrawString as well as any IRenderableIpso.
For information on getting your project set up to use GumBatch — including how to wire up KernSmith so you do not need to ship any .fnt files — see the Setup for GumBatch page.
GumBatch draws only what you pass it. In some cases controls may create additional controls on the popup layer, such as ComboBox and Tooltip. If your UI includes these controls, you may need to also draw your popup layers, as shown in the following code:
// Draw
Core.GumBatch.Begin();
Core.GumBatch.Draw(YourCustomObjects);
// Now draw the popup root so popups show up
Core.GumBatch.Draw(GumService.Default.PopupRoot);
Core.GumBatch.End();Relationship with the Camera
GumBatch draws through the same Camera as the rest of Gum — the one at GumService.Default.Renderer.Camera. It does not ignore the camera: the camera's Zoom, Position (X and Y), and CameraCenterOnScreen all apply to everything you draw between Begin and End. So if you zoom the camera to handle a window resize (see Resolution and Resizing the Game Window), your GumBatch output zooms along with the rest of your UI.
Begin also refreshes the camera's client dimensions from the current GraphicsDevice.Viewport on every call, so GumBatch always matches the live viewport size.
Begin accepts an optional transform: Begin(Matrix). This matrix composes on top of the camera transform rather than replacing it — the effective transform is your matrix multiplied with the camera's view.
Because the matrix composes on top of the camera, setting Camera.Zoom to a non-default value and passing a scaling matrix to Begin(Matrix) applies the scale twice. Drive scaling from a single source: either leave the matrix off and use Camera.Zoom, or pass a matrix and keep Camera.Zoom at 1.
Rendering TextRuntimes
The most flexible way to draw text with GumBatch is to create a TextRuntime. TextRuntimes support all of Gum's layout rules — wrapping, alignment, rotation, sizing — and integrate with Gum's font system so you can set Font and FontSize directly and let KernSmith create the atlas on demand.
The following code shows how to create a TextRuntime and render it using GumBatch:

If you would rather load a specific .fnt file instead of using KernSmith, set UseCustomFont = true and assign CustomFontFile to the path of the .fnt file. See Custom Font File for details.
Rendering Strings
GumBatch can also render strings directly via DrawString. This is a lower-level API than TextRuntime — it does not support layout, wrapping, or rotation — but the call shape matches SpriteBatch.DrawString and is convenient for quick HUD-style text.
DrawString takes a BitmapFont. The recommended way to obtain one is to ask KernSmith for it directly:
The following code renders a string at X = 100, Y = 200:

Multiple strings can be rendered between Begin and End calls:

DrawString can accept newlines and color the text:

If you would rather load a pre-built .fnt file instead of generating with KernSmith, you can construct a BitmapFont directly:
This is useful when you have a hand-tuned font atlas (for example one produced by the Gum tool's FontCache or by an external tool such as bmfont). See File Loading for the file resolution rules.
Rendering Parent/Child Hierarchy
GumBatch.Draw renders any argument renderable object. If the object has children, then the Draw call performs a hierarchical draw, respecting the parent/child relationship to control draw order.
For example, the following code creates a parent ColoredRectangleRuntime and a child TextRuntime:
Since the Draw call is only called on the Parent, then only the Parent reference is kept at class scope:

Mixing with SpriteBatch
GumBatch wraps an internal SpriteBatch and exposes it through the SpriteBatch property. This lets you issue your own SpriteBatch draw calls between GumBatch.Begin and GumBatch.End without managing a second batch — both your draws and Gum's draws land in the same batch, so they share sort order, blend state, and transform matrix.
This is useful when you want to draw a few of your own textures alongside Gum's immediate-mode output without paying for two Begin/End pairs.
The underlying SpriteBatch instance is stable for the lifetime of the GumBatch — Gum may mutate its state (clip regions, blend, transform) but never swaps the instance — so it is safe to cache the reference if you want.
You are sharing a batch with Gum, so any state you change on the SpriteBatch directly (e.g. by calling End and re-issuing Begin with different parameters) will affect subsequent Gum draws. If you need independent state, use your own separate SpriteBatch instead.
RenderTargets
GumBatch can be used to render Gum objects on RenderTarget2Ds, just like regular SpriteBatch calls.
The following code shows how to render on a RenderTarget:
Note that if you are rendering multiple objects on a render target, the BlendState must be set as to add the transparency. Using the default BlendState may result in alpha being "removed" from the render target when new instances are drawn.
The following shows how to create a BlendState for objects which have partial transparency and are to be drawn on RenderTargets:
Last updated
Was this helpful?

