DualShockSupport.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using UnityEngine.InputSystem.Layouts;
  2. namespace UnityEngine.InputSystem.DualShock
  3. {
  4. /// <summary>
  5. /// Adds support for PS4 DualShock controllers.
  6. /// </summary>
  7. #if UNITY_DISABLE_DEFAULT_INPUT_PLUGIN_INITIALIZATION
  8. public
  9. #else
  10. internal
  11. #endif
  12. static class DualShockSupport
  13. {
  14. public static void Initialize()
  15. {
  16. InputSystem.RegisterLayout<DualShockGamepad>();
  17. // HID version for platforms where we pick up the controller as a raw HID.
  18. // This works without any PS4-specific drivers but does not support the full
  19. // range of capabilities of the controller (the HID format is undocumented
  20. // and only partially understood).
  21. //
  22. // NOTE: We match by PID and VID here as that is the most reliable way. The product
  23. // and manufacturer strings we get from APIs often return inconsistent results
  24. // or none at all. E.g. when connected via Bluetooth on OSX, the DualShock will
  25. // not return anything from IOHIDDevice_GetProduct() and IOHIDevice_GetManufacturer()
  26. // even though it will report the expected results when plugged in via USB.
  27. #if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_WSA || UNITY_EDITOR
  28. InputSystem.RegisterLayout<DualShock4GamepadHID>(
  29. matches: new InputDeviceMatcher()
  30. .WithInterface("HID")
  31. .WithCapability("vendorId", 0x54C) // Sony Entertainment.
  32. .WithCapability("productId", 0x9CC)); // Wireless controller.
  33. // Just to make sure, also set up a matcher that goes by strings so that we cover
  34. // all bases.
  35. InputSystem.RegisterLayoutMatcher<DualShock4GamepadHID>(
  36. new InputDeviceMatcher()
  37. .WithInterface("HID")
  38. .WithManufacturer("Sony.+Entertainment")
  39. .WithProduct("Wireless Controller"));
  40. InputSystem.RegisterLayout<DualShock3GamepadHID>(
  41. matches: new InputDeviceMatcher()
  42. .WithInterface("HID")
  43. .WithCapability("vendorId", 0x54C) // Sony Entertainment.
  44. .WithCapability("productId", 0x268)); // PLAYSTATION(R)3 Controller.
  45. InputSystem.RegisterLayoutMatcher<DualShock3GamepadHID>(
  46. new InputDeviceMatcher()
  47. .WithInterface("HID")
  48. .WithManufacturer("Sony.+Entertainment")
  49. .WithProduct("PLAYSTATION(R)3 Controller"));
  50. #endif
  51. }
  52. }
  53. }