BMFont制作美术字体

 

 

 

 

 

 

 

 

生成 Number.fnt、Number_0.png 两个文件,将其拖入Unity 相应位置,继续下一步

 

 

 

 

 

箭头所指就是我们要得到的最终目标,在文本处字体使用它就可以了。

 

在使用 Tools -> BMFont Maker 之前得先完成以下步骤:

 

  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 中的以下类:

posted @ 2018-07-07 23:13  oayx  阅读(1592)  评论(0编辑  收藏  举报