在XNA中为纹理绘制一个字符串?

时间:2011-12-08 03:51:45

标签: fonts xna textures

非常简单。我想带一个像“Bananas”这样的字符串,在它上面拍一个SpriteFont,然后将其呈现为Texture2D而不是直接到SpriteBatch允许的屏幕。

我能这样做吗?或者,我可以通过某种FBO功能实现类似的功能吗?

1 个答案:

答案 0 :(得分:7)

您可以使用RenderTarget2D类。 http://msdn.microsoft.com/en-us/library/bb198676.aspx 像这样:

RenderTarget2D target = new RenderTarget2D(GraphicsDevice, width,height);
GraphicsDevice.SetRenderTarget(target);// Now the spriteBatch will render to the RenderTarget2D

spriteBatch.Begin();

spriteBatch.DrawString();//Do your stuff here

spriteBatch.End();

GraphicsDevice.SetRenderTarget(null);//This will set the spriteBatch to render to the screen again.

//If you are going to create the render target inside the Draw method, do this:
target.Dispose();