Image selector

The ImageSelector script will use GetMouseButtonDown and a Graphic Raycaster to determine if the user has selected one of the Preview GameObjects. If these two events are simultaneously true, then the image associated with the Preview GameObject will be assigned to the FullImage GameObject. In the following steps, we'll create the ImageSelector script, which will then be applied to the Preview GameObjects:

  1. Double-click the ImageSelector script in the Project/Scripts directory to open it in the default editor.
  2. The first step is to let Unity know what components are going to be used in the script. Since we'll be checking to see if the Raycaster collides with a UI element, we'll need the UnityEngine.UI and UnityEngine.EventSystems components. Likewise, line 8 calls up the Graphic Raycaster, which is how we'll be interacting with the Preview thumbnails.
  3. Modify the Start() and Update() methods, as indicated in the following script:
 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

[RequireComponent(typeof(GraphicRaycaster))]

public class ImageSelector : MonoBehaviour { // Use this for initialization void Start () {
}

// Update is called once per frame
void Update () {

} }
  1. We'll need the following public variables to present unique content for each gallery image. Add the following public variables before the Start() method:
public string categoryTitleName; 
public Text categoryTitle; 
public Image fullImage; 
public Material hilightMaterial;

private void Start () {

}

Next, we'll modify the Start() and Update() functions. These blocks of code will control what happens when the GameObject appears in the scene and how it should respond while the scene is running.

  1. Modify the Start() and Update() methods, as indicated in the following script:
private void Start () { 
          categoryTitle.text = categoryTitleName; 
}

private void Update () { if (Input.GetMouseButtonDown(0)) { OnPointerDown ();
} }
The OnPointerDown function will appear read, because it has not been defined.

In the Start() method, the categoryTitle.text code is used to replace the Category Title default text with the value attached to the GameObject having this script. We will see this in action once the script has been completed and the values have been set for the Preview GameObjects.

The if statement in the Update method checks to see if the user has pressed a button on a controller or touch device. In our case, GetMouseButtonDown(0) refers to the Gear VR's trackpad button and, if it has been pressed, the OnPointerDown() function will be called.

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

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