Use touch events instead of hand gestures

The only effort in adapting the project for Vuforia is, instead of the HoloLens InputManager and GestureRecogizer, we are going to use standard Unity mouse events (which already map mobile screen touches). We wrote this code for the preceding ARKit version and can re-use it here.

  1. For PictureAction.cs, use exactly the same script we showed for ARKit.
  2. For ClickableObjects.cs, use exactly the same script we showed for ARKit.
  3. For ScaleTool.cs, use exactly the same script we showed for ARKit.

For the MoveTool, we must adapt it specifically for Vuforia.

To move our picture along the same plane as the image target, we need to define a large box collider for ImageTarget so that input events can be detected on it (in HoloLens, the spatial mesh served a similar purpose). We will put the target on a new Wall layer so the Move tool does not interfere with other UI inputs:

  1. In Unity, select the Layers at the top-right of the widow and choose Edit Layers.
  2. Then unfold the Layers list and add one named Wall, as shown here:

Now, put the ImageTarget on the Wall, but not any of its children.

  1. In Hierarchy, select ImageTarget.
  2. In Inspector, set its Layer to Wall.
  3. When asked Do you want to set layer to Wall for all child objects as well?, answer NO, this object only.
  4. Add a Box Collider component to ImageTarget.
  5. Set its Size to (10, 0, 10) making a big plane on X-Z for the mouse events.

Open the MoveTool.cs script and modify it to use mouse events and remove all references to SpatialMapping, as follows:

File: MoveTool.cs
using UnityEngine; public class MoveTool : MonoBehaviour { public LayerMask WallLayerMask; ... void Start() { ... relativeOffset = transform.position - picture.transform.position; relativeOffset.y = 0f; } void Update() { if (isEditing) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, Mathf.Infinity, WallLayerMask)) { Debug.DrawLine(ray.origin, hit.point); picture.transform.position = hit.point - relativeOffset; } } if (!Input.GetMouseButton(0)) { DoneEdit(); } } private void OnMouseDown() { if (!isEditing) { BeginEdit(); } } private void OnMouseUp() { if (isEditing) { DoneEdit(); } }
  1. Save the script. In Unity, select the MoveButton under the toolbar, and in Inspector, set its Move Tool Wall Layer Mask to Wall.

Now, when you press Play, the Move tool will work, dragging the picture with your touch until you let up.

The following is a photo of the wonderful photo-wall adorning my office, built with the mobile AR version of our app:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset