ScriptExporter.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using AssetBank.Settings;
  7. using System.Collections.Generic;
  8. namespace AssetBank.Editor.Tools
  9. {
  10. public static class ScriptExporter
  11. {
  12. [MenuItem("Tools/Exporter/Export Scripts")]
  13. public static void ExportScripts()
  14. {
  15. ProcessAndCopyScripts();
  16. }
  17. public static void ProcessAndCopyScripts(string destinationPath = null)
  18. {
  19. var settings = ProjectExporterSettings.GetOrCreateSettings();
  20. var foldersToIgnore = settings.FoldersToIgnore;
  21. var scriptGuids = AssetDatabase.FindAssets("t:Script", new[] { "Assets" });
  22. var filteredScriptPaths = new List<string>();
  23. foreach (var guid in scriptGuids)
  24. {
  25. var path = AssetDatabase.GUIDToAssetPath(guid);
  26. var directoryName = Path.GetDirectoryName(path);
  27. if (foldersToIgnore.Any(folder => directoryName != null && directoryName.StartsWith(folder, StringComparison.OrdinalIgnoreCase))) continue;
  28. filteredScriptPaths.Add(path);
  29. }
  30. destinationPath ??= EditorUtility.SaveFolderPanel("Choose destination to copy scripts into", Application.dataPath, "");
  31. if (string.IsNullOrEmpty(destinationPath)) return;
  32. foreach (var scriptPath in filteredScriptPaths)
  33. {
  34. CopyFile(scriptPath, destinationPath);
  35. }
  36. }
  37. private static void CopyFile(string sourcePath, string targetDir)
  38. {
  39. var relativePath = Path.GetRelativePath(Application.dataPath, sourcePath);
  40. var targetPath = Path.Combine(targetDir, relativePath);
  41. var dirName = Path.GetDirectoryName(targetPath);
  42. if (!Directory.Exists(dirName))
  43. {
  44. if (dirName != null) Directory.CreateDirectory(dirName);
  45. }
  46. File.Copy(sourcePath, targetPath, true);
  47. }
  48. }
  49. }