5
5
namespace Explorer
6
6
{
7
7
/// <summary>
8
- /// Version-agnostic UnityEngine Input module using Reflection.
8
+ /// Version-agnostic Input module using Reflection.
9
9
/// </summary>
10
10
public static class InputHelper
11
11
{
12
12
// If Input module failed to load at all
13
13
public static bool NO_INPUT ;
14
14
15
- // Base UnityEngine.Input class
16
- private static Type Input => _input ?? ( _input = ReflectionHelpers . GetTypeByName ( "UnityEngine.Input" ) ) ;
15
+ // If using new InputSystem module
16
+ public static bool USING_NEW_INPUT ;
17
+
18
+ // Cached Types
19
+ private static Type TInput => _input ?? ( _input = ReflectionHelpers . GetTypeByName ( "UnityEngine.Input" ) ) ;
17
20
private static Type _input ;
18
21
19
- // Cached member infos
22
+ private static Type TKeyboard => _keyboardSys ?? ( _keyboardSys = ReflectionHelpers . GetTypeByName ( "UnityEngine.InputSystem.Keyboard" ) ) ;
23
+ private static Type _keyboardSys ;
24
+
25
+ private static Type TMouse => _mouseSys ?? ( _mouseSys = ReflectionHelpers . GetTypeByName ( "UnityEngine.InputSystem.Mouse" ) ) ;
26
+ private static Type _mouseSys ;
27
+
28
+ private static Type TKey => _key ?? ( _key = ReflectionHelpers . GetTypeByName ( "UnityEngine.InputSystem.Key" ) ) ;
29
+ private static Type _key ;
30
+
31
+ // Cached member infos (new system)
32
+ private static PropertyInfo _keyboardCurrent ;
33
+ private static PropertyInfo _kbItemProp ;
34
+ private static PropertyInfo _isPressed ;
35
+ private static PropertyInfo _wasPressedThisFrame ;
36
+ private static PropertyInfo _mouseCurrent ;
37
+ private static PropertyInfo _leftButton ;
38
+ private static PropertyInfo _rightButton ;
39
+ private static PropertyInfo _position ;
40
+ private static MethodInfo _readValueMethod ;
41
+
42
+ // Cached member infos (legacy)
20
43
private static PropertyInfo _mousePosition ;
21
44
private static MethodInfo _getKey ;
22
45
private static MethodInfo _getKeyDown ;
@@ -25,74 +48,175 @@ public static class InputHelper
25
48
26
49
public static void Init ( )
27
50
{
28
- if ( Input == null && ! TryManuallyLoadInput ( ) )
51
+ if ( TKeyboard != null || TryManuallyLoadNewInput ( ) )
29
52
{
30
- NO_INPUT = true ;
53
+ InitNewInput ( ) ;
31
54
return ;
32
55
}
33
56
34
- // Cache reflection now that we know Input is loaded
57
+ if ( TInput != null || TryManuallyLoadLegacyInput ( ) )
58
+ {
59
+ InitLegacyInput ( ) ;
60
+ return ;
61
+ }
62
+
63
+ ExplorerCore . LogWarning ( "Could not find any Input module!" ) ;
64
+ NO_INPUT = true ;
65
+ }
66
+
67
+ private static void InitNewInput ( )
68
+ {
69
+ ExplorerCore . Log ( "Initializing new InputSystem support..." ) ;
70
+
71
+ USING_NEW_INPUT = true ;
72
+
73
+ _keyboardCurrent = TKeyboard . GetProperty ( "current" ) ;
74
+ _kbItemProp = TKeyboard . GetProperty ( "Item" , new Type [ ] { TKey } ) ;
75
+
76
+ var btnControl = ReflectionHelpers . GetTypeByName ( "UnityEngine.InputSystem.Controls.ButtonControl" ) ;
77
+ _isPressed = btnControl . GetProperty ( "isPressed" ) ;
78
+ _wasPressedThisFrame = btnControl . GetProperty ( "wasPressedThisFrame" ) ;
79
+
80
+ _mouseCurrent = TMouse . GetProperty ( "current" ) ;
81
+ _leftButton = TMouse . GetProperty ( "leftButton" ) ;
82
+ _rightButton = TMouse . GetProperty ( "rightButton" ) ;
83
+
84
+ _position = ReflectionHelpers . GetTypeByName ( "UnityEngine.InputSystem.Pointer" )
85
+ . GetProperty ( "position" ) ;
86
+
87
+ _readValueMethod = ReflectionHelpers . GetTypeByName ( "UnityEngine.InputSystem.InputControl`1" )
88
+ . MakeGenericType ( typeof ( Vector2 ) )
89
+ . GetMethod ( "ReadValue" ) ;
90
+ }
91
+
92
+ private static void InitLegacyInput ( )
93
+ {
94
+ ExplorerCore . Log ( "Initializing Legacy Input support..." ) ;
35
95
36
- _mousePosition = Input . GetProperty ( "mousePosition" ) ;
96
+ _mousePosition = TInput . GetProperty ( "mousePosition" ) ;
97
+ _getKey = TInput . GetMethod ( "GetKey" , new Type [ ] { typeof ( KeyCode ) } ) ;
98
+ _getKeyDown = TInput . GetMethod ( "GetKeyDown" , new Type [ ] { typeof ( KeyCode ) } ) ;
99
+ _getMouseButton = TInput . GetMethod ( "GetMouseButton" , new Type [ ] { typeof ( int ) } ) ;
100
+ _getMouseButtonDown = TInput . GetMethod ( "GetMouseButtonDown" , new Type [ ] { typeof ( int ) } ) ;
101
+ }
37
102
38
- _getKey = Input . GetMethod ( "GetKey" , new Type [ ] { typeof ( KeyCode ) } ) ;
39
- _getKeyDown = Input . GetMethod ( "GetKeyDown" , new Type [ ] { typeof ( KeyCode ) } ) ;
40
- _getMouseButton = Input . GetMethod ( "GetMouseButton" , new Type [ ] { typeof ( int ) } ) ;
41
- _getMouseButtonDown = Input . GetMethod ( "GetMouseButtonDown" , new Type [ ] { typeof ( int ) } ) ;
103
+ private static bool TryManuallyLoadNewInput ( )
104
+ {
105
+ if ( ReflectionHelpers . LoadModule ( "Unity.InputSystem" ) && TKeyboard != null )
106
+ {
107
+ ExplorerCore . Log ( "Loaded new InputSystem module!" ) ;
108
+ return true ;
109
+ }
110
+ else
111
+ {
112
+ return false ;
113
+ }
42
114
}
43
115
44
- #pragma warning disable IDE1006 // Camel-case property (Unity style)
45
- public static Vector3 mousePosition
116
+ private static bool TryManuallyLoadLegacyInput ( )
117
+ {
118
+ if ( ( ReflectionHelpers . LoadModule ( "UnityEngine.InputLegacyModule" ) || ReflectionHelpers . LoadModule ( "UnityEngine.CoreModule" ) )
119
+ && TInput != null )
120
+ {
121
+ ExplorerCore . Log ( "Loaded legacy InputModule!" ) ;
122
+ return true ;
123
+ }
124
+ else
125
+ {
126
+ return false ;
127
+ }
128
+ }
129
+
130
+ public static Vector3 MousePosition
46
131
{
47
132
get
48
133
{
49
134
if ( NO_INPUT ) return Vector3 . zero ;
135
+
136
+ if ( USING_NEW_INPUT )
137
+ {
138
+ var mouse = _mouseCurrent . GetValue ( null , null ) ;
139
+ var pos = _position . GetValue ( mouse , null ) ;
140
+
141
+ return ( Vector2 ) _readValueMethod . Invoke ( pos , new object [ 0 ] ) ;
142
+ }
143
+
50
144
return ( Vector3 ) _mousePosition . GetValue ( null , null ) ;
51
145
}
52
146
}
53
- #pragma warning restore IDE1006
54
147
55
148
public static bool GetKeyDown ( KeyCode key )
56
149
{
57
150
if ( NO_INPUT ) return false ;
151
+
152
+ if ( USING_NEW_INPUT )
153
+ {
154
+ var parsed = Enum . Parse ( TKey , key . ToString ( ) ) ;
155
+ var currentKB = _keyboardCurrent . GetValue ( null , null ) ;
156
+ var actualKey = _kbItemProp . GetValue ( currentKB , new object [ ] { parsed } ) ;
157
+
158
+ return ( bool ) _wasPressedThisFrame . GetValue ( actualKey , null ) ;
159
+ }
160
+
58
161
return ( bool ) _getKeyDown . Invoke ( null , new object [ ] { key } ) ;
59
162
}
60
163
61
164
public static bool GetKey ( KeyCode key )
62
165
{
63
166
if ( NO_INPUT ) return false ;
167
+
168
+ if ( USING_NEW_INPUT )
169
+ {
170
+ var parsed = Enum . Parse ( TKey , key . ToString ( ) ) ;
171
+ var currentKB = _keyboardCurrent . GetValue ( null , null ) ;
172
+ var actualKey = _kbItemProp . GetValue ( currentKB , new object [ ] { parsed } ) ;
173
+
174
+ return ( bool ) _isPressed . GetValue ( actualKey , null ) ;
175
+ }
176
+
64
177
return ( bool ) _getKey . Invoke ( null , new object [ ] { key } ) ;
65
178
}
66
179
67
- /// <param name="btn">1 = left, 2 = middle, 3 = right, etc</param>
180
+ /// <param name="btn">0/ 1 = left, 2 = middle, 3 = right, etc</param>
68
181
public static bool GetMouseButtonDown ( int btn )
69
182
{
70
183
if ( NO_INPUT ) return false ;
184
+
185
+ if ( USING_NEW_INPUT )
186
+ {
187
+ var mouse = _mouseCurrent . GetValue ( null , null ) ;
188
+
189
+ PropertyInfo btnProp ;
190
+ if ( btn < 2 ) btnProp = _leftButton ;
191
+ else btnProp = _rightButton ;
192
+
193
+ var actualBtn = btnProp . GetValue ( mouse , null ) ;
194
+
195
+ return ( bool ) _wasPressedThisFrame . GetValue ( actualBtn , null ) ;
196
+ }
197
+
71
198
return ( bool ) _getMouseButtonDown . Invoke ( null , new object [ ] { btn } ) ;
72
199
}
73
200
74
201
/// <param name="btn">1 = left, 2 = middle, 3 = right, etc</param>
75
202
public static bool GetMouseButton ( int btn )
76
203
{
77
204
if ( NO_INPUT ) return false ;
78
- return ( bool ) _getMouseButton . Invoke ( null , new object [ ] { btn } ) ;
79
- }
80
-
81
- private static bool TryManuallyLoadInput ( )
82
- {
83
- ExplorerCore . Log ( "UnityEngine.Input is null, trying to load manually...." ) ;
84
205
85
- if ( ( ReflectionHelpers . LoadModule ( "UnityEngine.InputLegacyModule" ) || ReflectionHelpers . LoadModule ( "UnityEngine.CoreModule" ) )
86
- && Input != null )
87
- {
88
- ExplorerCore . Log ( "Ok!" ) ;
89
- return true ;
90
- }
91
- else
206
+ if ( USING_NEW_INPUT )
92
207
{
93
- ExplorerCore . Log ( "Could not load Input module!" ) ;
94
- return false ;
208
+ var mouse = _mouseCurrent . GetValue ( null , null ) ;
209
+
210
+ PropertyInfo btnProp ;
211
+ if ( btn < 2 ) btnProp = _leftButton ;
212
+ else btnProp = _rightButton ;
213
+
214
+ var actualBtn = btnProp . GetValue ( mouse , null ) ;
215
+
216
+ return ( bool ) _isPressed . GetValue ( actualBtn , null ) ;
95
217
}
218
+
219
+ return ( bool ) _getMouseButton . Invoke ( null , new object [ ] { btn } ) ;
96
220
}
97
221
}
98
222
}
0 commit comments