A game about forced loneliness, made by TACStudios
1#if UNITY_EDITOR
2using System;
3using System.Linq;
4using UnityEditor;
5
6namespace UnityEngine.InputSystem.Editor
7{
8 internal static class EditorPlayerSettingHelpers
9 {
10 /// <summary>
11 /// Whether the backends for the new input system are enabled in the
12 /// player settings for the Unity runtime.
13 /// </summary>
14 public static bool newSystemBackendsEnabled
15 {
16 get
17 {
18#if UNITY_2020_2_OR_NEWER
19 var property = GetPropertyOrNull(kActiveInputHandler);
20 return property == null || ActiveInputHandlerToTuple(property.intValue).newSystemEnabled;
21#else
22 var property = GetPropertyOrNull(kEnableNewSystemProperty);
23 return property == null || property.boolValue;
24#endif
25 }
26 set
27 {
28#if UNITY_2020_2_OR_NEWER
29 var property = GetPropertyOrNull(kActiveInputHandler);
30 if (property != null)
31 {
32 var tuple = ActiveInputHandlerToTuple(property.intValue);
33 tuple.newSystemEnabled = value;
34 property.intValue = TupleToActiveInputHandler(tuple);
35 property.serializedObject.ApplyModifiedProperties();
36 }
37 else
38 {
39 Debug.LogError($"Cannot find '{kActiveInputHandler}' in player settings");
40 }
41#else
42 var property = GetPropertyOrNull(kEnableNewSystemProperty);
43 if (property != null)
44 {
45 property.boolValue = value;
46 property.serializedObject.ApplyModifiedProperties();
47 }
48 else
49 {
50 Debug.LogError($"Cannot find '{kEnableNewSystemProperty}' in player settings");
51 }
52#endif
53 }
54 }
55
56 /// <summary>
57 /// Whether the backends for the old input system are enabled in the
58 /// player settings for the Unity runtime.
59 /// </summary>
60 public static bool oldSystemBackendsEnabled
61 {
62 get
63 {
64#if UNITY_2020_2_OR_NEWER
65 var property = GetPropertyOrNull(kActiveInputHandler);
66 return property == null || ActiveInputHandlerToTuple(property.intValue).oldSystemEnabled;
67#else
68 var property = GetPropertyOrNull(kDisableOldSystemProperty);
69 return property == null || !property.boolValue;
70#endif
71 }
72 set
73 {
74#if UNITY_2020_2_OR_NEWER
75 var property = GetPropertyOrNull(kActiveInputHandler);
76 if (property != null)
77 {
78 var tuple = ActiveInputHandlerToTuple(property.intValue);
79 tuple.oldSystemEnabled = value;
80 property.intValue = TupleToActiveInputHandler(tuple);
81 property.serializedObject.ApplyModifiedProperties();
82 }
83 else
84 {
85 Debug.LogError($"Cannot find '{kActiveInputHandler}' in player settings");
86 }
87#else
88 var property = GetPropertyOrNull(kDisableOldSystemProperty);
89 if (property != null)
90 {
91 property.boolValue = !value;
92 property.serializedObject.ApplyModifiedProperties();
93 }
94 else
95 {
96 Debug.LogError($"Cannot find '{kDisableOldSystemProperty}' in player settings");
97 }
98#endif
99 }
100 }
101
102
103#if UNITY_2020_2_OR_NEWER
104 private const string kActiveInputHandler = "activeInputHandler";
105
106 private enum InputHandler
107 {
108 OldInputManager = 0,
109 NewInputSystem = 1,
110 InputBoth = 2
111 };
112
113 private static (bool newSystemEnabled, bool oldSystemEnabled) ActiveInputHandlerToTuple(int value)
114 {
115 switch ((InputHandler)value)
116 {
117 case InputHandler.OldInputManager:
118 return (false, true);
119 case InputHandler.NewInputSystem:
120 return (true, false);
121 case InputHandler.InputBoth:
122 return (true, true);
123 default:
124 throw new ArgumentException($"Invalid value of 'activeInputHandler' setting: {value}");
125 }
126 }
127
128 private static int TupleToActiveInputHandler((bool newSystemEnabled, bool oldSystemEnabled) tuple)
129 {
130 switch (tuple)
131 {
132 case (false, true):
133 return (int)InputHandler.OldInputManager;
134 case (true, false):
135 return (int)InputHandler.NewInputSystem;
136 case (true, true):
137 return (int)InputHandler.InputBoth;
138 // Special case, when using two separate bool's of the public API here,
139 // it's possible to end up with both settings in false, for example:
140 // - EditorPlayerSettingHelpers.newSystemBackendsEnabled = true;
141 // - EditorPlayerSettingHelpers.oldSystemBackendsEnabled = false;
142 // - EditorPlayerSettingHelpers.newSystemBackendsEnabled = false;
143 // - EditorPlayerSettingHelpers.oldSystemBackendsEnabled = true;
144 // On line 3 both settings will be false, even if we set old system to true on line 4.
145 case (false, false):
146 return (int)InputHandler.OldInputManager;
147 }
148 }
149
150#else
151 private const string kEnableNewSystemProperty = "enableNativePlatformBackendsForNewInputSystem";
152 private const string kDisableOldSystemProperty = "disableOldInputManagerSupport";
153#endif
154
155 private static SerializedProperty GetPropertyOrNull(string name)
156 {
157 var playerSettings = Resources.FindObjectsOfTypeAll<PlayerSettings>().FirstOrDefault();
158 if (playerSettings == null)
159 return null;
160 var playerSettingsObject = new SerializedObject(playerSettings);
161 return playerSettingsObject.FindProperty(name);
162 }
163 }
164}
165#endif // UNITY_EDITOR