AssetsImporter.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor;
  5. using UnityEngine;
  6. public partial class AssetsImporter : AssetPostprocessor
  7. {
  8. private static string[] targetModelNames = new string[] { "SpatialComputing", "Tracking2D", "Tracking3D" };
  9. static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) {
  10. foreach (string importedAsset in importedAssets)
  11. {
  12. foreach (string targetName in targetModelNames) {
  13. string keyName = "Demos/" + targetName + "/AlgAssets";
  14. if (importedAsset.EndsWith(keyName))
  15. {
  16. Debug.Log("-20002- OnPostprocessAllAssets importedAsset=" + importedAsset);
  17. if (Directory.Exists(importedAsset))
  18. {
  19. string folderName = Path.GetFileName(importedAsset);
  20. string sourceAssetsPath = Application.dataPath + "/" + importedAsset.Substring("Assets/".Length);
  21. if (!Directory.Exists(sourceAssetsPath)) {
  22. continue;
  23. }
  24. string streamingAssetsPath = Application.dataPath + "/StreamingAssets";
  25. string destinationFolderPath = Path.Combine(streamingAssetsPath, targetName+"/"+folderName);
  26. if (!Directory.Exists(destinationFolderPath))
  27. {
  28. Directory.CreateDirectory(destinationFolderPath);
  29. }
  30. Debug.Log("-20002- OnPostprocessAllAssets copy From=[ " + sourceAssetsPath + "] to [" + destinationFolderPath + "]");
  31. CopyAll(new DirectoryInfo(sourceAssetsPath),new DirectoryInfo(destinationFolderPath));
  32. }
  33. // 刷新 AssetDatabase,使其能够在 Unity 编辑器中立即显示
  34. AssetDatabase.Refresh();
  35. }
  36. }
  37. }
  38. }
  39. private static void CopyAll(DirectoryInfo source, DirectoryInfo target)
  40. {
  41. // 复制文件
  42. foreach (FileInfo file in source.GetFiles())
  43. {
  44. file.CopyTo(Path.Combine(target.FullName, file.Name), true);
  45. }
  46. // 递归复制子文件夹
  47. foreach (DirectoryInfo sourceSubDir in source.GetDirectories())
  48. {
  49. DirectoryInfo targetSubDir = target.CreateSubdirectory(sourceSubDir.Name);
  50. CopyAll(sourceSubDir, targetSubDir);
  51. }
  52. }
  53. }