C#开发跨平台游戏——在游戏中使用中文
上一篇,我们编写了第一个C#游戏,这一篇我们来学习以下如何在游戏中使用中文!
下载安装MonoGame字符支持简化工具(本人开发的工具)
ms-windows-store://pdp/?productid=9nvl2gr44t01
在输入框中输入【这是我的第一个游戏】,点击【转换】
点击【复制到剪贴板】
双击打开【DefaultFont.spritefont】文件
找到<CharacterRegions>节点,如图红线位置,右键粘贴
完整代码如下:
<?xml version="1.0" encoding="utf-8"?> <!-- This file contains an xml description of a font, and will be read by the XNA Framework Content Pipeline. Follow the comments to customize the appearance of the font in your game, and to change the characters which are available to draw with. --> <XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics"> <Asset Type="Graphics:FontDescription"> <!-- Modify this string to change the font that will be imported. --> <FontName>Segoe UI Mono</FontName> <!-- Size is a float value, measured in points. Modify this value to change the size of the font. --> <Size>14</Size> <!-- Spacing is a float value, measured in pixels. Modify this value to change the amount of spacing in between characters. --> <Spacing>0</Spacing> <!-- UseKerning controls the layout of the font. If this value is true, kerning information will be used when placing characters. --> <UseKerning>true</UseKerning> <!-- Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic", and "Bold, Italic", and are case sensitive. --> <Style>Regular</Style> <!-- If you uncomment this line, the default character will be substituted if you draw or measure text that contains characters which were not included in the font. --> <!-- <DefaultCharacter>*</DefaultCharacter> --> <!-- CharacterRegions control what letters are available in the font. Every character from Start to End will be built and made available for drawing. The default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin character set. The characters are ordered according to the Unicode standard. See the documentation for more information. --> <CharacterRegions> <CharacterRegion> <Start> </Start> <End>~</End> </CharacterRegion> <CharacterRegion> <Start>这</Start> <End>这</End> </CharacterRegion> <CharacterRegion> <Start>是</Start> <End>是</End> </CharacterRegion> <CharacterRegion> <Start>我</Start> <End>我</End> </CharacterRegion> <CharacterRegion> <Start>的</Start> <End>的</End> </CharacterRegion> <CharacterRegion> <Start>第</Start> <End>第</End> </CharacterRegion> <CharacterRegion> <Start>一</Start> <End>一</End> </CharacterRegion> <CharacterRegion> <Start>个</Start> <End>个</End> </CharacterRegion> <CharacterRegion> <Start>游</Start> <End>游</End> </CharacterRegion> <CharacterRegion> <Start>戏</Start> <End>戏</End> </CharacterRegion> </CharacterRegions> </Asset> </XnaContent>
找到spriteBatch.DrawString(defaultFont, “Hello World”, Vector2.Zero, Color.White);将【Hello World】修改为【这是我的第一个游戏】
完整代码如下:
using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; namespace HelloWorld.XNA { /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; SpriteFont defaultFont; 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); // TODO: use this.Content to load your game content here defaultFont = Content.Load<SpriteFont>("DefaultFont"); } /// <summary> /// UnloadContent will be called once per game and is the place to unload /// all 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) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.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(Color.CornflowerBlue); // TODO: Add your drawing code here spriteBatch.Begin(); spriteBatch.DrawString(defaultFont, "这是我的第一个游戏", Vector2.Zero, Color.White); spriteBatch.End(); base.Draw(gameTime); } } }
点击【启动】
这时候游戏就可以正常显示中文了!
使用MonoGame字符支持简化工具的目的,是将所用到的中文字符转换成单个spritefont的字符范围,存放到spritefont文件中进行字符编译。