|
| 1 | +#if UNITY_EDITOR && UNITY_2020_2_OR_NEWER |
| 2 | +using System; |
| 3 | +using System.Collections.ObjectModel; |
| 4 | +using UnityEditor; |
| 5 | +using UnityEditor.PackageManager; |
| 6 | + |
| 7 | +namespace UnityEngine.InputSystem.Editor |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Forces the Editor to restart if the InputSystem package is removed to activate and initialize it on the managed side. |
| 11 | + /// Automatically sets "Project Settings > Player > Active Input Handling" to "Input Manager" once the package is removed. |
| 12 | + /// </summary> |
| 13 | + internal class InputSystemPackageControl |
| 14 | + { |
| 15 | + const string packageName = "com.unity.inputsystem"; |
| 16 | + |
| 17 | + [InitializeOnLoadMethod] |
| 18 | + static void SubscribePackageManagerEvent() |
| 19 | + { |
| 20 | + //There's a number of cases where it might not be called, for instance if the user changed the project manifest and deleted the Library folder before opening the project |
| 21 | + UnityEditor.PackageManager.Events.registeringPackages += CheckForInputSystemPackageRemoved; |
| 22 | + } |
| 23 | + |
| 24 | + private static void CheckForInputSystemPackageRemoved(PackageRegistrationEventArgs packageArgs) |
| 25 | + { |
| 26 | + if (IsInputSystemRemoved(packageArgs.removed)) |
| 27 | + HandleInputSystemRemoved(); |
| 28 | + } |
| 29 | + |
| 30 | + private static bool IsInputSystemRemoved(ReadOnlyCollection<UnityEditor.PackageManager.PackageInfo> packages) |
| 31 | + { |
| 32 | + foreach (var package in packages) |
| 33 | + { |
| 34 | + if (package.name == packageName) |
| 35 | + return true; |
| 36 | + } |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + private static void HandleInputSystemRemoved() |
| 41 | + { |
| 42 | + //Set input handling to InputManager |
| 43 | + EditorPlayerSettingHelpers.newSystemBackendsEnabled = false; |
| 44 | + if (EditorUtility.DisplayDialog("The Unity Editor needs to be restarted", "You've removed the Input System package. This requires a restart of the Unity Editor.", "Restart the Editor", "Ignore (Not recommended)")) |
| 45 | + EditorApplication.OpenProject(Environment.CurrentDirectory); |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | +#endif |
0 commit comments