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:
- In your @MyAssets|Scripts folder, in the Project window, right-click and select Create|C# Script. Call it OnTheGoHandler.cs:
- Double-click on it to open it in Visual Studio:
-
Add the Vuforia library at the top of the script to use its features:
using Vuforia;
- 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.
- Inside the Start() method, add the following code:
contentPosBehaviour = GetComponent<ContentPositioningBehaviour>();
This will retrieve the ContentPositioningBehaviour component of the GameObject that contains the script.
-
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.
- 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:
- 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).
- From the Content Positioning Behaviour drop-down, uncheck Duplicate Stage so that it starts with a single chair:
- 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:
- 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...:
- 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:
- 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:
- Press the button to switch to multiple-chair mode, move the camera around, and place multiple chairs all over the room:
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.