MonoGame,XNA以及FNA在使用中文时比较麻烦,在之前我写过两种方式:

1.资源文件方式使用中文:https://www.xnadevelop.com/ios/use-chinese-in-monogame/

2.预编译xnb方式使用中文:http://www.xnadevelop.com/ios/monogame-character-support-simplification-tool/

下面介绍另一种方式:任意字符生成图片的方式使用中文

注意:本方法使用可在windows下运行

定义Texture2D来存储字符生成的图片

Texture2D textTexture2D;

引用 System.Drawing.dll

在【LoadContent()】方法中将中文字符转换成Texture2D

string str = "使用monogame开发跨平台游戏";
Graphics g = Graphics.FromImage(new Bitmap(1, 1));
Font font = new Font("宋体", 20);
SizeF sizeF = g.MeasureString(str, font); //测量出字体的高度和宽度
Brush brush; //笔刷,颜色
brush = Brushes.Lime;
PointF pf = new PointF(0, 0);
Bitmap img = new Bitmap(Convert.ToInt32(sizeF.Width), Convert.ToInt32(sizeF.Height));
g = Graphics.FromImage(img);
g.DrawString(str, font, brush, pf);

//将字符转换成Texture2D
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

textTexture2D=Texture2D.FromStream(graphics.GraphicsDevice, ms);

在【 Draw(GameTime gameTime)】方法中绘制

spriteBatch.Begin();

spriteBatch.Draw(textTexture2D, Vector2.Zero, Microsoft.Xna.Framework.Color.White);
 
spriteBatch.End();

完整代码如下:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Drawing;
using System.IO;

namespace Font2ImageGame
{
    /// <summary>
    /// This is the main type for your game.
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D textTexture2D;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            string str = "使用monogame开发跨平台游戏";
            Graphics g = Graphics.FromImage(new Bitmap(1, 1));
            Font font = new Font("宋体", 20);
            SizeF sizeF = g.MeasureString(str, font); //测量出字体的高度和宽度
            Brush brush; //笔刷,颜色
            brush = Brushes.Lime;
            PointF pf = new PointF(0, 0);
            Bitmap img = new Bitmap(Convert.ToInt32(sizeF.Width), Convert.ToInt32(sizeF.Height));
            g = Graphics.FromImage(img);
            g.DrawString(str, font, brush, pf);

            //将字符转换成Texture2D
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

            textTexture2D=Texture2D.FromStream(graphics.GraphicsDevice, ms);
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// game-specific content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();

            spriteBatch.Draw(textTexture2D, Vector2.Zero, Microsoft.Xna.Framework.Color.White);
             
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

运行结果:

image



如果您对C#游戏开发感兴趣,可以扫下面二维码加入我们的QQ群来一起学习交流

原创文章,转载请注明本文链接地址(违者必究):字符生成图片的方式在MonoGame或XNA中使用中文

打赏作者
很喜欢这篇文章,打赏犒劳下作者,以此激励作者创作更多

您的支持将鼓励我们继续创作!

[微信] 扫描二维码打赏

[支付宝] 扫描二维码打赏

发表评论

邮箱地址不会被公开。 必填项已用*标注