LaunchTemplateSetup.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.IO;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using PackageInfo = UnityEditor.PackageManager.PackageInfo;
  6. [InitializeOnLoad]
  7. public static class LaunchTemplateSetup
  8. {
  9. static LaunchTemplateSetup() => EditorApplication.update += Initialize;
  10. static void Initialize()
  11. {
  12. if (!CanLoadLaunchTemplates()) return;
  13. EditorApplication.update -= Initialize;
  14. if (!Directory.Exists("Assets/Launch")) LoadLaunchTemplates();
  15. }
  16. [MenuItem("AppSettings/Load Launch Templates", false, 100)]
  17. public static void LoadLaunchTemplates()
  18. {
  19. try { EnsureLaunchTemplate(); }
  20. catch (Exception exception) { Debug.LogError("[AppRuntimes] Could not install the Launch template: " + exception.Message); }
  21. }
  22. [MenuItem("AppSettings/Load Launch Templates", true, 100)]
  23. static bool CanLoadLaunchTemplates() => !EditorApplication.isCompiling && !EditorApplication.isUpdating
  24. && !EditorApplication.isPlayingOrWillChangePlaymode && !BuildPipeline.isBuildingPlayer;
  25. public static void EnsureLaunchTemplate()
  26. {
  27. const string destination = "Assets/Launch";
  28. if (Directory.Exists(destination))
  29. {
  30. Debug.Log("[AppRuntimes] Assets/Launch already exists; its contents were preserved.");
  31. return;
  32. }
  33. var package = PackageInfo.FindForAssembly(typeof(LaunchTemplateSetup).Assembly)
  34. ?? throw new InvalidOperationException("AppRuntimes must be installed through Package Manager.");
  35. if (CopyIfMissing(Path.Combine(package.resolvedPath, "Templates~/Launch"), destination))
  36. {
  37. AssetDatabase.Refresh();
  38. Debug.Log("[AppRuntimes] Copied the Launch template to Assets/Launch.");
  39. }
  40. }
  41. internal static bool CopyIfMissing(string source, string destination)
  42. {
  43. if (Directory.Exists(destination)) return false;
  44. if (!Directory.Exists(source)) throw new DirectoryNotFoundException("Launch template missing: " + source);
  45. // Stage outside Assets so an interrupted copy cannot leave a partially imported Launch.
  46. var staging = Path.Combine("Library", "AppRuntimesLaunch-" + Guid.NewGuid().ToString("N"));
  47. try
  48. {
  49. CopyDirectory(source, staging);
  50. Directory.Move(staging, destination);
  51. if (File.Exists(source + ".meta") && !File.Exists(destination + ".meta"))
  52. File.Copy(source + ".meta", destination + ".meta");
  53. return true;
  54. }
  55. finally { if (Directory.Exists(staging)) Directory.Delete(staging, true); }
  56. }
  57. static void CopyDirectory(string source, string destination)
  58. {
  59. Directory.CreateDirectory(destination);
  60. foreach (var file in Directory.GetFiles(source)) File.Copy(file, Path.Combine(destination, Path.GetFileName(file)));
  61. foreach (var directory in Directory.GetDirectories(source)) CopyDirectory(directory, Path.Combine(destination, Path.GetFileName(directory)));
  62. }
  63. }