BitmapImporter.cs


SUBMITTED BY: Taluigi

DATE: Feb. 17, 2020, 1:27 p.m.

FORMAT: C

SIZE: 4.3 kB

HITS: 552

  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using System.Collections;
  4. using System;
  5. using UnityEditor;
  6. using System.IO;
  7. using System.Xml;
  8. public static class BitmapFontImporter
  9. {
  10. [MenuItem("Assets/Generate Bitmap Font")]
  11. public static void GenerateFont ()
  12. {
  13. TextAsset selected = (TextAsset)Selection.activeObject;
  14. string rootPath = Path.GetDirectoryName (AssetDatabase.GetAssetPath (selected));
  15. Texture2D texture = AssetDatabase.LoadAssetAtPath (rootPath + "/" + selected.name + ".png", typeof(Texture2D)) as Texture2D;
  16. if (!texture)
  17. throw new UnityException ("Texture2d asset doesn't exist for " + selected.name);
  18. string exportPath = rootPath + "/" + Path.GetFileNameWithoutExtension (selected.name);
  19. Work (selected, exportPath, texture);
  20. }
  21. private static void Work (TextAsset import, string exportPath, Texture2D texture)
  22. {
  23. if (!import)
  24. throw new UnityException (import.name + "is not a valid font-xml file");
  25. XmlDocument xml = new XmlDocument ();
  26. xml.LoadXml (import.text);
  27. XmlNode info = xml.GetElementsByTagName ("info") [0];
  28. XmlNode common = xml.GetElementsByTagName("common")[0];
  29. XmlNodeList chars = xml.GetElementsByTagName ("chars") [0].ChildNodes;
  30. float texW = texture.width;
  31. float texH = texture.height;
  32. CharacterInfo[] charInfos = new CharacterInfo[chars.Count];
  33. Rect r;
  34. for (int i=0; i<chars.Count; i++) {
  35. XmlNode charNode = chars [i];
  36. if (charNode.Attributes != null) {
  37. CharacterInfo charInfo = new CharacterInfo ();
  38. charInfo.index = (int)ToFloat (charNode, "id");
  39. charInfo.advance = (int)ToFloat (charNode, "xadvance");
  40. r = new Rect ();
  41. r.x = ((float)ToFloat (charNode, "x")) / texW;
  42. r.y = ((float)ToFloat (charNode, "y")) / texH;
  43. r.width = ((float)ToFloat (charNode, "width")) / texW;
  44. r.height = ((float)ToFloat (charNode, "height")) / texH;
  45. r.y = 1f - r.y - r.height;
  46. charInfo.uvBottomLeft = new Vector2(r.xMin, r.yMin);
  47. charInfo.uvBottomRight = new Vector2(r.xMax, r.yMin);
  48. charInfo.uvTopLeft = new Vector2(r.xMin, r.yMax);
  49. charInfo.uvTopRight = new Vector2(r.xMax, r.yMax);
  50. r = new Rect ();
  51. r.x = (float)ToFloat (charNode, "xoffset");
  52. r.y = (float)ToFloat (charNode, "yoffset");
  53. r.width = (float)ToFloat (charNode, "width");
  54. r.height = (float)ToFloat (charNode, "height");
  55. r.y = -r.y;
  56. r.height = -r.height;
  57. charInfo.minX = (int)r.xMin;
  58. charInfo.maxX = (int)r.xMax;
  59. charInfo.minY = (int)r.yMax;
  60. charInfo.maxY = (int)r.yMin;
  61. charInfos [i] = charInfo;
  62. }
  63. }
  64. // Create material
  65. Shader shader = Shader.Find ("UI/Default");
  66. Material material = new Material (shader);
  67. material.mainTexture = texture;
  68. AssetDatabase.CreateAsset (material, exportPath + ".mat");
  69. // Create font
  70. Font font = new Font ();
  71. font.material = material;
  72. font.name = info.Attributes.GetNamedItem ("face").InnerText;
  73. font.characterInfo = charInfos;
  74. SerializedObject mFont = new SerializedObject(font);
  75. mFont.FindProperty("m_FontSize").floatValue = float.Parse(common.Attributes.GetNamedItem("base").InnerText);
  76. mFont.FindProperty("m_LineSpacing").floatValue = float.Parse(common.Attributes.GetNamedItem("lineHeight").InnerText);
  77. /* Don't work yet
  78. int kerningsCount = int.Parse(kernings.Attributes.GetNamedItem("count").InnerText);
  79. if (kerningsCount > 0)
  80. {
  81. SerializedProperty kerningsProp = mFont.FindProperty("m_KerningValues");
  82. for (int i = 0; i < kerningsCount; i++)
  83. {
  84. kerningsProp.InsertArrayElementAtIndex(i);
  85. XmlNode kerning = kernings.ChildNodes[i];
  86. SerializedProperty kern = kerningsProp.GetArrayElementAtIndex(i);
  87. kern.FindPropertyRelative("second").floatValue = float.Parse(kerning.Attributes.GetNamedItem("amount").InnerText); ;
  88. }
  89. }*/
  90. mFont.ApplyModifiedProperties();
  91. AssetDatabase.CreateAsset (font, exportPath + ".fontsettings");
  92. }
  93. private static float ToFloat (XmlNode node, string name)
  94. {
  95. return float.Parse (node.Attributes.GetNamedItem (name).InnerText);
  96. }
  97. }
  98. #endif

comments powered by Disqus