Virtual buttons

IntermediateProgrammer

Rather than bind controls to physical keys and buttons, you can bind them to virtual buttons. Players can then assign physical buttons to the virtual buttons, allowing them to create their own control schemes.

For example, imagine you develop a first-person shooter game and need to assign a key for the UseMedkit function. Rather than bind the function to a particular key, you can create a virtual button called UseMedkit, then bind the virtual button to, say, the F key. If they want to, the player can then bind the virtual key to a different key at runtime.

Virtual buttons

Use virtual buttons

  • Bind a key, button, or pointer to a virtual button (eg MyButton).
  • Create a list of virtual buttons.
  • Add MyButton to the list of virtual buttons.
  • Assign a function to MyButton.
  • Create additional virtual buttons.
  • Add the additional buttons to the same list, or create additional lists.

Example code

  1. public override void Start()
  2. {
  3. base.Start();
  4. // Create a new VirtualButtonConfigSet if none exists.
  5. Input.VirtualButtonConfigSet = Input.VirtualButtonConfigSet ?? new VirtualButtonConfigSet();
  6. //Bind "M" key, GamePad "Start" button and left mouse button to a virtual button "MyButton".
  7. VirtualButtonBinding b1 = new VirtualButtonBinding("MyButton", VirtualButton.Keyboard.M);
  8. VirtualButtonBinding b2 = new VirtualButtonBinding("MyButton", VirtualButton.GamePad.Start);
  9. VirtualButtonBinding b3 = new VirtualButtonBinding("MyButton", VirtualButton.Mouse.Left);
  10. VirtualButtonConfig c = new VirtualButtonConfig();
  11. c.Add(b1);
  12. c.Add(b2);
  13. c.Add(b3);
  14. Input.VirtualButtonConfigSet.Add(c);
  15. }
  16. public override void Update() {
  17. float button = Input.GetVirtualButton(0, "MyButton");
  18. }

See also