ModelImportTask.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace FL.Editor.Imports
  5. {
  6. public class ModelImportTask
  7. {
  8. [MenuItem("Tools/FL/导入模型Spine")]
  9. public static void Test()
  10. {
  11. var task = new ModelImportTask();
  12. task.Start();
  13. }
  14. private const string export_folder = "Export";
  15. private const string spine_atlas_txt = ".atlas.txt";
  16. private const string spine_png = ".png";
  17. private const string spine_skel_bytes = ".skel.bytes";
  18. public void Start()
  19. {
  20. var config = CustomModelConfig.Load();
  21. if (config == null)
  22. {
  23. return;
  24. }
  25. if (!Directory.Exists(config.customModelsPath))
  26. {
  27. Debug.LogError($"外部资源根路径错误:{config.customModelsPath}");
  28. return;
  29. }
  30. foreach (var item in config.paths)
  31. {
  32. var fromPath = Path.Combine(config.customModelsPath, item.from);
  33. var toPath = item.to;
  34. if (!Directory.Exists(fromPath) || !Directory.Exists(toPath))
  35. {
  36. Debug.Log($"无效的路径配置.From:{item.from} To:{item.to}");
  37. continue;
  38. }
  39. var subFolders = Directory.GetDirectories(fromPath);
  40. foreach (var subFolder in subFolders)
  41. {
  42. CopyFiles(subFolder, toPath);
  43. }
  44. }
  45. AssetDatabase.SaveAssets();
  46. AssetDatabase.Refresh();
  47. }
  48. private void CopyFiles(string fromPath, string toPath)
  49. {
  50. var modelName = Path.GetFileName(fromPath);
  51. if (XGame.Editor.Asset.FileUtil.HasChinese(modelName) || modelName.Contains(' '))
  52. {
  53. Debug.LogError($"模型名字包含中文或者空格. Path:{fromPath}");
  54. return;
  55. }
  56. var atlasFile = modelName + spine_atlas_txt;
  57. var atlasPath = Path.Combine(fromPath, export_folder, atlasFile);
  58. if (!File.Exists(atlasPath))
  59. {
  60. Debug.Log($"找不到模型图集文件. Path:{atlasPath}");
  61. return;
  62. }
  63. var pngFile = modelName + spine_png;
  64. var pngPath = Path.Combine(fromPath, export_folder, pngFile);
  65. if (!File.Exists(pngPath))
  66. {
  67. Debug.Log($"找不到模型贴图. Path:{pngPath}");
  68. return;
  69. }
  70. var skelFile = modelName + spine_skel_bytes;
  71. var skelPath = Path.Combine(fromPath, export_folder, skelFile);
  72. if (!File.Exists(skelPath))
  73. {
  74. Debug.Log($"找不到模型动作文件. Path:{skelPath}");
  75. return;
  76. }
  77. toPath = Path.Combine(toPath, modelName);
  78. if (!Directory.Exists(toPath))
  79. {
  80. Directory.CreateDirectory(toPath);
  81. }
  82. CopyFile(atlasPath, Path.Combine(toPath, atlasFile));
  83. CopyFile(pngPath, Path.Combine(toPath, pngFile));
  84. CopyFile(skelPath, Path.Combine(toPath, skelFile));
  85. }
  86. private void CopyFile(string fromPath, string toPath)
  87. {
  88. if (File.Exists(toPath))
  89. {
  90. FileUtil.DeleteFileOrDirectory(toPath);
  91. }
  92. FileUtil.CopyFileOrDirectory(fromPath, toPath);
  93. }
  94. }
  95. }