Setting touch control

Accepting user input will be done as part of a two-step process when the Gear VR's trackpad button is pressed. First, we need to determine if the user clicked on a selectable GameObject: if this is TRUE, we then need to capture which GameObject was selected and, finally we need to switch the FullImage with the image of the current GameObject. We'll address identifying and capturing the GameObject (our Preview images) within the OnPointerDown function.

As mentioned at the start of this chapter, we will use a Graphic Raycaster to project a ray into the scene. However, instead of doing this all the time, we only need to do this when the trackpad button has been pressed. We can then determine if the ray collides with a UI GameObject and, in the event that it does collide, we can store the required GameObject attributes. Use the following link for a detailed explanation of the PointerEventData in Unity's documentation. It contains a complete list of attributes captured by this data type: https://docs.unity3d.com/ScriptReference/EventSystems.PointerEventData.html:

  1. Add the following code after the Update() method:
public void OnPointerDown () {
GraphicRaycaster gr = GetComponent<GraphicRaycaster> ();
PointerEventData data = new PointerEventData (null);
data.position = Input.mousePosition;

List<RaycastResult> results =new List<RaycastResult> ();
gr.Raycast (data, results);

if (results.Count > 0) {
OnPreviewClick (results [0].gameObject);
}
}
The OnPreviewClick function will appear read because it has not been defined.

When the Gear VR's trackpad button is pressed, we create a GraphicRaycaster called gr, a variable called data to store the current mousePosition and an empty list for the collided GameObjects called results. So, when the button is pressed a ray, gr, is created which starts at the camera's location and points through the click location. All game objects which collide with the ray are then stored in the result list. For our gallery, we only need the topmost GameObject, which will have an index of 0. We then pass this object along to a new function which will handle switching the Preview image with the FullImage image.

  1. Add the following code after the OnPointerDown() method:
void OnPreviewClick (gameObject thisButton) { 
     Image previewImage = thisButton.GetComponent<Image> (); 
     if (previewImage != null) { 
     fullImage.sprite = previewImage.sprite; 
     fullImage.type = Image.Type.Simple; 
     fullImage.preserveAspect = true;
} }

The OnPointerDown() method determines which Preview thumbnail was being selected and it does so by casting a ray from the main camera to the mouseclick location. Now that we know which Preview was chosen, we'll create the OnPreviewClick (another custom function) which will set the FullImage sprite to be the same as the selected Preview sprite.

The function works by passing along a GameObject when called from OnPointerDown. In OnPreviewClick() the GameObject is stored locally as the variable thisButton and its image component is copied to the variable previewImage.

Now that we have the image to swap, we check to make sure the image is not empty by testing for a null value with an if statement. As long as the value is not null, we can set fullImage.sprite to the same image as previewImage.sprite. The next two commands are used to make sure the image displays properly on the new GameObject.

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

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