As virtual reality (VR) gains momentum, you may decide that you’d like to try your hand at making a virtual reality app. It’s actually easier than you’d think. Using Unity and Google Cardboard, you can have a very basic VR scene in no time at all.
Setting Up Unity and the GoogleVR SDK
First, you’ll want to download Unity if you don’t have it yet. The personal version is free, and you can get it here.
Once you’ve got it installed, open it up to create your first project. Create a title and set your location for your new project. Unity also allows you to create 2D apps, but by default, 3D is selected. Click “Create project.”
When Unity finally loads, you’ll see the default setup with a basic scene. The scene will only have a directional light and a main camera.
Next, we’ll need to download the GoogleVR SDK. (You can get it here from Google.) Next, you’ll import it into your Unity project. You can do this in a couple of ways. As one option, you can drag it from the folder and over the “Assets” section in the Project window.
Alternatively, you could go to Assets > Import Package > Custom Package. Locate the GoogleVRForUnity package file and open it. Unity will work on preparing the package for importing.
Then, you’ll see a pop-up to import the package. It lists everything that will be imported with the SDK. Ensure all the boxes are checked by clicking on “All.” Then, click on “Import” to import everything. It should take a minute or so, but when it’s done, you should see everything that was imported show up in your assets folder within Unity.
Sometimes you may see a message saying that an API update is required. Since this is a new project and there’s nothing in danger of breaking, go ahead and click “I made a backup.” In the future, you’ll want to be sure you’ve made a backup before updating anything, as future versions can clash with older features.
Save your empty scene to give it a name. Next, let’s set up our build settings. Go to File > Build Settings.
Click on “Add Open Scenes” to ensure your scene gets added to the build. Under Platform, select Android, and then click “Switch Platform.” It could take a minute to import any assets needed for Android. Once that’s done, click on “Player Settings.” This may not appear to do much, but in the inspector tab, you’ll find the player settings configurations.
Set your company name and product name at the top if they’re different than your project name. Ensure that you are looking at the Android tab. Enable “Virtual Reality Supported” by checking the box. Unity has some built-in virtual reality SDKs—You can add and remove them here. In Unity version 5.6 (which is in beta), you can click on the plus sign and add Cardboard (or another) here.
In the Identification section, set your bundle identifier, version number, bundle version code, and minimum API level. The minimum API level should be set to 19 for Cardboard.
Click “Build” from the player settings and choose a name to save your APK file. When that’s finished, close the player settings window.
Fix an Issue with GoogleVR
Note: We’ll need to fix a compile error in the GoogleVR scripts before we can run and build our project. In your project folder, locate the GvrVideoPlayerTexture.cs script. (GoogleVR > Scripts > Video > GvrVideoPlayerTexture.cs.) Right-click to open the file. Unity will open it in Monodevelop by default.
Private IEnumerator CallPluginAtEndofFrames() { If (processingRunning) { Debug.LogError(“CallPluginAtEndOfFrames invoked while already running. “ ); Debug.LogError(StackTraceUtility.ExtraStackTrace()); Yield return false; }
Change line 595 to “yield return false;” in order to fix the compile error. You may end up seeing a prompt that says Unity needs to re-import the package. Accept the re-import to allow for backwards compatibility.
Setting Up Your VR Scene
- Drag the GvrViewerMain into your scene. The GvrViewerMain is located in GoogleVR > Prefabs. Click and drag it into the empty space on the bottom of the scene hierarchy. This will adapt the Main Camera to VR mode.
- Next, we need to add a couple of things to the Main Camera. Add GvrReticlePointer to the main camera by dragging it from GoogleVR > Prefabs > UI onto the Main Camera element in the scene hierarchy.
- Add a physics raycaster to the main camera. Select Main Camera from the hierarchy. Click on “Add Component” from the Inspector window. Type in Physics Raycaster and select it to add it to the camera. Be sure to select Physics Raycaster and not Physics 2D Raycaster.
- Next, we’ll need to add an Event Manager to our scene. Right-click on the hierarchy’s empty space. Click on UI > Event System. In the Inspector window of the Event Manager, click on “Add Component” to add a GvrPointerInputModule. Click on the little gear in the top right corner of the GvrPointerInputModule component and click “Move up.” This will position the component above the Standalone Input Module and make your reticle work correctly.
- To test it out, save your scene and hit “Play” at the top in Unity. You should see your game window change to simulate a VR split view. Hold alt and move your mouse around to make the camera move. You should also see a little white dot in the center of your view. This is the reticle that will change to a circle when it hovers over something that is selectable. Hit “Play” again to stop the play mode.
Events and Selectable Objects
You have a very basic scene setup now, but we haven’t seen our reticle in action. Let’s ensure it’s working.
To make an object clickable, it needs two things—a collider and an event trigger.
- Add a cube or another 3D object to your scene. Right-click on the empty space in the hierarchy, or click on “Create” in the hierarchy. Click on 3D Object > Cube. This will add a 3D cube to your scene.
- Cubes have a box collider on them by default. Your object may have a sphere or capsule collider. Any of these colliders will work. If yours doesn’t have a collider, click on “Add Component” and search for “collider” to add one.
- Set up your object as an Event Trigger. Click on “Add Component.” Type in Event Trigger. Click “Add New Event Type” and add a Pointer Click event. You’d add your scripts here that would happen when clicking on the object.
- Click “Play” again. Now when your reticle is over the object, it should change from a small dot to an open circle.
Setting up a basic scene that’s Google Cardboard-ready is quick. From here, you’re able to go just about anywhere in VR. This scene can be used as a template for additional scenes in your VR experience.
I’ve added a simple setup on my GitHub page. (Note: I was using Unity 5.6 for that one, so mine may be a little different.)