How to log in a Unity android game?

10,248

Solution 1

Debug.Log on Android logs to the system log. Which you can view via $ adb logcat command.

Unity Reference: Log Files

Plug in your Android phone via USB cable. Open up Terminal and change folder to your Android SDK platform-tools folder, then run:

$ adb logcat

or, to filter only on Unity messages (if you are on mac/linux):

$ adb logcat | grep -e "Unity" 

Once you have the phone plugged into USB, you can also use adb to switch it over to Wifi.

But alternatively I also wrote a script for Android to route all Debug.Log over Wifi to a TCP listener. It's one script to add to your scene:

Unity3D Debug Logs over WiFi

Solution 2

You'll want to use the Unity Profiler. http://docs.unity3d.com/Manual/Profiler.html

You can connect over local wifi, or usb to debug an app on android, ios, or PC.

The unity docs at the link describe the steps.

But in case that page moves:

For WiFi profiling, follow these steps:

  • Make sure to disable Mobile Data on your Android device.
  • Connect your Android device to your WiFi network.
  • Attach your device to your Mac/PC via cable, check the “Development Build” and “Autoconnect Profiler” checkboxes in Unity’s build settings dialog, and hit “Build & Run” in Unity Editor.
  • When the app launches on the device, open the profiler window in Unity Editor (Window->Profiler)
  • If the Unity Editor fails to autoconnect to the device, select the appropriate device from the Profiler Window Active Profiler drop down menu. Note: The Android device and host computer (running the Unity Editor) must both be on the same subnet for the device detection to work.

For ADB profiling, follow these steps:

  • Attach your device to your Mac/PC via cable and make sure ADB recognizes the device (i.e. it shows in adb devices list).
  • Check the “Development Build” checkbox in Unity’s build settings dialog, and hit “Build & Run”.
  • When the app launches on the device, open the profiler window in Unity Editor (Window->Profiler)
  • Select the AndroidProfiler([email protected]:54999) from the Profiler Window Active Profiler drop down menu. Note: The Unity editor will automatically create an adb tunnel for your application when you press “Build & Run”. If you want to profile another application or you restart the adb server you have to setup this tunnel manually. To do this, open a Terminal window / CMD prompt and enter:

Solution 3

  • Menu "Window-Package Manager"
  • Install "Android Logcat"
  • Then menu "Window-Analasys-Android Logcat"

The device must be connected via USB. Development mode and USB debugging must be enabled on device

Share:
10,248
Anton Nel
Author by

Anton Nel

Updated on June 04, 2022

Comments

  • Anton Nel
    Anton Nel almost 2 years

    Sorry for the noob question, but how to I view logs when I have built the game as an .APK on an Android device? I know how to view Debug.Log (in the Unity console), but not on Android. If there is something else than Debug.Log, what is it? I just need to log some lines in my script while it is running on an Android device(NOT EDITOR) since I'm having issues with Google Play Leaderboard login.

    Thank you so much!

    • Fattie
      Fattie about 8 years
      I think it's a good question! But it's possible it will be closed on here as it is not about "code" as such.
  • Fattie
    Fattie about 8 years
    Plastic, you're sure the Profiler shows log messages? If so where, thanks for the tip.
  • Anton Nel
    Anton Nel about 8 years
    Sumbled on this after a quick Bing search: answers.unity3d.com/questions/32368/… so looks like it is worth a shot... will try tomorrow.
  • Fattie
    Fattie about 8 years
    Wow @peterept that looks crazy impressive script! One question, TBC you're saying the process you describe in the first six lines works on a Mac even? cheers
  • Anton Nel
    Anton Nel about 8 years
    So to use $ adb logcat I simply put that in a c# script in my app or what? I found the adb.exe in the android SDK tools
  • Plastic Sturgeon
    Plastic Sturgeon about 8 years
    @JoeBlow - You're right. No log messages. The answer from peterept above seems to answer the specific question better. Android studio and LogCat might also work.
  • Fattie
    Fattie about 8 years
    hey thanks for that, it's a great help. this answer is still very useful to googlers ... (perhaps you should just edit in a word that it doesn't specifically bring up the logs, in this version of Unity anyways). Now, about my nose job...
  • Fattie
    Fattie about 8 years
    Hi @AntonNel ... on your mac, click SpotlightSearch (loupe at extreme top right of physical Mac screen), type in Terminal, launch that. if you're not familiar with a shell, that's what that is (example, type ls or date. PeterEpt is suggesting commands to thus type in to Terminal (ie, the unix shell of your Mac ... it's really just a unix box with some stupid interface crap on top - that's why they work so well these days).
  • Anton Nel
    Anton Nel about 8 years
    And on Windows 10? I'm on Windows since I built my own PC, I'm sure there is a Terminal equivalent since Windows is supposed to have more software available than Mac. Thanks for the help!
  • Anton Nel
    Anton Nel about 8 years
    I tried to use $ adb logcat in the command prompt (cmd.exe) but it didn't recognize it
  • peterept
    peterept about 8 years
    ahhh you are on windows? you open up command, then go to your platform tools, eg: c: cd \android\sdk\platform-tools then run adb logcat.
  • peterept
    peterept about 8 years
    Also if you are not sure the Android SDK location, look inside your Unity settings - it will be listed there.
  • Anton Nel
    Anton Nel about 8 years
    THANK YOU! I did win+R (open run) then cmd.exe (command prompt). Once the Command Prompt was open I navigated to the sdk folder using cd .. (to go up a folder) and cd to open a folder. Then once I was in C/Program Files (x86) \Android\android-sdk\platform-tools I typed in adb logcat and it says "waiting for device". Looks like it is working!
  • Anton Nel
    Anton Nel about 8 years
    Also can I filter the logcat so it shows just the logs from my app? Because currently it is showing hundreds of lines per second making it useless
  • peterept
    peterept about 8 years
    Yes, use adb logcat -s Unity