MySpritesPacker.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using System;
  4. using System.IO;
  5. using UnityEditor;
  6. using System.Collections.Generic;
  7. using System.Xml;
  8. public class MySpritesPacker : Editor
  9. {
  10. [MenuItem("Tools/SpritesPacker/TexturePacker")]
  11. public static void BuildTexturePacker()
  12. {
  13. string inputPath = string.Format("{0}/TP/SheetsByTP/", Application.dataPath);
  14. string[] imagePath = Directory.GetFiles(inputPath);
  15. foreach (string path in imagePath)
  16. {
  17. if (Path.GetExtension(path) == ".png" || Path.GetExtension(path) == ".PNG")
  18. {
  19. string sheetPath = GetAssetPath(path);
  20. Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(sheetPath);
  21. Debug.Log(texture.name);
  22. string rootPath = CommandBuild.outputPath;
  23. string pngPath = rootPath + "/" + texture.name + ".png";
  24. TextureImporter asetImp = null;
  25. Dictionary<string, Vector4> tIpterMap = new Dictionary<string,Vector4>();
  26. if (Directory.Exists(rootPath))
  27. {
  28. if(File.Exists(pngPath))
  29. {
  30. Debug.Log("exite: " + pngPath);
  31. asetImp = GetTextureIpter(pngPath);
  32. SaveBoreder(tIpterMap, asetImp);
  33. File.Delete(pngPath);
  34. }
  35. File.Copy(inputPath + texture.name + ".png", pngPath);
  36. }
  37. else
  38. {
  39. Directory.CreateDirectory(rootPath);
  40. File.Copy(inputPath + texture.name + ".png", pngPath);
  41. }
  42. AssetDatabase.Refresh();
  43. FileStream fs = new FileStream(inputPath + texture.name + ".xml", FileMode.Open);
  44. StreamReader sr = new StreamReader(fs);
  45. string jText = sr.ReadToEnd();
  46. fs.Close();
  47. sr.Close();
  48. XmlDocument xml = new XmlDocument();
  49. xml.LoadXml(jText);
  50. XmlNodeList elemList = xml.GetElementsByTagName("SubTexture");
  51. WriteMeta(elemList, texture.name, tIpterMap);
  52. }
  53. }
  54. AssetDatabase.Refresh();
  55. }
  56. //如果这张图集已经拉好了9宫格,需要先保存起来
  57. static void SaveBoreder(Dictionary<string,Vector4> tIpterMap,TextureImporter tIpter)
  58. {
  59. for(int i = 0,size = tIpter.spritesheet.Length; i < size; ++i)
  60. {
  61. tIpterMap.Add(tIpter.spritesheet[i].name, tIpter.spritesheet[i].border);
  62. }
  63. }
  64. static TextureImporter GetTextureIpter(Texture2D texture)
  65. {
  66. TextureImporter textureIpter = null;
  67. string impPath = AssetDatabase.GetAssetPath(texture);
  68. textureIpter = TextureImporter.GetAtPath(impPath) as TextureImporter;
  69. return textureIpter;
  70. }
  71. static TextureImporter GetTextureIpter(string path)
  72. {
  73. TextureImporter textureIpter = null;
  74. Texture2D textureOrg = AssetDatabase.LoadAssetAtPath<Texture2D>(GetAssetPath(path));
  75. string impPath = AssetDatabase.GetAssetPath(textureOrg);
  76. textureIpter = TextureImporter.GetAtPath(impPath) as TextureImporter;
  77. return textureIpter;
  78. }
  79. //写信息到SpritesSheet里
  80. static void WriteMeta(XmlNodeList elemList, string sheetName,Dictionary<string,Vector4> borders)
  81. {
  82. string path = string.Format("Assets/Content/Altas/{0}.png", sheetName);
  83. Texture texture = AssetDatabase.LoadAssetAtPath<Texture>(path);
  84. string impPath = AssetDatabase.GetAssetPath(texture);
  85. TextureImporter asetImp = TextureImporter.GetAtPath(impPath) as TextureImporter;
  86. SpriteMetaData[] metaData = new SpriteMetaData[elemList.Count];
  87. for (int i = 0, size = elemList.Count; i < size; ++i)
  88. {
  89. XmlElement node = (XmlElement)elemList.Item(i);
  90. Rect rect = new Rect();
  91. rect.x = int.Parse(node.GetAttribute("x"));
  92. rect.y = texture.height - int.Parse(node.GetAttribute("y")) - int.Parse(node.GetAttribute("height"));
  93. rect.width = int.Parse(node.GetAttribute("width"));
  94. rect.height = int.Parse(node.GetAttribute("height"));
  95. metaData[i].rect = rect;
  96. metaData[i].pivot = new Vector2(0.5f, 0.5f);
  97. metaData[i].name = node.GetAttribute("name");
  98. if (borders.ContainsKey(metaData[i].name))
  99. {
  100. metaData[i].border = borders[metaData[i].name];
  101. }
  102. }
  103. asetImp.spritesheet = metaData;
  104. asetImp.textureType = TextureImporterType.Sprite;
  105. asetImp.spriteImportMode = SpriteImportMode.Multiple;
  106. asetImp.mipmapEnabled = false;
  107. asetImp.SaveAndReimport();
  108. }
  109. static string GetAssetPath(string path)
  110. {
  111. string[] seperator = { "Assets" };
  112. string p = "Assets" + path.Split(seperator, StringSplitOptions.RemoveEmptyEntries)[1];
  113. return p;
  114. }
  115. }
  116. internal class TextureIpter
  117. {
  118. public string spriteName = "";
  119. public Vector4 border = new Vector4();
  120. public TextureIpter() { }
  121. public TextureIpter(string spriteName, Vector4 border)
  122. {
  123. this.spriteName = spriteName;
  124. this.border = border;
  125. }
  126. }
  127. #endif