InControl: Touch Controls

InControl provides touch control functionality by means of a virtual device which behaves like any other controllers. Currently, four control components are provided:

  1. Button controls
  2. Virtual stick controls
  3. Directional swipe controls
  4. Track pad controls

Controls can be positioned and sized either in terms of screen percent or pixels.

Getting Started

First, make sure you have the InControlManager added to the scene. You can add it via the GameObject > InControl > Manager menu command. This will:

  1. Create a new game object in the scene hierarchy named InControl.
  2. Attach the InControlManager component to the InControl game object.

Next, create the TouchManager component. You can add it via the GameObject > InControl > Touch > Manager menu command. This will:

  1. Add the Touch Manager component to the InControl game object.
  2. Create a Touch Camera game object as a child to the InControl game object.

InControlManager and TouchManager inspectors

At this point InControl is set up for touch support. Even without adding touch controls, you can now take advantage of the TouchManager API to query touches, which provides a few advantages over Unity’s touch API.

Touches in InControl are represented by classes instead of by structs like Unity’s touches. This means you can grab a reference to a touch and hang on to it. You can also compare touches for equality using these references. The primary advantage is tracking touches is far easier. InControl also ensures touches are properly cancelled when they should be. Unity does not always take care of this.

Touch Manager API

The TouchManager component is also a singleton class with a static public API, making it convenient to use and query elsewhere in your project.

public class TouchManager
{
    public static ReadOnlyCollection<Touch> Touches;
    public static int TouchCount;
    public static Touch GetTouch( int touchIndex );
    public static Touch GetTouchByFingerId( int fingerId );
    public static GizmoShowOption ControlsShowGizmos;
    public static bool ControlsEnabled;
}

The first three correspond, essentially, to Unity&rsquo;s Input.touches, Input.touchCount and Input.GetTouch(). These are used to query and return instances of the Touch class:

public class Touch
{
    public int fingerId;

    public TouchPhase phase;
    public int tapCount;

    public Vector2 position;
    public Vector2 deltaPosition;
    public Vector2 lastPosition;

    public float deltaTime;
    public ulong updateTick;
}

These correspond to the equivalent members of Unity&rsquo;s Touch struct, and add a few more.

Gizmo Visibility

You can use the TouchManager.ControlsShowGizmos property to control gizmo visibility for touch controls. Since sprites are not instantiated and displayed until the app is playing, it can be hard to visualize where your controls are. InControl will display gizmos to assist you in setup and layout of your touch controls. This setting will let you fine tune when gizmos are visible for each control. Valid options are:

public enum GizmoShowOption
{
    Never,
    WhenSelected,
    UnlessPlaying,
    Always
}

Enabling / Disabling Controls

You can use the TouchManager.ControlsEnabled property to enable and disable all touch controls as a whole. When controls are disabled, they will be hidden and will not provide any input events.

Additionally, touch controls can be enabled or disabled individually by setting the enabled property of each control component.

Touch controls may be enabled and disabled at run time.

Button Controls

You can add a button touch control to the scene via the GameObject > InControl > Touch > Button Control menu command, or with the &ldquo;Create Button Control&rdquo; shortcut button on the TouchManager component.

TouchButtonControl inspector

Virtual Stick Controls

You can add a button touch control to the scene via the GameObject > InControl > Touch > Stick Control menu command, or with the &ldquo;Create Stick Control&rdquo; shortcut button on the TouchManager component.

TouchStickControl inspector

Directional Swipe Controls

You can add a swipe touch control to the scene via the GameObject > InControl > Touch > Swipe Control menu command, or with the &ldquo;Create Swipe Control&rdquo; shortcut button on the TouchManager component.

TouchSwipeControl inspector

Track Pad Control

You can add a track pad touch control to the scene via the GameObject > InControl > Touch > Track Control menu command, or with the &ldquo;Create Track Control&rdquo; shortcut button on the TouchManager component.

TouchTrackControl inspector

Modifying Touch Controls At Runtime

All properties of touch control components can be modified at runtime, including their sprites, sizes and positioning, whether to use percent or pixel units.