资源预览内容
第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
亲,该文档总共4页全部预览完了,如果喜欢就下载吧!
资源描述
在 XNA 中显示中文字符或其它 UNICODE 字符(非 GDI+)XNA3.0+VS2008SP1 下调试通过由于 XNA 内置的 DrawString 方法并不能输出全角 UNICODE 字符,只能设定字符 的起始和终止的内码,欧洲语系的字元不多,可以一次导入并生成字体,但像亚洲 语系这样动辄上千的字元又是全角,好像设计者并没有考虑到这些情况。为了实现 字符输出,已经有一些方法,例如利用.net 中的 GDI+。下面的实现方法是通过生成 自定义的文字托管方式来实现的。步骤如下:建立字体文件建立字体文件1.在当前项目中的“Content”中点击右键 2.加入新的文件,类型是“Sprite Font“,起名为“DefaultFont.spritefont“ 3.打开这个 XML 格式的文件,将“FontName“节中的字体改成含有目标语言字体的字 体文件,这里使用“幼圆” 字典文件:字典文件:1.把游戏中需要的文字放到一个文本文件“message.txt”中,方便随时修改,以换行符 结尾,内容如下: message.txt中文输入测试,日本語入力2.将这个文件放置在项目的 Content 目录下。 3.另存为一下这个文件,以“UTF-8”编码格式。 4.在解决方案浏览器中选择这个文件,在属性窗口的高级中的“生成操作”中选择“无”, “复制到输出目录”里选择“始终复制”。 文字处理类:文字处理类:1.在解决方案浏览器中右键点击解决方案 2.添加新的项目,在项目类型对话框中选择“Windows Game Library(3.0)“,起名 “FontProcess“ 3.在项目中增加引用,选择“Microsoft.Xna.Content.Pipeline” 4.将这个项目中的默认类“Class1.cs“改名为“DefaultFontProcessor.cs“,修改为如下 代码: 5.重新编译这个项目 DefaultFontProcessor.csusing System.IO;using Microsoft.Xna.Framework.Content.Pipeline;using Microsoft.Xna.Framework.Content.Pipeline.Graphics;using Microsoft.Xna.Framework.Content.Pipeline.Processors;namespace FontProcessorsContentProcessorpublic class DefaultFontProcessor : FontDescriptionProcessorpublic override SpriteFontContent Process(FontDescription input, ContentProcessorContext context)/载入文件string fullPath = Path.GetFullPath(“message.txt“);context.AddDependency(fullPath);string letters = File.ReadAllText(fullPath, System.Text.Encoding.UTF8);/导入字符foreach (char c in letters)input.Characters.Add(c);return base.Process(input, context);添加项目引用添加项目引用1.在本项目的 Content 下的“引用“上点击右键,添加项目引用,在弹出的对话框中选 择刚才建立的项目:“FontProcessors” 2.在解决方案管理器选择本项目中的字体文件“DefaultFont.spritefont“ 3.在属性窗口中的 Content Processor 中选择我们建立的处理类: “DefaultFontProssor“ 在项目用使用在项目用使用1.在我们的项目中使用如下代码: Game1.csusing System;using System.Collections.Generic;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.Net;using Microsoft.Xna.Framework.Storage;namespace HelloWorld public class GameMain : Microsoft.Xna.Framework.Game private GraphicsDeviceManager graphics; private SpriteBatch spriteBatch;private SpriteFont font; public GameMain() graphics=new GraphicsDeviceManager(this);Content.RootDirectory=“Content“;protected override void Initialize() base.Initialize();protected override void LoadContent() spriteBatch=new SpriteBatch(GraphicsDevice);font=Content.Load(“DefaultFont“);protected override void UnloadContent() protected override void Update(GameTime gameTime) if (GamePad.GetState(PlayerIndex.One).Buttons.Back=ButtonState.Pressed) Exit();base.Update(gameTime);protected override void Draw(GameTime gameTime) graphics.GraphicsDevice.Clear(Color.CornflowerBlue);spriteBatch.Begin();DrawString(“中文输入测试,日本語入力“,50,50);spriteBatch.End();base.Draw(gameTime);private void DrawString(String str,int x,int y) spriteBatch.DrawString(font,str,new Vector2(x,y),Color.White);
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号