CustomFontImportor.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Xml;
  5. using System;
  6. public class CustomFontImportor : MonoBehaviour
  7. {
  8. public Font font;
  9. public TextAsset textAsset;
  10. //解析xml格式的fnt
  11. // void Awake()
  12. // {
  13. // if (font == null || textAsset == null)
  14. // {
  15. // //Debug.LogError("请设置font和textAsset.");
  16. // return;
  17. // }
  18. // XmlDocument xmlDocument = new XmlDocument();
  19. // xmlDocument.LoadXml(textAsset.text);
  20. // int totalWidth = Convert.ToInt32(xmlDocument["font"]["common"].Attributes["scaleW"].InnerText);
  21. // int totalHeight = Convert.ToInt32(xmlDocument["font"]["common"].Attributes["scaleH"].InnerText);
  22. // XmlElement xml = xmlDocument["font"]["chars"];
  23. // ArrayList characterInfoList = new ArrayList();
  24. // for (int i = 0; i < xml.ChildNodes.Count; ++i)
  25. // {
  26. // XmlNode node = xml.ChildNodes[i];
  27. // if (node.Attributes == null)
  28. // {
  29. // continue;
  30. // }
  31. // int index = Convert.ToInt32(node.Attributes["id"].InnerText);
  32. // int x = Convert.ToInt32(node.Attributes["x"].InnerText);
  33. // int y = Convert.ToInt32(node.Attributes["y"].InnerText);
  34. // int width = Convert.ToInt32(node.Attributes["width"].InnerText);
  35. // int height = Convert.ToInt32(node.Attributes["height"].InnerText);
  36. // int xOffset = Convert.ToInt32(node.Attributes["xoffset"].InnerText);
  37. // int yOffset = Convert.ToInt32(node.Attributes["yoffset"].InnerText);
  38. // int xAdvance = Convert.ToInt32(node.Attributes["xadvance"].InnerText);
  39. // CharacterInfo info = new CharacterInfo();
  40. // Rect uv = new Rect();
  41. // uv.x = (float)x / totalWidth;
  42. // uv.y = (float)(totalHeight - y - height) / totalHeight;
  43. // uv.width = (float)width / totalWidth;
  44. // uv.height = (float)height / totalHeight;
  45. // info.index = index;
  46. // info.uvBottomLeft = new Vector2(uv.xMin, uv.yMin);
  47. // info.uvBottomRight = new Vector2(uv.xMax, uv.yMin);
  48. // info.uvTopLeft = new Vector2(uv.xMin, uv.yMax);
  49. // info.uvTopRight = new Vector2(uv.xMax, uv.yMax);
  50. // info.minX = xOffset;
  51. // info.maxX = xOffset + width;
  52. // info.minY = -yOffset - height;
  53. // info.maxY = -yOffset;
  54. // info.advance = xAdvance;
  55. // info.glyphWidth = width;
  56. // info.glyphHeight = height;
  57. // characterInfoList.Add(info);
  58. // }
  59. // font.characterInfo = characterInfoList.ToArray(typeof(CharacterInfo)) as CharacterInfo[];
  60. // Debug.Log("生成成功.");
  61. // }
  62. //解析txt格式的fnt
  63. void Start()
  64. {
  65. if (font == null || textAsset == null)
  66. {
  67. //Debug.LogError("请设置font和textAsset.");
  68. return;
  69. }
  70. Hashtable kvs = new Hashtable();
  71. List<Hashtable> chars = new List<Hashtable>();
  72. string[] kvStrs = textAsset.text.Split(new char[]{' ', '\n'});
  73. Hashtable charInfo = null;
  74. int charListIndex = 0;
  75. foreach (var kvStr in kvStrs)
  76. {
  77. if (kvStr.Trim().Length == 0) continue;
  78. if (kvStr == "char") {
  79. if (charListIndex != chars.Count) chars.Add(charInfo);
  80. charInfo = new Hashtable();
  81. charListIndex++;
  82. continue;
  83. }
  84. if (!kvStr.Contains("=")) continue;
  85. string k = kvStr.Split(new char[]{'='})[0];
  86. string v = kvStr.Split(new char[]{'='})[1];
  87. if (charInfo != null) {
  88. charInfo.Add(k, v);
  89. continue;
  90. }
  91. kvs.Add(k, v);
  92. }
  93. if (charListIndex != chars.Count) chars.Add(charInfo);
  94. int totalWidth = Convert.ToInt32(kvs["scaleW"]);
  95. int totalHeight = Convert.ToInt32(kvs["scaleH"]);
  96. ArrayList characterInfoList = new ArrayList();
  97. for (int i = 0; i < chars.Count; ++i)
  98. {
  99. Hashtable charRecord = chars[i];
  100. int index = Convert.ToInt32(charRecord["id"]);
  101. int x = Convert.ToInt32(charRecord["x"]);
  102. int y = Convert.ToInt32(charRecord["y"]);
  103. int width = Convert.ToInt32(charRecord["width"]);
  104. int height = Convert.ToInt32(charRecord["height"]);
  105. int xOffset = Convert.ToInt32(charRecord["xoffset"]);
  106. int yOffset = Convert.ToInt32(charRecord["yoffset"]);
  107. int xAdvance = Convert.ToInt32(charRecord["xadvance"]);
  108. CharacterInfo info = new CharacterInfo();
  109. Rect uv = new Rect();
  110. uv.x = (float)x / totalWidth;
  111. uv.y = (float)(totalHeight - y - height) / totalHeight;
  112. uv.width = (float)width / totalWidth;
  113. uv.height = (float)height / totalHeight;
  114. info.index = index;
  115. info.uvBottomLeft = new Vector2(uv.xMin, uv.yMin);
  116. info.uvBottomRight = new Vector2(uv.xMax, uv.yMin);
  117. info.uvTopLeft = new Vector2(uv.xMin, uv.yMax);
  118. info.uvTopRight = new Vector2(uv.xMax, uv.yMax);
  119. info.minX = xOffset;
  120. info.maxX = xOffset + width;
  121. info.minY = -yOffset - height;
  122. info.maxY = -yOffset;
  123. info.advance = xAdvance;
  124. info.glyphWidth = width;
  125. info.glyphHeight = height;
  126. characterInfoList.Add(info);
  127. }
  128. font.characterInfo = characterInfoList.ToArray(typeof(CharacterInfo)) as CharacterInfo[];
  129. Debug.Log("生成成功.");
  130. }
  131. }