Here’s how to debug an Unity3D Android application on a real device (not in Unity3D editor) in Visual Studio. This recipe is written for Unity 2017.4 and Visual Studio 2017, maybe it works also in other versions.
Step 1] Connect device over WIFI
- Get the IP number of your Android phone over the WIFI network (no USB cable needed) with ‘adb shell ip addr show wlan0’:
$ adb shell ip addr show wlan0 24: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 3000 link/ether 2c:fd:a1:8c:dc:f1 brd ff:ff:ff:ff:ff:ff inet 192.168.25.221/22 brd 192.168.27.255 scope global wlan0 valid_lft forever preferred_lft forever inet6 fe80::2efd:a1ff:fe8c:dcf1/64 scope link valid_lft forever preferred_lft forever
Ip adress in this case is: 192.168.25.221
- Make a connection using adb
$ adb connect 192.168.25.221 connected to 192.168.25.221:5555
If connect fails take a look at https://stackoverflow.com/a/41422712/676402
- Make sure your Android phone is disconnected from USB
- Check connection with:
$ adb devices List of devices attached 192.168.25.221:5555 device
Step 2] Prepare debug version of Unity3D app
In Unity3D, go to Android build settings and check ‘Development Build’ and ‘Script Debugging’:
Now deploy and run your application (control-b). It’s important to run the application because otherwise the AndroidPlayer debugger will not show up in the next dialog.
Step 3] Set Visual Studio breakpoint and debug
Now in Visual Studio open a script and put a breakpoint.
Go to Debug -> Attach Unity debugger and a dialog with two debuggers should appear (one for Unity3D editor, one for Android device):
Select the ‘AndroidPlayer (USB)’ item and Visual Studio goes in debug mode. Note: This step can take a while if then Unity project is big.
Now run the application on the device and the breakpoint should be hit 🙂