Tuesday, April 16, 2013

Setting Up Unity To Remote With A Nexus 4: A Step By Step Guide

As much for my own future reference as the sanity of others, I have collated here all the necessary steps to get the Unity Remote working on the Nexus 4. It’s an otherwise simple process that’s beleaguered by a series of ill-explained hoops too jump through, which trawling the internet for the past hour or so finally has revealed to me. Perhaps most sites simply assume a prior knowledge of Android development. Well, I have none, and I’m not going to assume any in the steps below either.

This guide is for PC users and specific to my experience with Windows Vista and the Nexus 4. I expect the process would be virtually identical on other Android devices, though you may need a different driver. I do not own a Mac and have no idea how to go about setting this up on one, so don't ask :)

On Your PC: 
1. Download and install Unity (OK, maybe that’s a little too obvious):

2.  Download the Android SDK

3. Unzip the SDK to your preferred directory (there is no need to run an installer)

4. Ensure you are connected to the internet! (yes, something so simple left me very confused for quite some time...)

5. Open the newly unzipped folder and run SDK Manager.exe
If you are NOT connected to the internet you will only see files that are already installed. If you ARE connected the program will fetch a large list of additional files.

EDIT 13 Oct, 2013:
Having recently upgraded to a new 64bit Window 8 system, I have been having some trouble launching the SDK Manager. To fix this, follow these steps:

5.a. Download the Java JDK: http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

5.b. Go to Control Panel\All Control Panel Items\System (or rightlick on My Computer and go to Properties). Click Advanced system settings. Click on the Advanced tab, then on Environment Variables. Under System Variables, scroll down to Path and click Edit. Now, what you have to do is make sure that the java/bin directory is set before the windows\system32 directory. For instance, mine looks like this:
Files\Java\jdk1.7.0_40\bin;%SystemRoot%\system32;

5.c. Try running the SDK manager again, if it still doesn't work, try step 5.d. below:

5.d Back in the Android SDK Directory, go to sdk\tools and open android.bat in a text editor. Look for this piece of code:

set java_exe=
call lib\find_java.bat
if not defined java_exe goto :EOF

and change it to your java.exe directory. Mine looks like this:

set java_exe="D:\Program Files\Java\jdk1.7.0_40\bin\java.exe"

Hopefully the SDK Manager will now open.

6.  Under Extras, install Google USB Driver*

7. Open Unity, go to Edit > Preferences... > External Tools and point the Android SDK Location to the sdk folder you just unzipped (the sdk folder itself, not the root folder).

8. close Unity

On your Nexus 4:
9. Install the Unity remote app on your Nexus 4:

10. Unplug your Nexus from your PC.

11. Enable Developer Options. In 4.2.2 (the OS version that came installed with my Nexus) this is hidden by default. To unhide it go to Settings > About Phone, and scroll down to Build number. Tap this seven (7) times. A prompt will appear informing you of your actions after a few taps, and the message “You are now a developer” will show up when you’re done. Developer Options can now be access from the Settings root menu.

12. Go to Settings > Developer Options and check USB Debugging. Also consider checking Stay awake to prevent your device going to sleep while it’s plugged in (which may cause it to lose its connection to Unity)

13. Turn off USB tethering (located under Settings > More... > Tethering & portable hotspot > USB tethering)**

14. Plug your Nexus into your PC

Back on your PC:
14. With Developer Options enabled, Windows should now prompt you to install a new driver

15. If for some reason you have already let Windows install its own driver, you may need to remove it. Go to Device Manager and look for the Nexus 4 under Portable Device or (if it appears under Other Devices you have USB tethering turned on. Turn it off before continuing!). Right click on Nexus 4 and chose uninstall.

16. If you’re not prompted to install a new driver, right click on Nexus 4 again and this time chose Update Driver Software...

17. Click Browse my computer for driver software, and search for the driver in this location:
(your unzipped folder)\sdk\extras\google\usb_driver

18. If all goes well Windows should find and install the driver

Back to your Nexus 4:
19. Run the Unity Remote
Word on the interwebs is that you must launch the remote before launching Unity, as if you close the remote of the phone goes to sleep, you will need to relaunch Unity. However having just tested both these scenarios I did not encounter any issues, so perhaps this is out-of-date information. 

Back on your PC:
20. Launch Unity and open a scene.

21. Click Play and watch it running on your phone!

22. Bask in your success if it worked, or agonize over where you went wrong if it didn't!


Additional Note
When turning on USB tethering on my Nexus Windows seems to think it is a different device, which prevents Windows from locating the driver during setup. It also prevents Unity from connecting to it once it is set up. Solution is simple: turn off tethering.


--------------------

*This guide is for the Nexus 4. Other Android devices may require a different driver. This page has a list of URLs that may be useful:

** See Additional Note above.


3 comments:

  1. Ah, beautiful! You saved my day!

    ReplyDelete
  2. Thanks this worked. Make sure you have a USB cable, and not a charging cable only, also. I notice my USB cable fills both the holes on the bottom of my Samsung Galaxy, and the charging-only cable fills the right hole only.

    ReplyDelete