我是新来的单机游戏,我想做一个。.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>!</Start>
<End>○</End>
</CharacterRegion>
</CharacterRegions>
</Asset>
</XnaContent>
我找了好几天都没找到解决方法,但没有结果,希望能得到帮助。
我无法在Monogame 3.7.1中重现J3soon所提供的答案的步骤。
但是在Monogame 3.7.1中,不再需要使用自定义内容管道,因为现在管道工具中包含了一个LocalizedFontProcessor。
我的步骤是:
我本以为第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> </Start>
<End>~</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
由于处理全部6.5万个字符需要太多时间。我们应该只处理我们正在使用的字符。
所以最简单的方法就是做一个 MonoGame Custom Content Pipeline
并通过某些方式加载我们正在使用的字符 .resx
文件。
我花了这么多时间寻找这个解决办法。所以我把我是怎么成功的发上来,希望能帮到以后有同样问题的人。
分步教程
创建一个类库。
参考 MonoGame.Framework.Content.Pipeline.Portable
包,使用 NuGet
. (请确保你检查了 Include Prerelease
复选框 )
下载 LocalizationSample
此处 并解压文件。
在 LocalizationPipeline\
拷贝 LocalizedFontDescription.cs
和 LocalizedFontProcessor.cs
入类库
构建类库,使其输出一个 LocalizationPipeline.dll
文件。
打开 Myfont.spritefont
并将其资产类型改为 LocalizationPipeline.LocalizedFontDescription
然后添加资源 <ResourceFiles><Resx>..\strings.resx</Resx></ResourceFiles>
(这些文件应该包含我们要绘制的字符串)
开放式 Content.mgcb
和参考文献 LocalizationPipeline.dll
设置 MyFont.spritefont
的处理器到 LocalizedFontProcessor
重新建立项目。
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> </Start>
<End>~</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
...
来源
LocalizationSample (感谢@Groo给我这个链接。)