Scene controller

As was originally stated, this project lets a user select images from multiple image galleries. So far, we've learned how to create the virtual environment, created canvases to hold gallery images, and created a script for facilitating the user's ability to select images from preview thumbnails. Next, we need a tool for navigating through multiple galleries. This SceneController script will read the user's swiping direction to determine which gallery to display next. Instead of going into deep details on every line, we'll focus on the functions and commands which are related to the Gear VR's trackpad and moving in VR space:

  1. Double-click the SceneController script in the Project/Scripts directory to open the base script in your default editor.
  1. Add variables and revise the Start() method as shown here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SceneController : MonoBehaviour {
public gameObject galleryHolder; public float slideSpeed; private OVRTouchpad.TouchArgs touchArgs; private OVRTouchpad.TouchEvent touchEvent; private bool isMoving;
void Start () { OVRTouchpad.Create (); OVRTouchpad.TouchHandler += SwipeHandler; }

The SceneController script will require a mix of five public and private variables. The first variable, galleryHolder, identifies the GameObject that holds our galleries. In our project, the aptly named Gallery Holder serves this purpose. This script will use the user's swipe motion to cycle through the individual galleries. The variables slideSpeed and isMoving take care of the swipe velocity and prevent the galleries from moving too far too quickly.

The last variables to address are associated with the OVRTouchpad which is an interface to the touchpad class. These variables, touchArgs and touchEvent, help determine the type of touch created when the user interacts with the trackpad.

  1. Modify the Update() loop to match the following script:
void Update () {
#if UNITY_EDITOR
if (!isMoving) {
if (Input.GetKeyDown (KeyCode.RightArrow)) {
StartCoroutine (SwipeRight (galleryHolder.transform.position.x));
} else if (Input.GetKeyDown (KeyCode.LeftArrow)) {
StartCoroutine (SwipeLeft (galleryHolder.transform.position.x));
}
}
#endif
}

The update loop runs about 60 times per second while the scene is playing. On each loop, we need first to determine if the trackpad has been touched, and then perform an action based on that interaction. While testing the application in Unity, we don't have access to the Gear's trackpad. Luckily, Unity provides a Platform Dependent Compilation (PDC) feature which will help us with this problem.

The code between the #if UNITY_EDITOR and #endif statements will only run within the Unity Editor and will not be complied within our final application build. This lets us simulate a user device swipe by pressing the left or right arrow while still in Unity. Without this work-around, we would have to build and install the application on the Gear VR device to see the intended results.

Learn more about the PDC in the Unity documentation. https://docs.unity3d.com/Manual/PlatformDependentCompilation.html.

The code between #if and #endif checks to see if the left or right arrow has been clicked and, if so, we call the SwipeRight or SwipeLeft functions which we'll design next. However, this only happens if Gallery Holder is not moving, as indicated by the if (!isMoving) statement. This condition is needed to prevent a double-swipe on the GameObject.

  1. Add the SwipeHandler() function after the Update() method:
void SwipeHandler (object sender, System.EventArgs e) {
touchArgs = (OVRTouchpad.TouchArgs) e;
touchEvent = touchArgs.TouchType;

if (!isMoving) {
if (touchEvent == OVRTouchpad.TouchEvent.Left) {
StartCoroutine (SwipeLeft
(galleryHolder.transform.position.x));
}
else if (touchEvent == OVRTouchpad.TouchEvent.Right) {
StartCoroutine (SwipeRight
(galleryHolder.transform.position.x));
}
}
}
  1. Complete the SceneController script by adding the SwipeRight and SwipeLeft functions after SwipeHandler():
private IEnumerator SwipeRight (float startingXPos) {
while (galleryHolder.transform.position.x != startingXPos - 4) {
isMoving = true;
galleryHolder.transform.position =
Vector3.MoveTowards (galleryHolder.transform.position, new
Vector3 (startingXPos - 4, galleryHolder.transform.position.y,
0f), slideSpeed * Time.deltaTime);
yield return null;
}
isMoving = false;
}

private IEnumerator SwipeLeft (float startingXPos) {
while (galleryHolder.transform.position.x != startingXPos + 4) {
isMoving = true;
galleryHolder.transform.position = Vector3.MoveTowards
(galleryHolder.transform.position,
new Vector3 (startingXPos + 4,
galleryHolder.transform.position.y, 0f),
slideSpeed * Time.deltaTime);
yield return null;
}
isMoving = false;
}

The SwipeHandler() function mimics the #if UNITY_EDITOR by accepting inputs when the Gallery Holder is not moving. Then the SwipeRight() and SwipeLeft() functions move the Gallery Holder GameObject four units left or right from its starting position at a speed we'll set after applying the script.

  1. Save the script.
  2. Return to Unity and add the script to the Controller GameObject.
  3. With the Controller GameObject selected, drag the GalleryHolder GameObject from the Hierarchy window to the GalleryHolder field of the Scene Controller (Script) component.
  1. Set the Slide Speed to 5.
    This will be a default value. Feel free to change the value after the script has been properly tested.
  2. Save the script and the project before proceeding.

With the scripts in place, our project is almost complete. The next steps are to add and apply imagery to the UI Image elements, link GameObject scripts to the correct targets, and build the Gear VR application.

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

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