在xna中显示中文字符或其它unicode字符(非gdi+)

上传人:第*** 文档编号:38907318 上传时间:2018-05-09 格式:DOC 页数:4 大小:44KB
返回 下载 相关 举报
在xna中显示中文字符或其它unicode字符(非gdi+)_第1页
第1页 / 共4页
在xna中显示中文字符或其它unicode字符(非gdi+)_第2页
第2页 / 共4页
在xna中显示中文字符或其它unicode字符(非gdi+)_第3页
第3页 / 共4页
在xna中显示中文字符或其它unicode字符(非gdi+)_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《在xna中显示中文字符或其它unicode字符(非gdi+)》由会员分享,可在线阅读,更多相关《在xna中显示中文字符或其它unicode字符(非gdi+)(4页珍藏版)》请在金锄头文库上搜索。

1、在 XNA 中显示中文字符或其它 UNICODE 字符(非 GDI+)XNA3.0+VS2008SP1 下调试通过由于 XNA 内置的 DrawString 方法并不能输出全角 UNICODE 字符,只能设定字符 的起始和终止的内码,欧洲语系的字元不多,可以一次导入并生成字体,但像亚洲 语系这样动辄上千的字元又是全角,好像设计者并没有考虑到这些情况。为了实现 字符输出,已经有一些方法,例如利用.net 中的 GDI+。下面的实现方法是通过生成 自定义的文字托管方式来实现的。步骤如下:建立字体文件建立字体文件1.在当前项目中的“Content”中点击右键 2.加入新的文件,类型是“Sprite

2、Font“,起名为“DefaultFont.spritefont“ 3.打开这个 XML 格式的文件,将“FontName“节中的字体改成含有目标语言字体的字 体文件,这里使用“幼圆” 字典文件:字典文件:1.把游戏中需要的文字放到一个文本文件“message.txt”中,方便随时修改,以换行符 结尾,内容如下: message.txt中文输入测试,日本語入力2.将这个文件放置在项目的 Content 目录下。 3.另存为一下这个文件,以“UTF-8”编码格式。 4.在解决方案浏览器中选择这个文件,在属性窗口的高级中的“生成操作”中选择“无”, “复制到输出目录”里选择“始终复制”。 文字处理

3、类:文字处理类: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;us

4、ing Microsoft.Xna.Framework.Content.Pipeline.Graphics;using Microsoft.Xna.Framework.Content.Pipeline.Processors;namespace FontProcessorsContentProcessorpublic class DefaultFontProcessor : FontDescriptionProcessorpublic override SpriteFontContent Process(FontDescription input, ContentProcessorContext

5、 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 下的“引用“上点击右键,添

6、加项目引用,在弹出的对话框中选 择刚才建立的项目:“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;u

7、sing 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.Gam

8、e 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 S

9、priteBatch(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);

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号