Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / game / Unity

Day 35 of 100 Days of VR: How to Run Google Cardboard on an Android Device in Unity

5.00/5 (6 votes)
7 Nov 2017CPOL6 min read 5.2K  
How to run Google Cardboard on an Android Device in Unity

Yesterday, we looked at how we can work with VR and went through the basic demo and understood how everything worked.

Today, we’re going to look at how we can install our game directly into the phone.

To do everything today, we need to have:

  1. A phone that supports Android Level 19 (Kit Kat)
  2. A USB to Micro-USB (or Type-C for newer devices) cable
  3. (Optional) Google Cardboard

Today, we’re going to:

  1. Install the Android SDK so we can build and run our app
  2. Install a USB driver for our computer to detect our phone
  3. Set up our phone to be in developer mode
  4. Build and run the demo app into our phone

With all that being said, let’s get started! Today, we’ll be following Unity’s Android SDK setup guide.

Step 1: Install the Necessary Android Software

Since we’re building our VR app for Android applications, we need the required software to compile, build, and run our app on our phone.

  1. Download and install the latest Java SDK to run Android Studio
  2. Download and Install Android Studio
  3. You might have to restart your computer first for your computer to recognize the new Java SDK that you installed.

When we’re done downloading and installing Android Studio (which will take a LONG time), we want to open the SDK Manager.

Image 1

In our new project, we can find our SDK Manager under Configure.

Now we’ll get this:

Image 2

Under SDK Platform, select the platform we want to support, in this case, it’s Android 4.4 for Cardboard and Android 7.0 for DayDream, however, I believe if you install the newest version, that’ll work for both.

Under SDK Tools, install:

  • Android SDK Platform-Tools
  • Android SDK Tools
  • Google USB Driver if you have a Nexus device

With all of this, we should now have everything we need to be able to build our game into our Android device.

Step 2: Install a USB Driver to Detect our Phone

The next part (and probably the part I hate the most) is installing a USB driver that allows our computer to detect our phone.

  1. Go to Google’s documentation on where to find the appropriate OEM USB driver for your phone and install it.

With any luck, your computer should be able to successfully recognize your phone when you plug it into your computer.

If not, then I refer you to Google this problem as there are too many possibilities of what could have gone wrong.

Step 3: Change Your Phone to Developer Mode

Now our computer can connect to our mobile device, the final thing we need to do is have our phone be in developer mode so Unity (or Android) can create the app and install it on our phone.

The instructions to enable Developer Mode varies depending on what your phone is. A quick Google search should give you what you need to enable it.

However, the most common approach these days is to:

  1. Go to Settings > About phone > Build Number
  2. Click build number 7 times to enable Developer Mode

Now under Settings, you should find Developer options.

  1. Go into Settings > Developer options and turn on USB Debugging

Hopefully, with this step completed, we can finally move on to our configurations in Unity!

Step 4: Configuring Unity to Build and Run our Android App

Now that our phone is ready, it’s time to finally build our game into Unity.

  1. Make sure that your phone is connected to your computer
  2. In Unity, go to File > Build & Run to create an APK file (our app) that will install it on our computer

That’s it. Now in the perfect world, that’s it, we’re done. Enjoy our VR game!

Unfortunately, there are always problems that we would encounter:

  1. Your API is at the wrong level
  2. You’re missing a Bundle Identifier
  3. Failed to compile resources with the following parameters: major version 52 is newer than 51, the highest major version supported by this compiler.

The 1st and 2nd problem can be resolved easily.

The first problem is because we need to make sure that we create a minimum version of Android devices that have the software we need to run our VR application.

  1. In Player Settings under Other Settings… in Minimum API Level select API Level 19 for Google Cardboard support and API Level 24 for Google Daydream. If you choose API Level 24, just make sure that your phone can run Daydream!

For the second problem, every Android app has a unique identifier that Google uses to identify the app. The error that we’re getting is that Unity is telling us that we’re using the default one and we should change it.

  1. In Player Settings under Other Settings… in Package Name change the string to be something else. Just make sure you follow the convention of <companyname>.<appname>. In our case, it doesn’t matter what it is, we can put anything we want.

Now for the third and final problem. This one is more interesting. Most likely, your error is something like this:

Failed to compile resources with the following parameters:

-bootclasspath "C:/Users/JoshDesktop/AppData/Local/Android/android-sdk\platforms\android-24\android.jar" 
-d "C:\Users\JoshDesktop\git\Cardboard\Temp\StagingArea\bin\classes" -source 1.6 -target 1.6 
-encoding UTF-8 "com\google\android\exoplayer\R.java" "com\google\gvr\exoplayersupport\R.java" 
"com\google\gvr\keyboardsupport\R.java" "com\google\gvr\permissionsupport\R.java" 
"com\google\vr\cardboard\R.java" "com\google\vr\keyboard\R.java" "com\Josh\Chang\R.java" 
"com\unity3d\unitygvr\R.java"

warning: C:\Users\JoshDesktop\AppData\Local\Android\android-sdk\platforms\android-24\android.jar
(java/lang/Object.class): major version 52 is newer than 51, the highest major version 
supported by this compiler.

It is recommended that the compiler be upgraded.

warning: C:\Users\JoshDesktop\AppData\Local\Android\android-sdk\platforms\android-24\android.jar
(java/lang/AutoCloseable.class): major version 52 is newer than 51, the highest major version 
supported by this compiler.

What all of this is saying is that our Java is out of date and we need to have at least Java SDK 8.52.

In my case, I previously had 8.51 installed and when I installed version 8.52, Unity didn’t pick up on the changes.

To fix this:

  1. Go to Edit > Preferences > External Tools under Android, select JDK and choose the path to your newest JDK file. For me, on my window machine, it was located at C:\Program Files\Java\jdk1.8.0_152.

With all of this done, hopefully, you should be able to successfully build and run the GvrDemo on your phone + Google Cardboard if you have one.

Conclusion

Hopefully, this was a useful guide to getting your Android device set up to play the scene. Leave a comment if you run into problems and I’ll try to help and update this article with any new information.

On a different note, it’s truly amazing playing with VR on our own mobile device. Just playing the VR game from Unity was interesting, but words can’t describe how much more realistic and interesting it becomes until you strap your phone onto your face!

I think at this point, we have a good understanding of the basics and what is and isn’t possible with the Google Cardboard now.

Tomorrow we’re going to look and see how we can incorporate the VR SDK into our simple FPS game to see how our game fares in VR!

Day 34 | 100 Days of VR | Day 36

Home

The post Day 35 of 100 Days of VR: How to Run Google Cardboard on an Android Device in Unity appeared first on Coding Chronicles.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)