2016-02-08 43 views
0

我是新來的monogame,我試圖製作一個.spritefont文件,以便使用我選擇的字體繪製字符串。如何在monogame中使用spritefont繪製每個角色

帶有英文字符的字符串可以在屏幕上顯示,但我希望用多種語言繪製字符串,如日文和中文。

所以,我試圖加載多語言字體「Microsoft JhengHei」中的所有字符。

字體的第一個字符是!(U+0021),最後一個字符是○(U+FFEE)

但是,當我試圖編譯程序,編譯器給了我一個錯誤:

.../Content/MyFont.spritefont : error : Importer 'FontDescriptionImporter' had unexpected failure!

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: CharacterRegion.End must be greater than CharacterRegion.Start

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

當我改變了○忮MSBuild stucks和永遠需要進行的內容。

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

我搜索了幾天的解決方案,但徒勞的,任何幫助表示讚賞。

+0

這是沒有意義的,因爲'CharacterRegion.End'和'.Start'都是'char'類型,'(char)0xFFEE'大於'(char)0x0021'。你確定這個問題並不簡單嗎,比如看一個錯誤的文件什麼的?如果將「End」更改爲「&#x0022;'? – Groo

+0

是的,它編譯。我在想,也許是'&#xFFEE;'太大了,因爲互聯網上的文章說內容管道會將字符區域中的所有字符解析爲圖像文件。 – J3soon

+0

@格羅但錯誤消息困惑我,我不知道我能做什麼。 – J3soon

回答

1

由於處理所有65000個字符需要太多時間。我們只應該處理我們正在使用的角色。

所以最簡單的方法是製作一個MonoGame Custom Content Pipeline並加載我們正在使用的一些.resx文件中的字符。

我花了很多時間尋找這個解決方案。所以我會發布我如何成功,希望它可以幫助未來有同樣問題的人。

步驟一步教程

  1. 創建一個類庫。

  2. 參考MonoGame.Framework.Content.Pipeline.Portable包使用NuGet。 (請確保您選中了Include Prerelease複選框)

  3. 下載LocalizationSamplehere並解壓縮文件。

  4. LocalizationPipeline\複製LocalizedFontDescription.csLocalizedFontProcessor.cs到類庫

  5. 構建類庫,所以它輸出LocalizationPipeline.dll文件。

  6. 打開Myfont.spritefont並改變其資產類型以LocalizationPipeline.LocalizedFontDescription

  7. 然後添加資源<ResourceFiles><Resx>..\strings.resx</Resx></ResourceFiles>(這些文件應包含我們要畫的字符串)LocalizationPipeline.dll

  8. 打開Content.mgcb和參考

  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> 

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. How to: Create a Localized Game

  3. LocalizationSample 1部分(感謝@Groo給我這個鏈接。)