在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,试试能不能绘制中文字符了吧。