1. 程式人生 > >BMFont制作美術字體

BMFont制作美術字體

是我 guilayout line info 兩個文件 由於 spa ttext efi

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

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

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

箭頭所指就是我們要得到的最終目標,在文本處字體使用它就可以了。

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

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

將這個類放入工程中,這樣在 Tools 中才可以找到 BMFont Maker,它的作用是賦予字體的詳細信息,由於它是借助 NGUI 來實現的工具,所以得加上 NGUI 中的以下類:

技術分享圖片

BMFont制作美術字體