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