XNA 2.0中处理中文绘制
来源:互联网 发布:java约瑟夫环答案 编辑:程序博客网 时间:2024/05/09 00:07
在XNA 2.0中,如果要绘制中文字符,有两种办法。
第一:添加SpriteFont,修该他的CharacterRegion的Start和End,范围就是中文的Unicode范围
。但是这种方法会极大的增加构建的时间,所以不推荐。
第二种方法是自定义content processor。
右键解决方案,添加——〉新建项目,选择Content Pipeline Extension Library.
在自动生成的文件中添加对System.IO和System.ComponentModel的引用。
using System.IO;
using System.ComponentModel;
删除:
using TInput=System.String;
using Toutput=System.String;
在类的声明中添加以下代码:
[DefaultValue("Message.txt")]
[DisplayName("Message File")]
[Description("The characters in this file will be automatically added to the
font.")]
public string MessageFile
{
get { return messageFile; }
set { messageFile = value; }
}
注意:Message.txt当中应当是你需要添加输出的字符。并且Message.txt应当保存为UTF8的编码
格式(这点很重要,否则将无法读出中文字符,在记事本中写好了要添加的字符后,点击另存为,可
以选择编码方式)。
修改Process方法,如下:
public override SpriteFontContent Process(FontDescription input,
ContentProcessorContext context)
{
// TODO: process the input object, and return the modified data.
string fullPath = Path.GetFullPath(MessageFile);
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);
}
以下是全部代码:
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using System.IO;
using System.ComponentModel;
// TODO: replace these with the processor input and output types.
//using TInput = System.String;
//using TOutput = System.String;
namespace FontProcess
{
/// <summary>
/// This class will be instantiated by the XNA Framework Content Pipeline
/// to apply custom processing to content data, converting an object of
/// type TInput to TOutput. The input and output types may be the same if
/// the processor wishes to alter data without changing its type.
///
/// This should be part of a Content Pipeline Extension Library project.
///
/// TODO: change the ContentProcessor attribute to specify the correct
/// display name for this processor.
/// </summary>
[ContentProcessor(DisplayName = "FontProcessor")]
public class FontProcessor : FontDescriptionProcessor
{
[DefaultValue("Message.txt")]
[DisplayName("Message File")]
[Description("The characters in this file will be automatically added to
the font.")]
public string MessageFile
{
get { return messageFile; }
set { messageFile = value; }
}
private string messageFile = "../Message.txt";
public override SpriteFontContent Process(FontDescription input,
ContentProcessorContext context)
{
// TODO: process the input object, and return the modified data.
string fullPath = Path.GetFullPath(MessageFile);
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);
}
}
}
写好以后,编译一次。
然后点击解决方案中你自己的项目,找到Content接点,右键添加引用-〉项目找到你刚刚写的processor,比如这里用的名字是FontProcess。
之后修改SpriteFont文件的Content Processor:
如上图所示:在下拉菜单中选择你写的FontProcessor。
最后在解决方案中选择你的项目,右键,项目依赖项,把FontProcessor钩上。
OK,试试能不能绘制中文字符了吧。
- XNA 2.0中处理中文绘制
- 在XNA中显示中文字符
- xna教程(四)——在xna中绘制字体
- xna中使用自定义的shader来绘制model
- XNA的中文支持
- XNA中文显示学习
- XNA中文显示学习
- 让XNA显示中文
- XNA-3D-绘制立方体
- XNA中实现点阵化(马赛克)后处理效果
- XNA学习笔记——2D屏幕中绘制线条
- XNA下的中文显示
- jsp中中文处理
- libxml2中处理中文
- Latex中处理中文
- jsp中 中文处理
- MDX中mesh绘制时坐标轴处理
- 关于XNA 2.0 beta
- 名片设计攻略(5)
- DHCP 服务器的安装和配置
- Hash 函数大全
- 名片设计攻略(6)
- Javascript
- XNA 2.0中处理中文绘制
- 安装 sql2005----花了一个上午搞定
- 名片设计攻略(7)
- 名片设计攻略(8)
- WSLICMGR破解
- JDBC 批量插入记录
- 还有一个星期就考试了
- 名片上最常用的中英文称呼
- Windows用户态程序高效排错 -- 排错的工具:调试器Windbg