Adding the logic of the app

Now, let's create the logic of the app by adding a new script to the project. This will be the script in charge of switching between one and multiple chairs:

  1. In your @MyAssets|Scripts folder, in the Project window, right-click and select Create|C# Script. Call it OnTheGoHandler.cs:

Adding a new script in the Project window
  1. Double-click on it to open it in Visual Studio:

The OnTheGoHandler script in Visual Studio
  1. Add the Vuforia library at the top of the script to use its features:

using Vuforia;
  1. Inside the class, declare the following variables:
public GameObject chairButton;
public Sprite[] buttonSprites;
private ContentPositioningBehaviour contentPosBehaviour;
private bool multipleChairs = false;

The chairButton variable will correspond to the button we created in Unity. The buttonSprites array will contain the two background images for the button and we'll switch between them. The contentPosBehaviour variable comes from the Vuforia class, which is in charge of adding our virtual objects to the real world. We will use the multipleChairs Boolean to switch between the two states (one chair or multiple chairs). Public variables will be initialized in the Unity editor.

  1. Inside the Start() method, add the following code:
contentPosBehaviour = GetComponent<ContentPositioningBehaviour>(); 

This will retrieve the ContentPositioningBehaviour component of the GameObject that contains the script.

  1. After the Update() method, create a new method:

public void SwitchMultipleChairs()
{
if (!multipleChairs)
{
chairButton.GetComponent<UnityEngine.UI.Image>().sprite = buttonSprites[1];
contentPosBehaviour.DuplicateStage = true;
multipleChairs = true;
}
else
{
chairButton.GetComponent<UnityEngine.UI.Image>().sprite = buttonSprites[0];
contentPosBehaviour.DuplicateStage = false;
multipleChairs = false;
}
}

When this method is called, it checks whether the current state is a single chair or multiple chairs. It switches the image of the UI button accordingly and adjusts the DuplicateStage parameter of the ContentPositioningBehaviour class to allow one or multiple instances of the same chair. Then, it sets multipleChairs to true or false to keep track of the current state.

  1. Back in the Unity editor, drag the script over the Plane Finder. Alternatively, in the Inspector window, click on Add Component|On The Go Handler:

Adding the created script to the Plane Finder GameObject
  1. In the On The Go Handler script, select or drag the button to the Chair Button field and select the two sprites, chair and cinema, from @MyAssets|Images in that order (see the following screenshot).
  2. From the Content Positioning Behaviour drop-down, uncheck Duplicate Stage so that it starts with a single chair:

 Assigning the corresponding elements to the OnTheGoHandler script in the Inspector window
  1. Finally, select the button and, in the On Click () method, at the bottom, choose the Plane Finder and select the On The Go Handler|SwitchMultipleChairs function:
Selecting the OnClick() method behaviour for the button in the Inspector window
  1. Before we add any new features, we need to build this app on a mobile device. Press Ctrl + Shift + B or go to File|Build Settings...:

Build Settings panel to build and run the app on a mobile device
  1. Press Build And Run, give your .apk file a name, and run it on the mobile device. Point the camera to the ground until a little icon appears, marking the ground level:

The white square shows the point of the ground where the virtual objects will be placed
  1. Move around and tap on the screen to place a chair. Now, you can walk around the virtual chair to see it from different angles:

The virtual red chair placed in a real environment
  1. Press the button to switch to multiple-chair mode, move the camera around, and place multiple chairs all over the room:

Two virtual chairs, one next to the other

It's probable that, on your first attempt, the chair won't have the desired size or the shadows won't correspond to the real ones. Furthermore, the chair will always be facing forward, so you'll have to move around the room before placing it in the desired place. Let's improve these little details to make the app more appealing.

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

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