Search Unity

Other How to debug Unity Game in Editor Play Mode in Ubuntu 22.10

Discussion in 'Linux' started by alecamara, Nov 10, 2022.

  1. alecamara

    alecamara

    Joined:
    Feb 12, 2017
    Posts:
    2
    TL;DR version in first reply to this post. Long version ahead.

    Hello! I've been fighting my way to get Unity working on a new Ubuntu 22.10 machine I recently repurposed for GNU/Linux. I got it to a point where the experience is quite similar to what I get from my other laptop (MacOS) when both use VSCode. I wanted to capture my process as I believe it can help others :D

    First of all, to setup Unity in Ubuntu 22.04 LTS (the official distro supported by Unity) please follow the excellent post by Gennady: https://forum.unity.com/threads/unity-hub-on-linux.1222968/ These instructions are apparently also applicable to 22.10. From this point on I managed to install a Unity version (2022.1.18f1) and open my project without a hitch. (Well, I had to #if-guard some of my code that was iOS specific, but for the rest that was it).

    The final bit I needed in order to develop my game in Ubuntu was a way to attach a debugger and inspect the game state when hitting a breakpoint. Here are some steps I followed to get it working:

    Step 1 - Install VS Code
    I started by installing VS code. I tried both installing from the Ubuntu Software app (snap) and downloading the .deb file from the official page. Both approaches work the same for me.

    Step 2 - Install the Unity Debugger extension
    Once installed, I went to the extensions and downloaded the latest version of the extensions:
    • Debugger for Unity - this is not maintained anymore, but seems to keep working for now.
    • C# - this is a dependency for the debugger
    upload_2022-11-10_20-5-14.png

    At this point I tried to attach using CTRL+SHIFT+P and running the "Unity Attach Debugger" command. However I was getting "No Unity Process Found" pop-ups. Searching for this error I found the actual JS code used to attach calls "mono UnityDebugger.exe list", so I tried to run that locally and found that "mono" was not a valid command :rolleyes:

    Step 3 - Install mono
    I had to install mono by just typing:

    sudo apt-get install mono-runtime

    After the installation, I tried to attach the debugger from vscode again and I got a list of processes to attach to! Exciting! However, clicking on any process will just closed the menu, it'll try to open the debugger panel, and it will close it almost immediately :(

    The key to solving this problem was looking at the logs for the extension. That file is on your home folder:
    ~/.vscode/extensions/unity.unity-debug-3.0.2/bin/UnityDebug-log.txt

    My log had the following message:

    20:06:25.365012: Log
    ---

    20:06:25.370385: Unity Editor process: Unity on id: 4147
    20:06:25.445041: UnityPlayerConnection is constructed
    20:06:25.449719: Known size of available Players: 0
    20:06:28.451626: New size of available Players: 0
    Very cryptic - but made me think that my Unity player wasn't ready to be debugged. Then I realised that you need to switch to "debug mode" in order to debug when playing in the editor.

    Step 4 - Switch to Debug Mode
    This one is very easy. Just hit the little "bug" icon on the bottom right of the Unity Editor. The official manual covers it very well: https://docs.unity3d.com/Manual/ManagedCodeDebugging.html

    upload_2022-11-10_20-11-39.png
    I tried to attach after this and... nah, still wouldn't work. I decided to check the log again in case it had changed. And it did indeed:

    9:23:29.588527: Log
    ---

    19:23:29.606478: UnityDebug
    19:23:29.610110: Running session
    19:23:29.616867: Exception: System.TypeLoadException: Could not load type of field 'Mono.Debugging.Client.BreakpointStore:breakpoints' (4) due to: Could not load file or assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies.
    at UnityDebug.Program.RunSession (System.IO.Stream inputStream, System.IO.Stream outputStream) [0x0000c] in <047db2e167724751817247551060301d>:0
    at UnityDebug.Program.Main (System.String[] argv) [0x00048] in <047db2e167724751817247551060301d>:0
    Clearly this is the UnityDebugger.exe (which is shipped with the Debugger for Unity vscode extension) trying to load an assembly (netstandard v2.0.0.0) that cannot find in the system. That led to me an issue in the mono github: https://github.com/mono/mono/issues/17148 Apparently we have to install mono-devel to get this code library.

    Step 5 - Install netstandard.dll via mono-devel
    This is easy, just an apt-get command:

    sudo apt-get install mono-devel

    After all of this, finally, I managed to get vscode attached to Unity and breaking execution at my breakpoints!

    upload_2022-11-10_20-26-5.png

    I realise this post is quite long, but I'm very excited to get this working and wanted to share this in case it's useful to anyone :) I'll write a quick summary of these steps in the first reply to this post.
     
    qliloushq likes this.
  2. alecamara

    alecamara

    Joined:
    Feb 12, 2017
    Posts:
    2
    Quick summary:
    • Step 0 - Install Unity using the official guide: https://forum.unity.com/threads/unity-hub-on-linux.1222968/
    • Step 1 - Install vscode either via Ubuntu Software or by installing the official .deb package
    • Step 2 - Install both the deprecated "Debugger for Unity" and "C#" extensions for vscode.
    • Step 3 - Install mono: apt-get install mono-runtime
    • Step 4 - Switch Unity Editor to debug mode (click on bug-like icon on the bottom-right of the editor)
    • Step 5 - Install netstandard library: apt-get install mono-devel
    • Step 6 - Make Games! :cool:
     
    Pnvanol and qliloushq like this.
  3. Pnvanol

    Pnvanol

    Joined:
    Jan 11, 2016
    Posts:
    116
    I found that also helps to call the debugger with ctrl+shift+P then select attach debugger and use the first unity instance that comes up