Fonts 在XNA中绘制纹理的字符串?

Fonts 在XNA中绘制纹理的字符串?,fonts,xna,textures,Fonts,Xna,Textures,很简单。我想拿一个像“香蕉”这样的字符串,在上面打一个SpriteFont,然后将它渲染成Texture2D,而不是像SpriteBatch允许的那样直接渲染到屏幕上 我可以这样做吗?或者,我可以通过某种FBO风格的功能实现类似的功能吗?您可以使用RenderTarget2D类。 大概是这样的: RenderTarget2D target = new RenderTarget2D(GraphicsDevice, width,height); GraphicsDevice.SetRenderTar

很简单。我想拿一个像“香蕉”这样的字符串,在上面打一个
SpriteFont
,然后将它渲染成
Texture2D
,而不是像
SpriteBatch
允许的那样直接渲染到屏幕上


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

您可以使用RenderTarget2D类。 大概是这样的:

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();