|
| 1 | +#if ((UNITY_EDITOR && UNITY_2021_1_OR_NEWER) || PACKAGE_DOCS_GENERATION) |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using UnityEditor; |
| 5 | + |
| 6 | +namespace UnityEngine.InputSystem.Editor |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// This class controls all required plugins and extension packages are installed for the InputSystem. |
| 10 | + /// </summary> |
| 11 | + /// <remarks> |
| 12 | + /// For some platforms, the InputSystem requires additional plugins to be installed. This class checks if the required plugins are installed and throws a warning if they are not. |
| 13 | + /// </remarks> |
| 14 | + public class InputSystemPluginControl |
| 15 | + { |
| 16 | + // Input system platform specific classes register with the input system via a class using InitializeOnLoad on static constructors. |
| 17 | + // Static constructors in classes that are tagged with the InitializeOnLoad attribute are called before methods using the InitializeOnLoadMethod attribute. |
| 18 | + // So the extra input system packages will be registered before this check which is done in InitializeOnLoadMethod. |
| 19 | + [InitializeOnLoadMethod] |
| 20 | + private static void CheckForExtension() |
| 21 | + { |
| 22 | + ThrowWarningOnMissingPlugin(); |
| 23 | + } |
| 24 | + |
| 25 | + // This static HashSet will be reset OnDomainReload and so it will be reset every Domain Reload (at the time of InitializeOnLoad). |
| 26 | + // This is pre-populated with the list of platforms that don't need a extra platform specific input system package to add platform specific functionality. |
| 27 | + private static HashSet<BuildTarget> s_supportedBuildTargets = new HashSet<BuildTarget>() |
| 28 | + { |
| 29 | + BuildTarget.StandaloneOSX, |
| 30 | + BuildTarget.StandaloneWindows, |
| 31 | + BuildTarget.iOS, |
| 32 | + BuildTarget.Android, |
| 33 | + BuildTarget.StandaloneWindows64, |
| 34 | + BuildTarget.WebGL, |
| 35 | + BuildTarget.WSAPlayer, |
| 36 | + BuildTarget.StandaloneLinux64, |
| 37 | + BuildTarget.tvOS, |
| 38 | + BuildTarget.LinuxHeadlessSimulation, |
| 39 | + BuildTarget.EmbeddedLinux, |
| 40 | + #if UNITY_2022_1_OR_NEWER |
| 41 | + BuildTarget.QNX, |
| 42 | + #endif |
| 43 | + #if UNITY_2023_3_OR_NEWER |
| 44 | + BuildTarget.VisionOS, |
| 45 | + #endif |
| 46 | + #if UNITY_6000_0_OR_NEWER |
| 47 | + BuildTarget.ReservedCFE, |
| 48 | + #endif |
| 49 | + #if UNITY_6000_0_7_OR_NEWER |
| 50 | + BuildTarget.Kepler |
| 51 | + #endif |
| 52 | + BuildTarget.NoTarget |
| 53 | + }; |
| 54 | + |
| 55 | + static bool BuildTargetNeedsPlugin() |
| 56 | + { |
| 57 | + BuildTarget target = EditorUserBuildSettings.activeBuildTarget; |
| 58 | + foreach (var platform in s_supportedBuildTargets) |
| 59 | + { |
| 60 | + if (platform == target) return false; |
| 61 | + } |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + private const string PlugInName = "com.unity.inputsystem."; |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Used to register extensions externally to the InputSystem, this is needed for all Platforms that require a plugin to be installed. |
| 69 | + /// </summary> |
| 70 | + /// <remarks> |
| 71 | + /// This method is internally called by the InputSystem package extensions to register the PlugIn. This can be called for custom extensions on custom platforms. |
| 72 | + /// </remarks> |
| 73 | + public static void RegisterPlatform(BuildTarget target) |
| 74 | + { |
| 75 | + s_supportedBuildTargets.Add(target); |
| 76 | + } |
| 77 | + |
| 78 | + private static bool IsPluginInstalled() |
| 79 | + { |
| 80 | + var registeredPackages = UnityEditor.PackageManager.PackageInfo.GetAllRegisteredPackages(); |
| 81 | + var plugInName = PlugInName + EditorUserBuildSettings.activeBuildTarget.ToString().ToLower(); |
| 82 | + foreach (var package in registeredPackages) |
| 83 | + { |
| 84 | + if (package.name.Equals(plugInName)) |
| 85 | + return true; |
| 86 | + } |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + private static void ThrowWarningOnMissingPlugin() |
| 91 | + { |
| 92 | + if (!BuildTargetNeedsPlugin()) |
| 93 | + return; |
| 94 | + Debug.Assert(IsPluginInstalled(), "Active Input Handling is set to InputSystem, but no Plugin for " + EditorUserBuildSettings.activeBuildTarget + " was found. Please install the missing InputSystem package extensions."); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | +#endif |
0 commit comments