SpriteFont

AdvancedProgrammer SpriteFont class is a convenient way to draw text. It works with the SpriteBatch class.

Note

Custom scene renderer to include it in the composition.

Load a spriteFont

SpriteFont instance using the @’Xenko.Core.Serialization.Assets.ContentManager’. It contains all the options to display a text (bitmaps, kerning, line spacing etc). Code: Load a SpriteFont

  1. var myFont = Content.Load<SpriteFont>("MyFont");

Write text on screen

SpriteBatch. The @’Xenko.Graphics.SpriteBatch.DrawString’ method performs the draw. For more information about the SpriteBatch, see the SpriteBatch page. Code: Write text

  1. // create the SpriteBatchvar spriteBatch = new SpriteBatch(GraphicsDevice);// don't forget the beginspriteBatch.Begin(GraphicsContext);// draw the text "Helloworld!" in red from the center of the screenspriteBatch.DrawString(myFont, "Helloworld!", new Vector2(0.5, 0.5), Color.Red);// don't forget the endspriteBatch.End();

SpriteEffects to the text:

  • Code: Advanced text drawing
    1. // draw the text "Hello world!" upside-down in red from the center of the screenspriteBatch.DrawString(myFont, "Hello world!", new Vector2(0.5, 0.5), Color.Red, 0, new Vector2(0, 0), new Vector2(1,1), SpriteEffects.FlipVertically, 0);

    See also

  • SpriteBatch