源战役客户端
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

229 linhas
8.1 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using System.IO;
  6. using System.Xml;
  7. using System.Text;
  8. public class FontMakerWizard : ScriptableWizard
  9. {
  10. public TextAsset xmlFile;
  11. public int leftPadding;
  12. public int rightPadding;
  13. public int topPadding;
  14. public int bottomPadding;
  15. public int advanceOffset;
  16. class Glyph
  17. {
  18. public char code;
  19. public Rect bound;
  20. public Rect interiorBound;
  21. public int baseline;
  22. public int spacingA;
  23. public int spacingB;
  24. public int spacingC;
  25. }
  26. private int lineHeight;
  27. private int baseline;
  28. private int textureWidth;
  29. private int textureHeight;
  30. private int glyphWidth;
  31. private int glyphHeight;
  32. private string texturePath;
  33. private List<Glyph> glyphList;
  34. //[MenuItem("Tools/Font/Convert FontMaker")]
  35. static void CreateWindow()
  36. {
  37. ScriptableWizard.DisplayWizard<FontMakerWizard>("Convert FontMaker Config File", "Convert");
  38. }
  39. void OnWizardUpdate()
  40. {
  41. helpString = "Specify font config file";
  42. isValid = (xmlFile != null);
  43. }
  44. void OnWizardCreate()
  45. {
  46. LoadGlyph();
  47. ExportBMFont();
  48. }
  49. void ExportBMFont()
  50. {
  51. StringBuilder sb = new StringBuilder();
  52. // common lineHeight=64 base=51 scaleW=512 scaleH=512 pages=1
  53. sb.AppendFormat("common lineHeight={0} base={1} scaleW={2} scaleH={3} pages=1", lineHeight, baseline, textureWidth, textureHeight);
  54. sb.AppendLine();
  55. // page id=0 file="textureName.png"
  56. string path = Path.GetFileName(texturePath);
  57. sb.AppendFormat("page id=0 file=\"{0}\"", path);
  58. sb.AppendLine();
  59. // char id=13 x=506 y=62 width=3 height=3 xoffset=-1 yoffset=50 xadvance=0 page=0 chnl=15
  60. foreach (var glyph in glyphList)
  61. {
  62. int x = (int)(glyph.bound.x + glyph.interiorBound.x) - leftPadding;
  63. int y = (int)(glyph.bound.y + glyph.interiorBound.y) - topPadding;
  64. int w = (int)glyph.interiorBound.width + (rightPadding + leftPadding);
  65. int h = (int)glyph.interiorBound.height + (topPadding + bottomPadding);
  66. int xOffset = glyph.spacingA;
  67. int yOffset = (int)glyph.interiorBound.y - topPadding;
  68. int xAdvance = glyph.spacingA + glyph.spacingB + glyph.spacingC + advanceOffset;
  69. sb.AppendFormat("char id={0} x={1} y={2} width={3} height={4} xoffset={5} yoffset={6} xadvance={7} page=0 chnl=15",
  70. (int)glyph.code, x, y, w, h, xOffset, yOffset, xAdvance);
  71. sb.AppendLine();
  72. }
  73. string xmlPath = AssetDatabase.GetAssetPath(xmlFile);
  74. string outputDirectory = Path.GetDirectoryName(xmlPath) + "/" + Path.GetDirectoryName(texturePath);
  75. string fntPath = outputDirectory + "/" + Path.GetFileNameWithoutExtension(xmlPath) + ".fnt";
  76. Debug.Log("Write fnt file " + fntPath);
  77. // save fnt file
  78. File.WriteAllText(fntPath, sb.ToString(), Encoding.UTF8);
  79. // refresh database
  80. AssetDatabase.Refresh();
  81. }
  82. void LoadGlyph()
  83. {
  84. glyphList = new List<Glyph>();
  85. // load font config file
  86. using (Stream stream = new MemoryStream(xmlFile.bytes))
  87. {
  88. XmlReaderSettings settings = new XmlReaderSettings();
  89. settings.IgnoreComments = true;
  90. settings.IgnoreWhitespace = true;
  91. using (XmlReader reader = XmlReader.Create(stream, settings))
  92. {
  93. reader.MoveToContent();
  94. reader.ReadStartElement("fontconfig");
  95. {
  96. // typeface
  97. if (reader.Name == "font")
  98. {
  99. lineHeight = int.Parse(reader.GetAttribute("height"));
  100. // move to next
  101. reader.Read();
  102. }
  103. // texture info
  104. if (reader.Name == "texture")
  105. {
  106. textureWidth = int.Parse(reader.GetAttribute("width"));
  107. textureHeight = int.Parse(reader.GetAttribute("height"));
  108. // move to next
  109. reader.Read();
  110. }
  111. // texture info
  112. reader.ReadToFollowing("size");
  113. if (reader.Name == "size")
  114. {
  115. glyphWidth = int.Parse(reader.GetAttribute("width"));
  116. glyphHeight = int.Parse(reader.GetAttribute("height"));
  117. // move to next
  118. reader.Read();
  119. }
  120. // images
  121. reader.ReadToFollowing("images");
  122. if (!reader.IsEmptyElement)
  123. {
  124. List<string> images = new List<string>();
  125. reader.ReadStartElement("images");
  126. if (reader.Name == "image")
  127. {
  128. do
  129. {
  130. string imagePath = reader.GetAttribute("path");
  131. images.Add(imagePath);
  132. }
  133. while (reader.ReadToNextSibling("image"));
  134. }
  135. reader.ReadEndElement();
  136. if (images.Count > 0)
  137. {
  138. // only handle one texture
  139. texturePath = images[0];
  140. }
  141. else
  142. {
  143. Debug.LogError("image path missing");
  144. return;
  145. }
  146. }
  147. else
  148. {
  149. reader.Read();
  150. }
  151. // glyph
  152. if (!reader.IsEmptyElement)
  153. {
  154. reader.ReadStartElement("glyphs");
  155. if (reader.Name == "glyph")
  156. {
  157. do
  158. {
  159. char character = char.Parse(reader.GetAttribute("char"));
  160. int pageIndex = int.Parse(reader.GetAttribute("page"));
  161. int x = int.Parse(reader.GetAttribute("x"));
  162. int y = int.Parse(reader.GetAttribute("y"));
  163. int w = int.Parse(reader.GetAttribute("w"));
  164. int h = int.Parse(reader.GetAttribute("h"));
  165. int ix = int.Parse(reader.GetAttribute("ix"));
  166. int iy = int.Parse(reader.GetAttribute("iy"));
  167. int iw = int.Parse(reader.GetAttribute("iw"));
  168. int ih = int.Parse(reader.GetAttribute("ih"));
  169. Glyph glyph = new Glyph();
  170. glyph.code = character;
  171. glyph.bound = new Rect(x, y, w, h);
  172. glyph.interiorBound = new Rect(ix, iy, iw, ih);
  173. glyph.baseline = int.Parse(reader.GetAttribute("baseline"));
  174. glyph.spacingA = int.Parse(reader.GetAttribute("spacingA"));
  175. glyph.spacingB = int.Parse(reader.GetAttribute("spacingB"));
  176. glyph.spacingC = int.Parse(reader.GetAttribute("spacingC"));
  177. baseline = glyph.baseline;
  178. glyphList.Add(glyph);
  179. }
  180. while (reader.ReadToNextSibling("glyph"));
  181. }
  182. reader.ReadEndElement();
  183. }
  184. else
  185. {
  186. reader.Read();
  187. }
  188. }
  189. reader.ReadEndElement();
  190. }
  191. }
  192. }
  193. }