slimecing

a fighting game featuring slimes and swords
Log | Files | Refs | README

GamepadIconsExample.cs (4728B)


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