Adding the scale button and script

First, let's add the ScaleButton prefab to the toolbar:

  1. From Assets/SimpleIcons/Prefabs/ drag the ScaleButton prefab into Hierarchy as a child of Toolbar.
  2. Set its Transform Position X to −0.15.

If you are not using the SimpleIcons package we provided, you can just add a cube with the scale (0.1, 0.1, 0.04), and name it ScaleButton.

  1. Now let's write the ScaleTool.cs script for the button. In your Scripts folder, create a new C# script, name it ScaleTool, and open it for editing.

First, we will capture an initial pinch-gesture, much like the OnInputClicked we used before, to begin editing. We will also define the completed or cancelled events for when we are done editing.

    1. Start to write the script as follows:
File: ScaleTool.cs
using UnityEngine; using UnityEngine.VR.WSA.Input; public class ScaleTool : MonoBehaviour { private PictureController picture; private Vector3 originaButtonScale; private GestureRecognizer scaleRecognizer; private bool isEditing; void Start() { picture = GetComponentInParent<PictureController>(); originaButtonScale = transform.localScale; scaleRecognizer = new GestureRecognizer(); scaleRecognizer.SetRecognizableGestures(GestureSettings.ManipulationTranslate); scaleRecognizer.ManipulationStartedEvent += OnStartedEvent; scaleRecognizer.ManipulationUpdatedEvent += OnUpdatedEvent; scaleRecognizer.ManipulationCompletedEvent += OnCompletedEvent; scaleRecognizer.ManipulationCanceledEvent += OnCanceledEvent; scaleRecognizer.StartCapturingGestures(); isEditing = false; }
    1. The following helper functions are called when the user begins and ends the editing mode:
    private void BeginEdit() { 
        transform.localScale = originaButtonScale * 2.5f; 
        isEditing = true; 
    } 
 
    private void DoneEdit() { 
        transform.localScale = originaButtonScale; 
        isEditing = false; 
    } 
 
    1. Remove the event handlers (opposite of Start()) as a matter of housekeeping when this object is destroyed:
 
    private void OnDestroy() { 
        scaleRecognizer.StopCapturingGestures(); 
        scaleRecognizer.ManipulationStartedEvent -= OnStartedEvent; 
        scaleRecognizer.ManipulationUpdatedEvent -= OnUpdatedEvent; 
        scaleRecognizer.ManipulationCompletedEvent -= OnCompletedEvent; 
        scaleRecognizer.ManipulationCanceledEvent -= OnCanceledEvent; 
    }
    1. Define the event handlers for start, update, complete, and cancel:
 
    private void OnStartedEvent(InteractionSourceKind source, Vector3 position, Ray ray) { 
Camera.main.transform.forward); 
        if (!isEditing) { 
            RaycastHit hitInfo; 
            if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hitInfo)) {  
                if (hitInfo.collider.gameObject == gameObject) { 
                    BeginEdit(); 
                } 
            } 
        } 
    } 
 
    private void OnUpdatedEvent(InteractionSourceKind source, Vector3 position, Ray ray) { 
    } 
 
    private void OnCompletedEvent(InteractionSourceKind source, Vector3 position, Ray ray) { 
        if (isEditing) { 
            DoneEdit(); 
        } 
    } 
 
    private void OnCanceledEvent(InteractionSourceKind source, Vector3 position, Ray ray) { 
        if (isEditing) { 
            DoneEdit(); 
        } 
    } 
} 

In this script, we're using the UnityEngine.VR.WSA.Input library. Like the Move tool, we keep a variable for the PictureController and original button scale, and initialize those in Start().

We also have a GestureRecognizer that is initialized in Start(), registering our handler functions. Then we tell it to start capturing gestures.

When a pinch gesture is detected, OnStartedEvent is called. It checks if the click event pertains to us-that is, we use the camera gaze vector to determine if the user is looking at this object when the event was detected. If so, we will call BeginEdit().

BeginEdit() resizes the Scale button object. We've also written a corresponding DoneEdit() and OnDestroy(), which reverses and cleans up the initializations. The isEditing flag tracks whether this component has been started, since we don't want to track Manipulation events started elsewhere.

Press Play and select the Scale button with a pinch gesture. It will enlarge. Release the gesture and it will go back to normal size. Pinch again and move your hand outside of the device's view-it will again go back to normal size, this time because the OnCanceledEvent was detected.

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

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