xna中制作显示中文字体

1.首先,我们在VS 2010的Solution Explorer中找到FormDesignXNA2010Content项目,右键菜单点击“Add”-“New Folder”,将新文件夹命名为Font,然后在Font上右键点击,选择“Add”-“New Item”,然后在对话框中选择创建“Sprite Font”,将字体文件命名为ChineseFont

2.然后我们在Solution Explorer中找到游戏的Project,在本例中,就是FormDesignXNA2010,右键菜单“Add”-“New Item”,选择“Text File”,命名为messages.txt。双击打开messages.txt,在里边添加游戏中要支持的所有中文字符。因为要使用File.ReadAllText,所以确保文本文件是以’r’或’n’结尾。

3.接下来要创建一个新的Content Processor Project,在Solution Explorer中选择Solution,右键点击”Add”-“New Project”,选择”Content Pipeline Extension Library(4.0)”,命名为FontProcessor

用下面的代码替换里面的内容,修改了ContentProcessor1的基类,将ContentProcessor替换为FontDescriptionProcessor

using System;
using System.Collections.Generic;
using System.Linq;
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;


namespace FontProcessor
{
    /// <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.ContentProcessor1")]
    public class ContentProcessor1 : FontDescriptionProcessor
    {
        //public override TOutput Process(TInput input, ContentProcessorContext context)
        //{
        //    // TODO: process the input object, and return the modified data.
        //    throw new NotImplementedException();
        //}

        public override SpriteFontContent Process(FontDescription input, ContentProcessorContext context)
        {
            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);
        }
        [DefaultValue("messages.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 = @"E:\vs2008projects\FormDesignXNA2010\FormDesignXNA2010\FormDesignXNA2010\messages.txt";

    }
}

注意private string messageFile = @"E:\vs2008projects\FormDesignXNA2010\FormDesignXNA2010\FormDesignXNA2010\messages.txt";为你创建的messages.txt目录

 4.完成这些之后,我们要首先编译一下FontProcessor,然后在Solution Explorer中,右键点击FormDesignXNA2010Content的References目录,选择“Add references”,在Project Tab页中,选择FontProcessor

5.接下来,在Solution Explorer中,右键点击Project Dependencies,将FontProcessor前的CheckBox选中

6.选中创建的ChineseFont,在ChineseFont.spritefont属性页中的Content Processor项中,将“Sprite Font Description - XNA Framework”切换为“FontProcessor.ContentProcessor1”。

7.最后,在游戏中书写中文字体,上面是测试显示效果

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值