GamepadIconsExample.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using UnityEngine.UI;
  3. ////TODO: have updateBindingUIEvent receive a control path string, too (in addition to the device layout name)
  4. namespace UnityEngine.InputSystem.Samples.RebindUI
  5. {
  6. /// <summary>
  7. /// This is an example for how to override the default display behavior of bindings. The component
  8. /// hooks into <see cref="RebindActionUI.updateBindingUIEvent"/> which is triggered when UI display
  9. /// of a binding should be refreshed. It then checks whether we have an icon for the current binding
  10. /// and if so, replaces the default text display with an icon.
  11. /// </summary>
  12. public class GamepadIconsExample : MonoBehaviour
  13. {
  14. public GamepadIcons xbox;
  15. public GamepadIcons ps4;
  16. protected void OnEnable()
  17. {
  18. // Hook into all updateBindingUIEvents on all RebindActionUI components in our hierarchy.
  19. var rebindUIComponents = transform.GetComponentsInChildren<RebindActionUI>();
  20. foreach (var component in rebindUIComponents)
  21. {
  22. component.updateBindingUIEvent.AddListener(OnUpdateBindingDisplay);
  23. component.UpdateBindingDisplay();
  24. }
  25. }
  26. protected void OnUpdateBindingDisplay(RebindActionUI component, string bindingDisplayString, string deviceLayoutName, string controlPath)
  27. {
  28. if (string.IsNullOrEmpty(deviceLayoutName) || string.IsNullOrEmpty(controlPath))
  29. return;
  30. var icon = default(Sprite);
  31. if (InputSystem.IsFirstLayoutBasedOnSecond(deviceLayoutName, "DualShockGamepad"))
  32. icon = ps4.GetSprite(controlPath);
  33. else if (InputSystem.IsFirstLayoutBasedOnSecond(deviceLayoutName, "Gamepad"))
  34. icon = xbox.GetSprite(controlPath);
  35. var textComponent = component.bindingText;
  36. // Grab Image component.
  37. var imageGO = textComponent.transform.parent.Find("ActionBindingIcon");
  38. var imageComponent = imageGO.GetComponent<Image>();
  39. if (icon != null)
  40. {
  41. textComponent.gameObject.SetActive(false);
  42. imageComponent.sprite = icon;
  43. imageComponent.gameObject.SetActive(true);
  44. }
  45. else
  46. {
  47. textComponent.gameObject.SetActive(true);
  48. imageComponent.gameObject.SetActive(false);
  49. }
  50. }
  51. [Serializable]
  52. public struct GamepadIcons
  53. {
  54. public Sprite buttonSouth;
  55. public Sprite buttonNorth;
  56. public Sprite buttonEast;
  57. public Sprite buttonWest;
  58. public Sprite startButton;
  59. public Sprite selectButton;
  60. public Sprite leftTrigger;
  61. public Sprite rightTrigger;
  62. public Sprite leftShoulder;
  63. public Sprite rightShoulder;
  64. public Sprite dpad;
  65. public Sprite dpadUp;
  66. public Sprite dpadDown;
  67. public Sprite dpadLeft;
  68. public Sprite dpadRight;
  69. public Sprite leftStick;
  70. public Sprite rightStick;
  71. public Sprite leftStickPress;
  72. public Sprite rightStickPress;
  73. public Sprite GetSprite(string controlPath)
  74. {
  75. // From the input system, we get the path of the control on device. So we can just
  76. // map from that to the sprites we have for gamepads.
  77. switch (controlPath)
  78. {
  79. case "buttonSouth": return buttonSouth;
  80. case "buttonNorth": return buttonNorth;
  81. case "buttonEast": return buttonEast;
  82. case "buttonWest": return buttonWest;
  83. case "start": return startButton;
  84. case "select": return selectButton;
  85. case "leftTrigger": return leftTrigger;
  86. case "rightTrigger": return rightTrigger;
  87. case "leftShoulder": return leftShoulder;
  88. case "rightShoulder": return rightShoulder;
  89. case "dpad": return dpad;
  90. case "dpad/up": return dpadUp;
  91. case "dpad/down": return dpadDown;
  92. case "dpad/left": return dpadLeft;
  93. case "dpad/right": return dpadRight;
  94. case "leftStick": return leftStick;
  95. case "rightStick": return rightStick;
  96. case "leftStickPress": return leftStickPress;
  97. case "rightStickPress": return rightStickPress;
  98. }
  99. return null;
  100. }
  101. }
  102. }
  103. }