BMFontEditor.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. //EditorApplication.SaveAssets();
  57. Debug.LogError("升级5.5 删除 EditorApplication.SaveAssets() 函数,如未成功,请检查代码");
  58. AssetDatabase.SaveAssets();
  59. EditorUtility.SetDirty(targetFont);
  60. Close();
  61. }
  62. }
  63. }