UI Toolbox logoUI Toolbox
Support

VirtualStick and TouchButton

VirtualStick and TouchButton in UIToolbox are the controls of a touch game: a thumbstick that moves the player and round buttons that fire, jump and aim. A finger or the mouse holds each of them. With no settings, the stick is a 200 by 200 area with a floating base, and the button is a 72 pixel circle that raises Pressed and Released.

Two virtual sticks, Move on the left and Aim and fire on the right, with a diamond button at the top centerTwo virtual sticks, Move on the left and Aim and fire on the right, with a diamond button at the top center

Demo scenes: VirtualStickDemo.unity and TouchButtonDemo.unity.

How to add them:

<tb:VirtualStick name="move" />
<tb:TouchButton name="fire" hold-to-repeat="true" />
<tb:TouchButton name="aim" drag-aim="true" />

How to read the stick, and answer the buttons:

var move = root.Q<VirtualStick>("move");
var fire = root.Q<TouchButton>("fire");
var aim = root.Q<TouchButton>("aim");

// In your update loop: x to the right, y up, a length from 0 to 1
player.Move(move.Value * speed * Time.deltaTime);

fire.Pressed += weapon.Shoot;          // again at each repeat while held
aim.AimChanged += direction => weapon.Aim(direction);

Value is zero while nobody holds the stick. Under the dead zone it is zero too, and above the dead zone it grows from zero, so the stick has no jump at the edge. ValueChanged is raised when the value changes, also when the finger lifts and the value goes back to zero.

Each control follows the pointer that pressed it, until that pointer lifts. A second finger on the same control is ignored, and a finger on another control works at the same time. So one thumb moves, another fires, and nothing blocks the other. A mouse works like one finger, with the left button.

VirtualStick

Member Description
Mode (mode) Where the base sits while the stick is held. Fixed: in the middle of the area. Floating: where the finger went down. Dynamic: like Floating, and the base follows the finger when the finger goes farther than the radius. Default Floating. The type is StickMode.
Radius (radius) The pixels that the thumb travels from the center of the base to full tilt. Default 64.
DeadZone (dead-zone) The share of the radius, from 0 to 0.95, under which the value is zero. Default 0.1.
SnapDirections (snap-directions) 0 gives every direction. 4 snaps the direction to right, up, left and down, and 8 adds the diagonals. The strength stays free. Default 0.
HideWhenIdle (hide-when-idle) True fades the base and the thumb out while nobody holds the stick. Default false.
Value The tilt: x to the right, y up, a length from 0 to 1.
IsActive True while a finger or the mouse holds the stick.
Release() Lets go as if the finger lifted. Call it when the game pauses or the stick hides.
Push(tilt) Tilts the stick from code, such as from a gamepad stick, as a finger at that tilt would. The dead zone and the snap apply, a held pointer wins, and zero lets go.
ValueChanged The value changed: the new value.
ControlPath Input System only: the control that receives Value, such as <Gamepad>/leftStick.

The area of the stick is the element itself. Size it with USS: a stick that fills the left half of the screen takes a touch anywhere in that half. A Floating base stays inside the area.

.move-stick {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 50%;
    height: 50%;
}

TouchButton

Three round TouchButton controls over a scene with fire, shield and bolt icons, the bolt button cooling downThree round TouchButton controls over a scene with fire, shield and bolt icons, the bolt button cooling down

Member Description
Icon (icon) The picture on the button: a Texture2D, a Sprite or a vector image.
HoldToRepeat (hold-to-repeat) True raises Pressed again while the button is held, for an automatic weapon. The first repeat comes after 0.4 seconds. Default false.
RepeatInterval (repeat-interval) The seconds between the repeats, from 0.02. Default 0.15.
DragAim (drag-aim) True turns the drag of the finger into Aim. Default false.
AimRadius (aim-radius) The pixels of drag that give an Aim of length 1. Default 64.
IsPressed True while a finger or the mouse holds the button.
Aim The drag from the place where the finger went down: x to the right, y up, a length from 0 to 1. Zero while DragAim is false and while nobody holds the button.
Release() Lets go as if the finger lifted. Raises Released when the button was held.
Pressed The button went down, and again at each repeat when HoldToRepeat is true.
Released The button came up: the finger lifted, the pointer was canceled, or Release() ran.
AimChanged Aim changed: the new aim. Also raised with zero at the release.
ControlPath Input System only: the control that receives 1 while the button is held and 0 otherwise, such as <Gamepad>/buttonSouth.

Enter, Space and the gamepad submit button raise Pressed and Released at once, so the button also works with a keyboard or a gamepad.

How to put a cooldown on a button: put an AbilitySlot inside it, set its picking mode to Ignore so that the button gets the touch, and start the cooldown from the Pressed handler. The slot fills the button and shows the sweep and the seconds.

<tb:TouchButton name="jump">
  <tb:AbilitySlot
      name="jumpSlot"
      picking-mode="Ignore"
      focusable="false"
      shape="Circle" />
</tb:TouchButton>
var slot = root.Q<AbilitySlot>("jumpSlot");
root.Q<TouchButton>("jump").Pressed += () =>
{
    if (slot.IsCoolingDown) return;
    player.Jump();
    slot.StartCooldown(2);
};

Input System

With the Input System package, a control can feed an on-screen control of the package, so a game that reads actions needs no other code. Set ControlPath to the path of the control:

stick.ControlPath = "<Gamepad>/leftStick";
fire.ControlPath = "<Gamepad>/buttonSouth";

An action that is bound to that control then answers the touch. The path is empty by default and feeds nothing. Without the package the property does not exist, and the controls work with Value, Pressed and Released.

Tips:

Styling

Class Element
tb-virtual-stick The area of the stick.
tb-virtual-stick--active A finger holds the stick.
tb-virtual-stick--hide-idle HideWhenIdle is true.
tb-virtual-stick__base The ring.
tb-virtual-stick__thumb The thumb.
tb-touch-button The button.
tb-touch-button--pressed A finger holds the button.
tb-touch-button__icon The picture.
.my-hud .tb-virtual-stick__base {
    border-color: rgba(255, 255, 255, 0.6);
    background-color: rgba(0, 0, 0, 0.5);
}

.my-hud .tb-touch-button {
    width: 88px;
    height: 88px;
}

The VirtualStickDemo scene shows a stick with no setup that moves a hero, the two sticks of a twin-stick shooter, a stick that snaps to four ways in a grid game, and a stick that shows only while it is held.

The TouchButtonDemo scene shows a fire button that repeats while held, a button that aims a spell with a drag, a jump button with a cooldown, and the size, the art and the shape of a button.