BMFont製做美術字體

 

 

 

 

 

 

 

 

生成 Number.fnt、Number_0.png 兩個文件,將其拖入Unity 相應位置,繼續下一步工具

 

 

 

 

 

箭頭所指就是咱們要獲得的最終目標,在文本處字體使用它就能夠了。字體

 

在使用 Tools -> BMFont Maker 以前得先完成如下步驟:spa

 

  1.  
    using UnityEngine;
  2.  
    using UnityEditor;
  3.  
     
  4.  
    public class BMFontEditor : EditorWindow
  5.  
    {
  6.  
    [MenuItem("Tools/BMFont Maker")]
  7.  
    static public void OpenBMFontMaker()
  8.  
    {
  9.  
    EditorWindow.GetWindow <BMFontEditor>(false, "BMFont Maker", true).Show();
  10.  
    }
  11.  
     
  12.  
    [SerializeField]
  13.  
    private Font targetFont;
  14.  
     
  15.  
    [SerializeField]
  16.  
    private TextAsset fntData;
  17.  
     
  18.  
    [SerializeField]
  19.  
    private Material fontMaterial;
  20.  
     
  21.  
    [SerializeField]
  22.  
    private Texture2D fontTexture;
  23.  
     
  24.  
    private BMFont bmFont = new BMFont();
  25.  
     
  26.  
    public BMFontEditor()
  27.  
    {
  28.  
    }
  29.  
     
  30.  
    void OnGUI()
  31.  
    {
  32.  
    targetFont = EditorGUILayout.ObjectField("Target Font", targetFont, typeof(Font), false) as Font;
  33.  
    fntData = EditorGUILayout.ObjectField("Fnt Data", fntData, typeof(TextAsset), false) as TextAsset;
  34.  
    fontMaterial = EditorGUILayout.ObjectField("Font Material", fontMaterial, typeof(Material), false) as Material;
  35.  
    fontTexture = EditorGUILayout.ObjectField("Font Texture", fontTexture, typeof(Texture2D), false) as Texture2D;
  36.  
     
  37.  
    if (GUILayout.Button("Create BMFont"))
  38.  
    {
  39.  
    BMFontReader.Load(bmFont, fntData.name, fntData.bytes); //借用NGUI封裝的讀取類
  40.  
    CharacterInfo[] characterInfo = new CharacterInfo[bmFont.glyphs.Count];
  41.  
    for (int i = 0; i < bmFont.glyphs.Count; i++)
  42.  
    {
  43.  
    BMGlyph bmInfo = bmFont.glyphs[i];
  44.  
    CharacterInfo info = new CharacterInfo();
  45.  
    info.index = bmInfo.index;
  46.  
    info.uv.x = (float)bmInfo.x / (float)bmFont.texWidth;
  47.  
    info.uv.y = 1 - (float)bmInfo.y / (float)bmFont.texHeight;
  48.  
    info.uv.width = (float)bmInfo.width / (float)bmFont.texWidth;
  49.  
    info.uv.height = -1f * (float)bmInfo.height / (float)bmFont.texHeight;
  50.  
    info.vert.x = 0;
  51.  
    info.vert.y = -(float)bmInfo.height;
  52.  
    info.vert.width = (float)bmInfo.width;
  53.  
    info.vert.height = (float)bmInfo.height;
  54.  
    info.width = (float)bmInfo.advance;
  55.  
    characterInfo[i] = info;
  56.  
    }
  57.  
    targetFont.characterInfo = characterInfo;
  58.  
    if (fontMaterial)
  59.  
    {
  60.  
    fontMaterial.mainTexture = fontTexture;
  61.  
    }
  62.  
    targetFont.material = fontMaterial;
  63.  
    fontMaterial.shader = Shader.Find("UI/Default");//這一行很關鍵,若是用standard的shader,放到Android手機上,第一次加載會很慢
  64.  
     
  65.  
    Debug.Log("Create Font <" + targetFont.name + "> Success");
  66.  
    Close();
  67.  
    }
  68.  
    }
  69.  
    }

將這個類放入工程中,這樣在 Tools 中才能夠找到 BMFont Maker,它的做用是賦予字體的詳細信息,因爲它是藉助 NGUI 來實現的工具,因此得加上 NGUI 中的如下類:code

相關文章
相關標籤/搜索