如何用monogame中的spritefont绘制每个字符

时间:2016-02-08 20:14:24

标签: c# fonts xna monogame spritefont

我是monogame的新手,我试图创建一个.spritefont文件,以便用我选择的字体绘制字符串。

带有英文字符的字符串可以在屏幕上很好地显示,但我希望用多种语言绘制字符串,如日语和中文。

所以,我试图加载多语言字体中的所有字符" Microsoft JhengHei"。

字体的第一个字符是!(U+0021),最后一个字符是○(U+FFEE)

但是当我尝试编译程序时,编译器给了我一个错误:

  

... / Content / MyFont.spritefont:错误:导入器' FontDescriptionImporter'意外失败了!

     

System.Reflection.TargetInvocationException:调用目标抛出了异常。 ---> System.ArgumentException:CharacterRegion.End必须大于CharacterRegion.Start

     

at Microsoft.Xna.Framework.Content.Pipeline.Graphics.FontDescription.set_CharacterRegions(CharacterRegion [] value)

当我将○更改为忮时,MSBuild卡住并永远继续进行内容。

以下MyFont.spritefont中的代码:

<?xml version="1.0" encoding="utf-8"?>
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
  <Asset Type="Graphics:FontDescription">
    <FontName>Microsoft JhengHei</FontName>
    <Size>14</Size>
    <Spacing>0</Spacing>
    <UseKerning>true</UseKerning>
    <Style>Regular</Style>
    <CharacterRegions>
      <CharacterRegion>
        <Start>&#x0021;</Start>
        <End>&#xFFEE;</End>
      </CharacterRegion>
    </CharacterRegions>
  </Asset>
</XnaContent>

我搜索了几天的解决方案,但是徒劳,任何帮助都表示赞赏。

2 个答案:

答案 0 :(得分:2)

我无法在Monogame 3.7.1中重现J3soon接受的答案的步骤。

但是在Monogame 3.7.1中,不再需要使用“自定义内容管道”,因为管道工具现在本机包含LocalizedFontProcessor。

我的步骤是:

  1. 在管道工具中将.spritefont Processor设置为LocalizedFontProcessor
  2. 在.spritefont中,包含resx文件的路径。
  3. 在.spritefont中,将 Asset Type =“ Graphics:FontDescription” 替换为 Asset Type =“ Graphics:LocalizedFontDescription”
  4. 重建内容

我本以为第1步将在后台执行第3步,但对我而言,必须在管道工具和.spritefont文件中都执行此操作。

spritefont文件

<?xml version="1.0" encoding="utf-8"?>
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
  <Asset Type="Graphics:LocalizedFontDescription">
    <FontName>Arial</FontName>
    <Size>16</Size>
    <Spacing>2</Spacing>
    <UseKerning>true</UseKerning>
    <Style>Regular</Style>
    <CharacterRegions>
      <CharacterRegion>
        <Start>&#32;</Start>
        <End>&#126;</End>
      </CharacterRegion>
    </CharacterRegions>
    <ResourceFiles>
      <Resx>..\Strings.fr.resx</Resx>
    </ResourceFiles>
  </Asset>
</XnaContent>

内容文件

#begin MyFont.spritefont
/importer:FontDescriptionImporter
/processor:LocalizedFontProcessor
/processorParam:PremultiplyAlpha=True
/processorParam:TextureFormat=Compressed
/build:MyFont.spritefont

答案 1 :(得分:1)

由于处理所有65,000个字符需要花费太多时间。我们应该只处理我们正在使用的字符。

所以最简单的方法是制作一个MonoGame Custom Content Pipeline并加载一些.resx文件我们正在使用的字符。

我花了很多时间寻找这个解决方案。所以我会发布我是如何成功的,希望它可以帮助将来有同样问题的人。

分步教程

  1. 创建一个类库。

  2. 使用MonoGame.Framework.Content.Pipeline.Portable引用NuGet包。 (确保选中了Include Prerelease复选框)

  3. 下载LocalizationSample here并解压缩该文件。

  4. LocalizationPipeline\复制LocalizedFontDescription.csLocalizedFontProcessor.cs下进入类库

  5. 构建类库,以便输出LocalizationPipeline.dll文件。

  6. 打开Myfont.spritefont并将其资产类型更改为LocalizationPipeline.LocalizedFontDescription

  7. 然后添加资源<ResourceFiles><Resx>..\strings.resx</Resx></ResourceFiles>(这些文件应包含我们想要绘制的字符串)

  8. 打开Content.mgcb并引用LocalizationPipeline.dll

  9. MyFont.spritefont的处理器设为LocalizedFontProcessor

  10. 重建项目。

  11. <强> MyFont.spritefont

    <?xml version="1.0" encoding="utf-8"?>
    <XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
      <Asset Type="LocalizationPipeline.LocalizedFontDescription">
        <FontName>Microsoft JhengHei</FontName>
        <Size>14</Size>
        <Spacing>0</Spacing>
        <UseKerning>true</UseKerning>
        <Style>Regular</Style>
        <CharacterRegions>
          <CharacterRegion>
            <Start>&#32;</Start>
            <End>&#126;</End>
          </CharacterRegion>
        </CharacterRegions>
        <ResourceFiles>
          <Resx>..\strings.resx</Resx>
        </ResourceFiles>
      </Asset>
    </XnaContent>
    

    <强> Content.mgcb

    ...
    #-------------------------------- References --------------------------------#
    
    /reference:..\LocalizationPipeline.dll
    
    #---------------------------------- Content ---------------------------------#
    ...
    #begin MyFont.spritefont
    /importer:FontDescriptionImporter
    /processor:LocalizedFontProcessor
    /build:MyFont.spritefont
    ...
    

    <强>来源

    1. Creating custom content importers for the MonoGame Pipeline

    2. 的第1部分
    3. How to: Create a Localized Game

    4. LocalizationSample(感谢@Groo给我这个链接。)

相关问题