| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using System;
- using System.IO;
- using UnityEditor;
- using UnityEngine;
- using PackageInfo = UnityEditor.PackageManager.PackageInfo;
- [InitializeOnLoad]
- public static class LaunchTemplateSetup
- {
- static LaunchTemplateSetup() => EditorApplication.update += Initialize;
- static void Initialize()
- {
- if (!CanLoadLaunchTemplates()) return;
- EditorApplication.update -= Initialize;
- if (!Directory.Exists("Assets/Launch")) LoadLaunchTemplates();
- }
- [MenuItem("AppSettings/Load Launch Templates", false, 100)]
- public static void LoadLaunchTemplates()
- {
- try { EnsureLaunchTemplate(); }
- catch (Exception exception) { Debug.LogError("[AppRuntimes] Could not install the Launch template: " + exception.Message); }
- }
- [MenuItem("AppSettings/Load Launch Templates", true, 100)]
- static bool CanLoadLaunchTemplates() => !EditorApplication.isCompiling && !EditorApplication.isUpdating
- && !EditorApplication.isPlayingOrWillChangePlaymode && !BuildPipeline.isBuildingPlayer;
- public static void EnsureLaunchTemplate()
- {
- const string destination = "Assets/Launch";
- if (Directory.Exists(destination))
- {
- Debug.Log("[AppRuntimes] Assets/Launch already exists; its contents were preserved.");
- return;
- }
- var package = PackageInfo.FindForAssembly(typeof(LaunchTemplateSetup).Assembly)
- ?? throw new InvalidOperationException("AppRuntimes must be installed through Package Manager.");
- if (CopyIfMissing(Path.Combine(package.resolvedPath, "Templates~/Launch"), destination))
- {
- AssetDatabase.Refresh();
- Debug.Log("[AppRuntimes] Copied the Launch template to Assets/Launch.");
- }
- }
- internal static bool CopyIfMissing(string source, string destination)
- {
- if (Directory.Exists(destination)) return false;
- if (!Directory.Exists(source)) throw new DirectoryNotFoundException("Launch template missing: " + source);
- // Stage outside Assets so an interrupted copy cannot leave a partially imported Launch.
- var staging = Path.Combine("Library", "AppRuntimesLaunch-" + Guid.NewGuid().ToString("N"));
- try
- {
- CopyDirectory(source, staging);
- Directory.Move(staging, destination);
- if (File.Exists(source + ".meta") && !File.Exists(destination + ".meta"))
- File.Copy(source + ".meta", destination + ".meta");
- return true;
- }
- finally { if (Directory.Exists(staging)) Directory.Delete(staging, true); }
- }
- static void CopyDirectory(string source, string destination)
- {
- Directory.CreateDirectory(destination);
- foreach (var file in Directory.GetFiles(source)) File.Copy(file, Path.Combine(destination, Path.GetFileName(file)));
- foreach (var directory in Directory.GetDirectories(source)) CopyDirectory(directory, Path.Combine(destination, Path.GetFileName(directory)));
- }
- }
|