如何在单机游戏中用spriteefont画出每一个人物?

问题描述 投票:0回答:1

我是新来的单机游戏,我想做一个。.spritefont 文件,以便用我选择的字体来绘制字符串。

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

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

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

但当我试图编译程序时,编译器给我一个错误。

...ContentMyFont.spriteefont : error : Importer 'FontDescriptionImporter' had unexpected failure!

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

在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>

我找了好几天都没找到解决方法,但没有结果,希望能得到帮助。

c# fonts xna monogame spritefont
1个回答
2
投票

我无法在Monogame 3.7.1中重现J3soon所提供的答案的步骤。

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

我的步骤是:

  1. 在管道工具中设置.splitefont处理器为LocalizedFontProcessor。
  2. 在.spriteefont中,包含resx文件的路径。
  3. 在.spritefont中,将 资产类型="图形:字体描述"资产类型="Graphics:LocalizedFontDescription"
  4. 重建内容

我本以为第1步可以在幕后完成第3步,但对我来说,在管道工具和.spriteefont文件中都需要这样做。

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
投票

由于处理全部6.5万个字符需要太多时间。我们应该只处理我们正在使用的字符。

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

我花了这么多时间寻找这个解决办法。所以我把我是怎么成功的发上来,希望能帮到以后有同样问题的人。

分步教程

  1. 创建一个类库。

  2. 参考 MonoGame.Framework.Content.Pipeline.Portable 包,使用 NuGet. (请确保你检查了 Include Prerelease 复选框 )

  3. 下载 LocalizationSample 此处 并解压文件。

  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. 重新建立项目。

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>

内容.mgcb

...
#-------------------------------- References --------------------------------#

/reference:..\LocalizationPipeline.dll

#---------------------------------- Content ---------------------------------#
...
#begin MyFont.spritefont
/importer:FontDescriptionImporter
/processor:LocalizedFontProcessor
/build:MyFont.spritefont
...

来源

  1. 第一部分 为MonoGame管道创建自定义内容导入器。

  2. 如何。创建一个本地化的游戏

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

© www.soinside.com 2019 - 2024. All rights reserved.