BuildTexturePacker.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using System.IO;
  4. using UnityEditor;
  5. using System.Text;
  6. using System.Diagnostics;
  7. public class CommandBuild : Editor
  8. {
  9. public static string inputPath = string.Format("{0}/Content/Raw_Art/UI", Application.dataPath);//小图目录
  10. public static string outputPath = string.Format("{0}/Content/Altas", Application.dataPath);//用TP打包好的图集存放目录
  11. [MenuItem("Tools/SpritesPacker/CommandBuild")]
  12. public static void BuildTexturePacker()
  13. {
  14. //选择并设置TP命令行的参数和参数值
  15. string commandText = " --sheet {0}.png --data {1}.xml --format sparrow --trim-mode None --pack-mode Best --smart-update --algorithm MaxRects --max-size 2048 --size-constraints POT --disable-rotation --scale 1 {2}";
  16. string[] imagePath = Directory.GetDirectories (inputPath);
  17. for (int i = 0; i < imagePath.Length; ++i)
  18. {
  19. UnityEngine.Debug.Log (imagePath [i]);
  20. StringBuilder sb = new StringBuilder("");
  21. string[] fileName = Directory.GetFiles(imagePath[i]);
  22. for (int j = 0; j < fileName.Length; ++j)
  23. {
  24. string extenstion = Path.GetExtension(fileName[j]);
  25. if (extenstion == ".png")
  26. {
  27. sb.Append(fileName[j]);
  28. sb.Append(" ");
  29. UnityEngine.Debug.Log("fileName [j]:" + fileName[j]);
  30. }
  31. }
  32. string name = Path.GetFileName(imagePath [i]);
  33. string outputName = string.Format ("{0}/{1}", outputPath, name);
  34. string sheetName = string.Format("{0}/TP/SheetsByTP/{1}", Application.dataPath, name);
  35. //执行命令行
  36. processCommand("D:/TexturePacker/bin/TexturePacker.exe", string.Format(commandText, sheetName, sheetName, sb.ToString()));
  37. }
  38. AssetDatabase.Refresh();
  39. }
  40. private static void processCommand(string command, string argument)
  41. {
  42. ProcessStartInfo start = new ProcessStartInfo(command);
  43. start.Arguments = argument;
  44. start.CreateNoWindow = false;
  45. start.ErrorDialog = true;
  46. start.UseShellExecute = false;
  47. if(start.UseShellExecute){
  48. start.RedirectStandardOutput = false;
  49. start.RedirectStandardError = false;
  50. start.RedirectStandardInput = false;
  51. } else{
  52. start.RedirectStandardOutput = true;
  53. start.RedirectStandardError = true;
  54. start.RedirectStandardInput = true;
  55. start.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8;
  56. start.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8;
  57. }
  58. Process p = Process.Start(start);
  59. if(!start.UseShellExecute)
  60. {
  61. UnityEngine.Debug.Log(p.StandardOutput.ReadToEnd());
  62. UnityEngine.Debug.Log(p.StandardError.ReadToEnd());
  63. }
  64. p.WaitForExit();
  65. p.Close();
  66. }
  67. }
  68. #endif