Skip to content
Shawn Featherly edited this page Sep 29, 2016 · 4 revisions

Basic touch detection

To use TouchScript your scene needs one instance of the component TouchManager.cs. You can add this by adding TouchManager.cs to any gameobject, adding a TouchScript Layer component to any gameobject, or access TouchManager.Instance in one of your scripts. Accessing TouchMAnager.Instance will create a TouchScript root level gameobject at runtime.

When using C#, a script should subscribe to TouchScript events during the OnEnable() phase of a Unity scene. It should unsubscribe during OnDisable(), to prevent memory leaks.

private void OnEnable()
{
    if (TouchManager.Instance != null)
    {
        TouchManager.Instance.TouchesBegan += touchesBeganHandler;
    }
}

private void OnDisable()
{
    if (TouchManager.Instance != null)
    {
        TouchManager.Instance.TouchesBegan -= touchesBeganHandler;
    }
}

Handlers you assign to touch events is where you can extend TouchScript to do the touch interaction that you'd like. Here's an example that implements the touchesBeganHandler above.

private void touchesBeganHandler(object sender, TouchEventArgs e)
{
    foreach (var point in e.Touches)
    {
        Debug.Log(point.Tags + " touched down at " + point.Position);
    }
}

Basic gesture detection

Gestures utilize the path one or more touches make as a group. The difference when using gestures compared to basic touch is that your gameobjects will need one of the TouchScript gesture components. Your script will leverage the events from that gesture component instead of TouchManager.Instance. Here's an example script using the ScreenTransformGesture.cs TouchScript gesture. The ScreenTransformGesture.cs could be placed on the same gameobject as this script.

public ScreenTransformGesture TestGesture;

private void OnEnable()
{
    TestGesture.Transformed += TestGesture_Transformed;
}

private void OnDisable()
{
    TestGesture.Transformed -= TestGesture_Transformed;
}

private void TestGesture_Transformed(object sender, System.EventArgs e)
{
    Debug.Log("TestGesture_Transformed" + TestGesture.DeltaPosition);
}

Notice how the TestGesture_Transformed Handler should use the Gesture component variable instead of EventArgs.

Getting uGUI elements to be interactable

Make uGUI elements excluded from TouchScript

  1. Remove the StandardInputModule from the EventSystem gameobject that's automatically created when you add a uGUI gameobject.
  2. Add the TouchScriptInputModule to that EventSystem gameobject.

At this point your uGUI should be interactable. You'll notice when you try to interact with your uGUI TouchScript may falsely trigger. Add a UILayer.cs as follows to prevent this: 3. Add a single UILayer.cs component to any gameobject. The UILayer component makes TouchScript aware that gestures need to be handled differently on RectTransforms. 3. If you don't yet have another TouchScript layer, add another TouchScript Layer. Any type will work, FullScreen Layer or Camera Layer. 4. A single TouchManager component must be in the scene somewhere. Having it allows you to modify the order of the TouchLayers. 5. In the TouchManager component, ensure that the UI Layer is at the top of the TouchLayers in its reorderable list.

Make specific uGUI elements interactable

Using UIGesture you can have a mix of TouchScript on your uGUI elements.

  1. Add a UIGesture.cs to every interactable uGUI element. For a button, this should be on the gameobject that contains the button component.
  2. Add a single UILayer.cs component to any gameobject. UILayer component makes TouchScript aware that gestures need to be handled differently on RectTransforms.
  3. If you don't yet have another TouchScript layer, add another TouchScript Layer. Any type will work, FullScreen Layer or Camera Layer.
  4. A single TouchManager component must be in the scene somewhere. Having it allows you to modify the order of the TouchLayers.
  5. In the TouchManager component, ensure that the UI Layer is at the top of the TouchLayers in its reorderable list.

UIGesture Limitations

  • TouchScript's UIGesture doesn't yet enable working with uGUI that uses any drag motion, such as sliders or scroll rects. workarounds

Types of TouchScript layers

FullScreen

Used to get touch interaction to effect an object even if you're not directly touching it, such as rotating a model by dragging below it.

UILayer

Enables uGUI buttons to be interactable. Refer to Getting uGUI elements to be interactable for more information.

Clone this wiki locally