上級 プログラマー
@'Stride.Graphics.SpriteFont' クラスは、テキストを描くのに便利なクラスです。@'Stride.Graphics.SpriteBatch' クラスとあわせて使用します。
[!Note] カスタムコードをコンポジションに含めるには、すべてのカスタムコードをカスタム シーン レンダラーに記述する必要があります。
フォントアセットをコンパイルした後は、@'Stride.Core.Serialization.Contents.ContentManager' を使って、@'Stride.Graphics.SpriteFont' インスタンスとして読み込むことができます。これは、テキストを表示するためのすべてのオプション(ビットマップ、カーニング、行間など)を含んでいます。
Code: スプライトフォントを読み込む
var myFont = Content.Load<SpriteFont>("MyFont");
フォントを読み込んだら、@'Stride.Graphics.SpriteBatch' で任意のテキストを表示することができます。描画を行うのは、@'Stride.Graphics.SpriteBatch.DrawString' メソッドです。スプライトバッチの詳細については、スプライト バッチを参照してください。 <!-- Once the font is loaded, you can display any text with a @'Stride.Graphics.SpriteBatch'. The @'Stride.Graphics.SpriteBatch.DrawString' method performs the draw. For more information about the SpriteBatch, see the SpriteBatch page. -->
コード: テキストを書く
// スプライトバッチを作成
// create the SpriteBatch
var spriteBatch = new SpriteBatch(GraphicsDevice);
// 開始を忘れないこと
// don't forget the begin
spriteBatch.Begin(GraphicsContext);
// "Helloworld!" というテキストを、赤字で画面の中央に描画
// draw the text "Helloworld!" in red from the center of the screen
spriteBatch.DrawString(myFont, "Helloworld!", new Vector2(0.5, 0.5), Color.Red);
// 終了を忘れないこと
// don't forget the end
spriteBatch.End();
さまざまなオーバーロードメソッドを使って、テキストの向き、拡大率、深さ、原点などを指定できます。また、テキストに次のような @'Stride.Graphics.SpriteEffects' を適用することもできます。
コード: 高度なテキスト描画
// "Hello world!" というテキストを、赤字で画面の中央に、上下逆さまに描く
// draw the text "Hello world!" upside-down in red from the center of the screen
spriteBatch.DrawString(myFont, "Hello world!", new Vector2(0.5, 0.5), Color.Red, 0, new Vector2(0, 0), new Vector2(1,1), SpriteEffects.FlipVertically, 0);