Skip to content

SpriteBatch class reference

Alan Stagner edited this page Jun 9, 2020 · 1 revision

Methods

void Begin()
void Begin(SpriteBlendMode blendMode)
void Begin(SpriteBlendMode blendMode, SpriteSortMode sortMode, bool saveState, Matrix4x4 transform)

Begin a new sprite batch. You must call this before calling any Draw methods. Passing a sortMode allows the sprite batch to sort the sprites you draw by some criteria - BackToFront sorts by depth putting sprites with a higher depth value on top, FrontToBack sorts by depth putting sprites with a lower depth value on top, Texture attempts to sort sprites by the texture they use, and Immediate does no sorting. Passing true for "saveState" restores any changes made to the current render state after the call ends.


void Draw(Texture2D texture, Vec2 position, Color color)
void Draw(Texture2D texture, Rect rect, Color color)
void Draw(Texture2D texture, Vec2 position, Rect? sourceRect, Color color)
void Draw(Texture2D texture, Rect rect, Rect? sourceRect, Color color)
void Draw(Texture2D texture, Rect rect, Rect? sourceRect, Color color, float rotation, Vec2 origin, SpriteFlags flags)
void Draw(Texture2D texture, Vec2 position, Rect? sourceRect, Color color, float rotation, Vec2 origin, float scale, SpriteFlags flags, float depth)
void Draw(Texture2D texture, Vec2 position, Rect? sourceRect, Color color, float rotation, Vec2 origin, Vec2 scale, SpriteFlags flags, float depth)

Draw a sprite to the batch from the given texture. If you specify a source rectangle, you can draw a portion of a sprite sheet. If you specify depth, sprites can optionally be sorted depending on the sort mode passed to Begin. You can also flip sprites horizontally and vertically as well using the SpriteFlags.


void End() - End the current sprite batch and draw sprites. This call sorts the sprites and converts them into draw calls, batching them wherever possible (drawing multiple subsequent sprites from the same texture will attempt to batch those sprites into the same draw call).