19#if UNITY_2019_1_OR_NEWER && INPUTSYSTEM_AVAILABLE
20using UnityEngine.InputSystem;
30 public static class Input
34 public enum WandButton : UInt32
44 [Obsolete(
"WandButton.System is deprecated, please use Wandbutton.T5 instead.",
true)]
46 [Obsolete(
"WandButton.Z is deprecated, please use Wandbutton.Three instead.",
true)]
50 #endregion Public Enums
53 #region Private Fields
55#if UNITY_2019_1_OR_NEWER && INPUTSYSTEM_AVAILABLE
56 internal static GlassesDevice[] glassesDevices =
new GlassesDevice[PlayerSettings.MAX_SUPPORTED_PLAYERS];
57 internal static WandDevice[,] wandDevices =
new WandDevice[PlayerSettings.MAX_SUPPORTED_PLAYERS, 2];
63 #region Public Functions
74 public static bool GetButton(WandButton button,
77 return TryGetButton(button, out var pressed, controllerIndex, playerIndex) && pressed;
89 public static bool TryGetButton(WandButton button, out
bool pressed,
92 if(!Player.TryGetGlassesHandle(playerIndex, out var glassesHandle))
98 return Wand.TryGetButton(button, out pressed, glassesHandle, controllerIndex);
109 public static bool GetButtonDown(WandButton button,
112 return TryGetButtonDown(button, out var buttonDown, controllerIndex, playerIndex) && buttonDown;
124 public static bool TryGetButtonDown(WandButton button, out
bool buttonDown,
128 if(!Player.TryGetGlassesHandle(playerIndex, out var glassesHandle))
134 return Wand.TryGetButtonDown(button, out buttonDown, glassesHandle, controllerIndex);
144 public static bool GetButtonUp(WandButton button,
147 return TryGetButtonUp(button, out var buttonUp, controllerIndex, playerIndex) && buttonUp;
160 public static bool TryGetButtonUp(WandButton button, out
bool buttonUp,
165 if(!Player.TryGetGlassesHandle(playerIndex, out var glassesHandle))
171 return Wand.TryGetButtonUp(button, out buttonUp, glassesHandle, controllerIndex);
182 if(TryGetStickTilt(out var stickTilt, controllerIndex, playerIndex))
196 public static bool TryGetStickTilt(out Vector2 stickTilt,
199 if(!Player.TryGetGlassesHandle(playerIndex, out var glassesHandle))
201 stickTilt = Vector2.zero;
204 return Wand.TryGetStickTilt(out stickTilt, glassesHandle, controllerIndex);
216 if(TryGetTrigger(out var triggerDisplacement, controllerIndex, playerIndex))
218 return triggerDisplacement;
231 public static bool TryGetTrigger(out
float triggerDisplacement,
234 if (!Player.TryGetGlassesHandle(playerIndex, out var glassesHandle))
236 triggerDisplacement = 0f;
239 return Wand.TryGetTrigger(out triggerDisplacement, glassesHandle, controllerIndex);
249 return Wand.TryCheckConnected(out var connected,
PlayerIndex.One, controllerIndex) && connected;
252 public static void Update()
259 #endregion Public Functions
262 #region Internal Functions
264 internal static bool GetButton(
this T5_ControllerState controllerState, WandButton button)
266 var buttonsState = controllerState.ButtonsState;
271 return buttonsState.T5;
273 return buttonsState.One;
275 return buttonsState.Two;
277 return buttonsState.Y;
279 return buttonsState.B;
281 return buttonsState.A;
283 return buttonsState.X;
284 case WandButton.Three:
285 return buttonsState.Three;
287 throw new ArgumentException(
"Invalid WandButton argument - enum value does not exist");
292 internal static bool TryGetButton(
this T5_ControllerState controllerState, WandButton button)
294 return controllerState.ButtonsValid && controllerState.GetButton(button);
297 internal static Vector2 TryGetStick(
this T5_ControllerState controllerState)
299 return controllerState.AnalogValid ? controllerState.Stick : Vector2.zero;
302 internal static float TryGetTrigger(
this T5_ControllerState controllerState)
304 return controllerState.AnalogValid ? controllerState.Trigger : 0f;
307#if UNITY_2019_1_OR_NEWER && INPUTSYSTEM_AVAILABLE
308 internal static GlassesDevice GetGlassesDevice(
PlayerIndex playerIndex)
310 return glassesDevices[(int)playerIndex - 1];
315 return wandDevices[(int)playerIndex - 1, (
int)controllerIndex];
319 #endregion Internal Functions
322 #region Private Functions
329#if UNITY_2019_1_OR_NEWER && INPUTSYSTEM_AVAILABLE
330 internal static void AddGlassesDevice(
PlayerIndex playerIndex)
332 var glassesDevices = Input.glassesDevices;
333 int i = (int)playerIndex - 1;
336 if (glassesDevices[i] ==
null)
338 var preexistingGlassesDevice = InputSystem.GetDevice<GlassesDevice>($
"Player{playerIndex}");
339 if (preexistingGlassesDevice !=
null)
341 glassesDevices[i] = preexistingGlassesDevice;
342 glassesDevices[i].PlayerIndex = playerIndex;
346 glassesDevices[i] = InputSystem.AddDevice<GlassesDevice>($
"T5 Glasses - Player {playerIndex}");
347 glassesDevices[i].PlayerIndex = playerIndex;
348 InputSystem.AddDeviceUsage(glassesDevices[i], $
"Player{playerIndex}");
352 else if (!glassesDevices[i].added)
354 InputSystem.AddDevice(glassesDevices[i]);
360 var wandDevices = Input.wandDevices;
361 int i = (int)playerIndex - 1;
362 int j = (int)controllerIndex;
367 var handednessLabel = controllerIndex == ControllerIndex.Right ?
"Right" :
"Left";
370 if (wandDevices[i, j] !=
null && !wandDevices[i, j].added)
372 var wandDevice = wandDevices[i, j];
373 InputSystem.AddDevice(wandDevice);
374 wandDevice.ControllerIndex = controllerIndex;
375 InputSystem.QueueConfigChangeEvent(wandDevice);
376 InputSystem.AddDevice(wandDevice);
383 WandDevice currentWandDevice = InputSystem.GetDevice<WandDevice>($
"Player{playerIndex}-{handednessLabel}Hand");
386 if (currentWandDevice !=
null)
388 wandDevices[i, j] = currentWandDevice;
393 wandDevices[i, j] = InputSystem.AddDevice<WandDevice>($
"T5 Wand - P{(int)playerIndex} {handednessLabel}");
396 wandDevices[i, j].playerIndex = playerIndex;
397 wandDevices[i, j].ControllerIndex = controllerIndex;
398 InputSystem.AddDeviceUsage(wandDevices[i, j], $
"Player{playerIndex}");
399 InputSystem.AddDeviceUsage(wandDevices[i, j], $
"Player{playerIndex}-{handednessLabel}Hand");
400 InputSystem.AddDeviceUsage(wandDevices[i, j], wandDevices[i, j].
ControllerIndex ==
ControllerIndex.Left ? CommonUsages.LeftHand : CommonUsages.RightHand);
401 InputSystem.QueueConfigChangeEvent(wandDevices[i, j]);
405 internal static void RemoveGlassesDevice(
PlayerIndex playerIndex)
407 var glassesDevices = Input.glassesDevices;
408 int i = (int)playerIndex - 1;
411 if (glassesDevices[i] !=
null)
413 var preexistingGlassesDevice = InputSystem.GetDevice<GlassesDevice>($
"Player{playerIndex}");
414 if (preexistingGlassesDevice !=
null)
416 InputSystem.RemoveDevice(preexistingGlassesDevice);
417 glassesDevices[i] =
null;
424 var wandDevices = Input.wandDevices;
425 int i = (int)playerIndex - 1;
426 int j = (int)controllerIndex;
431 var handednessLabel = controllerIndex == ControllerIndex.Right ?
"Right" :
"Left";
434 if (wandDevices[i, j] !=
null && !wandDevices[i, j].added)
436 var wandDevice = wandDevices[i, j];
437 InputSystem.RemoveDevice(wandDevice);
438 InputSystem.QueueConfigChangeEvent(wandDevice);
439 wandDevices[i, j] =
null;
446 WandDevice currentWandDevice = InputSystem.GetDevice<WandDevice>($
"Player{playerIndex}-{handednessLabel}Hand");
449 if (currentWandDevice !=
null)
451 InputSystem.RemoveDevice(currentWandDevice);
452 InputSystem.QueueConfigChangeEvent(currentWandDevice);
453 wandDevices[i, j] =
null;
459 #endregion Private Functions
ControllerIndex
Since wands are all physically identical (they have no "handedness"), it doesn't make sense to addres...
PlayerIndex
The Player index (e.g. Player One, Player Two, etc)