Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Visual Studio Code on Mac: THE DEFINITIVE GUIDE

Discussion in 'Code Editors & IDEs' started by noio, Jul 8, 2015.

  1. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    226
    Well, the soon to be definitive guide ;)

    So, VSCode 0.5.0 was recently released with some new features aimed at Unity users! Hooray!
    • The -r option to reuse the opened project/window
    • The -g option to go to a specific line!
    • The ability to exclude (.meta) files!
    Brilliant! Those are some big improvements in terms of just working with VSCode as a text editor! This prompted me to finally try and set it up for maximum convenience.

    I thought I would compile all information I found on how to get it to work nicely, including skipping to the right line number and such. I still haven't gotten it to work entirely, but I will update this text as I go along.


    THE GUIDE

    1. Creating a command line shortcut
    First, it's important to create a command line shortcut to VSCode, to be able to run from a terminal or a bash script. On their site, for mac users, they recommend a new command line shortcut.I use the following, which works at preserving the command line arguments (important!)
    Code (bash):
    1. code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $*; }

    If you have opened a project (drag and drop a folder onto the icon), you can confirm that this works by typing code -r -g somefile.txt:10 which should open that file in the existing window, at the given line number.

    2. Opening Your Project
    Before working, you need to open the folder that contains your Unity project. Either drag the folder to VSCode in the doc, select "File -> Open" in VSCode, or open it using the above shortcut as in "code my/project/folder". I always have a terminal open in my project folder to do git commands in, so I simply do "code ." .



    Then, if it is the first time opening the folder, click on the "flame" in the statusbar, and open the -csharp.sln file as a project.

    I read that Unity does not generate these anymore in version 5. I think for that you have to either:


    3. Setting VSCode as Script Editor
    Use UniVSCode by @Rob1508. It is a C# script that hooks into "OnOpenAsset" to open VSCode when a script is clicked. I can confirm that it opens the file at the correct line number.

    Research:

    In order to make Unity open a script file in the same VSCode window with the right line number, we need to somehow pass these -r -g arguments. On Windows this is done from by setting extra arguments in the Unity Preferences. Sadly, those are not available on Mac. There is a generic method described in the wiki article on Using Sublime Text, consisting of creating a shell script and passing through the arguments. However, in another forum thread, it is mentioned that
    Even creating an App out of a shell script through the Automator (@briansetsnail 's suggestion) does not work, as it seems that Unity does not pass the arguments at all. (Thanks @slumtrimpet / source ).



    Apparently the App also needs to implement the "External Editor Apple Event API" (source). VSCProxy is a Mac app that does that.



    4. Connecting the debugger
    This is not something I regularly do, but I'll compile some information once I get there.

    References:
     
    Last edited: Aug 13, 2015
    lprunk and Meltdown like this.
  2. briansetsnail

    briansetsnail

    Joined:
    Mar 3, 2015
    Posts:
    5
    Hey, i have a fix for this. Might not be the right way to do it, but it works. What i did was to go into Automator and chose program. Then from there i added "start shell instruction" (might be called something different, my lang is danish).

    in there i typed
    open -n -b "com.microsoft.VSCode" --args -r -g "$1":$2:$3

    Saved it and used it from unity... And voila!

    I don't think automator is needed, i havn't tried any other method, but what i think makes your solution not work is that you are running outside of your own bash profile, so it doesn't know what the "code" command is. I completely circumvent it by using the direct package command.

    I'm in no way an expert when it comes to shell commands, so its probably super hacky, but it works! :)
     
  3. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    226
    Smart! That's creating an actual .app which wraps a shell script. I'm still having some trouble because it refuses to use the --args but I'm going to try some workarounds.

    Edit: OK I had to make sure to Use Input and also select "as arguments". Also, the default selected shell (/usr/bin/bash) did not work, so I had to select /bin/bash, after which— oddly—/usr/bin/bash disappeared from the list.
     
    Last edited: Jul 8, 2015
  4. briansetsnail

    briansetsnail

    Joined:
    Mar 3, 2015
    Posts:
    5
    Same here :)

    Have you got it working yet? It is working just fine here.. If not, then let me know
     
  5. thoorne

    thoorne

    Joined:
    Jul 22, 2012
    Posts:
    64
    I followed instructions and scripts now open in proper window however I'm not moved to correct line after double clicking Debug.Logs from console.
     
  6. slumtrimpet

    slumtrimpet

    Joined:
    Mar 18, 2014
    Posts:
    372
    I agree, auto-jumping to line numbers isn't working currently for me either.
    See:
    http://forum.unity3d.com/threads/osx-external-editor-w-line-number-and-column.341268/
    http://forum.unity3d.com/threads/external-script-editor-what-apps-are-allowed.8073/

    Also, would be useful to add the bottom bit (about clicking the little flame icon and opening your solution) from here:
    http://laumania.net/2015/04/30/how-to-use-visual-studio-code-as-unity3d-script-editor/
    to the above guide as without it intellisense wouldn't work for me.

    Oh... and also maybe this is obvious to some, but you need to also 'open' your base Unity project dir in your VSC window in order for it to 'see' all the solution file and for all the intellisense stuff to work as well.
     
  7. figbash

    figbash

    Joined:
    May 13, 2008
    Posts:
    60
    noio and slumtrimpet like this.
  8. slumtrimpet

    slumtrimpet

    Joined:
    Mar 18, 2014
    Posts:
    372
  9. Jon-D

    Jon-D

    Joined:
    May 21, 2013
    Posts:
    4
    I made those changes to the bash script in Automator, I get taken to the correct file in the same window, but don't get taken to the correct line. I guess for "for now" workaround is to just use VS Code's UI to jump to the error or warning...
     
  10. Jon-D

    Jon-D

    Joined:
    May 21, 2013
    Posts:
    4
    I also wanted to jump in and post how to filter out .meta files from the VS Code Explore file hierarchy, just in case you're using them and don't want them cluttering up the workspace.

    How to Exclude .meta Files from File Explorer in VS Code
    Open Preferences (or ⌘-,), and in the settings.json file, make sure it looks like the following:

    (note: code is .json, not C#)
    Code (csharp):
    1.  
    2. // Place your settings in this file to overwrite the default settings
    3. {
    4.     "files.exclude": {
    5.         "**/.git": true,
    6.         "**/.DS_Store": true,
    7.         "**/**.meta": true
    8.     }
    9. }
    10.  
    There's more info on Editor options on the VS Code website: https://code.visualstudio.com/Updates#_editor-options
     
  11. slumtrimpet

    slumtrimpet

    Joined:
    Mar 18, 2014
    Posts:
    372
    Use the app figbash linked:

    That works and handles the jumping to the correct line automatically.
     
  12. Jon-D

    Jon-D

    Joined:
    May 21, 2013
    Posts:
    4
    I tried using that app, but it just hangs on my system. It sits open, and doesn't open VS Code. I'm running OS X 10.10.

    I'll gladly take the inconvenience of an added couple clicks to get to an error line, when the trade-off is having really good IntelliSense.
     
  13. slumtrimpet

    slumtrimpet

    Joined:
    Mar 18, 2014
    Posts:
    372
    Did you double-click and run it manually or did you actually set it up to run from Unity as you external editor? Running it manually does sort of just hang but if you run it via Unity is works perfectly for me. (also on 10.10)
     
  14. Jon-D

    Jon-D

    Joined:
    May 21, 2013
    Posts:
    4
    I pointed Unity at it, as the External Editor, but it was still hanging. I wrapped the shell script posted earlier as an app out of Automator, and Unity triggers that just fine, minus not jumping to the line number.
     
  15. e_tip

    e_tip

    Joined:
    Jan 27, 2014
    Posts:
    18
    Does anyone have autocompletion icons wrong ?
    After upgraded from 0.1.0 to 0.5.0 i got all my autocomplete icons like in the image attached
    Schermata 2015-07-22 alle 18.28.03.png

    EDIT: If anyone faced this issue i got this because i clicked on unity file with VSC closed.
    if i open VSC manually and than click on the file in unity i got my icon ... :confused:
     
    Last edited: Jul 22, 2015
  16. Giometric

    Giometric

    Joined:
    Dec 20, 2011
    Posts:
    170
    Finally got around to trying this, works pretty much perfectly!

    Also, I'm having the same issue as you e_tip. It's worth noting that it's not just the icons that are incorrect; none of the auto-complete seems to work at all. Luckily your solution worked for me too. As long as Code is open before I double-click the files in Unity, it comes up working normally.
     
  17. e_tip

    e_tip

    Joined:
    Jan 27, 2014
    Posts:
    18
    Well icons problem is the first thing that i've noted without coding... now that i've looked better at this autocomplete doesn't work... Question.. where should we report this bug ? code bug tracker ?
     
  18. Rob1508

    Rob1508

    Joined:
    Jun 25, 2012
    Posts:
    17
    Info: I created a small script which allows opening cs files in VSCode without any proxy Mac App. If you double click on a console error, log or warning VSCode will automatically jump to that line.

    Simply copy the file into your Unity project: https://github.com/robin7331/UniVSCode

    Please leave an issue if you face a bug. I am currently working on getting the debugger attached to the mono project.
     
    noio and lprunk like this.
  19. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    226
    Hey guys! Hadn't seen the replies yet! I will compile everything into the guide ASAP! Apologies!
     
  20. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    226
    I always have to op en VSC first, and then open files into the already-opened project from Unity.

    Cool! But that means everyone in the project has to use VSC, doesn't it?
     
  21. Rob1508

    Rob1508

    Joined:
    Jun 25, 2012
    Posts:
    17
    Well, that's how it is right now. I've made some modifications though. First, if you are on a other platform than a Mac your c# file will be opened by the tool you've selected in your settings menu. Next I've integrated a little menu item where you can choose wether you like to use VSCode for c# files or not. This setting is user specific to every project member.

    Link: https://github.com/robin7331/UniVSCode

    Let me know if you miss something else! :)

    Cheers
     

    Attached Files:

    noio likes this.
  22. Rob1508

    Rob1508

    Joined:
    Jun 25, 2012
    Posts:
    17
    UPDATE: I've now added a 'Open Project in VSCode' menu to the file menu. This simply opens your project in VSCode as a folder.
     
    noio likes this.
  23. Rob1508

    Rob1508

    Joined:
    Jun 25, 2012
    Posts:
    17
    UPDATE 2: There are some additional changes.. You can now open the whole project folder in VSCode within a simple menu item. In addition to that you can determine and send debugging information to VSCode.
    If you are in play mode in unity simply hit "File -> Set Debugging Port in VSCode", switch back to VSCode and hit Play to debug!
    Docs & Download here: https://github.com/robin7331/UniVSCode
     
    noio likes this.
  24. kujo

    kujo

    Joined:
    Aug 19, 2013
    Posts:
    106
    Is there a way to use unsafe code in VSCode? I'm building for iOS and some of the plugins I use require unsafe, but VSCode/OmniSharp doesn't like it - it gives:

    Code (csharp):
    1. [ERROR:OmniSharp.MSBuild.MSBuildProjectSystem] Unsafe code requires the `unsafe' command line option to be specified
    Which then means that the projects don't load correctly and intellisense doesn't work.
     
  25. thoorne

    thoorne

    Joined:
    Jul 22, 2012
    Posts:
    64
    As far as I know, Unity also won't let you use unsafe keyword unless you make some Mono modifications. Maybe try applying them also to Mono that OmniSharp uses?
     
  26. kujo

    kujo

    Joined:
    Aug 19, 2013
    Posts:
    106
    @thoorne - thanks for the info - I haven't been able to find anything recent on the Mono modifications. The only thing I found was from about 2010 but Unity has all changed since then.

    Unity apparently allows it by using a gmsc.rsp or smcs.rsp file with the line -unsafe in it, and that allows it to work... it seems VSCode ignores this
     
  27. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    226
    After Updating to VSCode 0.7.0 I get an "Error: Failed to start OmniSharp" when loading my project. I will follow up if I find an answer.

    Edit+Solution: I simply brew install mono and it worked again. I hadn't installed mono separately before, and it seems that VSCode automatically picked up on the new version.

    However all the autocomplete and suggestions are INSANELY slow now. I wouldn't recommend updating. mono-sgen is constantly using 100% cpu and I have to wait for VSCode to come up with suggestions, first it's basically telling me that every character I type is a syntax error for a minute.
     
    Last edited: Aug 13, 2015
    nomadic likes this.
  28. CheesyPixel

    CheesyPixel

    Joined:
    May 24, 2015
    Posts:
    3
    I had everything working fine, now I can't get any projects to open. Here is error I get, even when I create a new Unity project and attempt to open it with VSCode. Posted the same question on SO. Any help would be greatly appreciated...

    [ERROR:OmniSharp.Startup] The project system 'MSBuildProjectSystem' threw an exception.
    System.ArgumentException: Value does not fall within the expected range.
    at Microsoft.CodeAnalysis.MSBuild.SectionBlock..ctor (System.String type, System.String parenthesizedName, System.String value, IEnumerable`1 keyValuePairs) [0x00000] in <filename unknown>:0

    http://stackoverflow.com/questions/32001233/vs-code-and-unity-osx-error
     
  29. lprunk

    lprunk

    Joined:
    Aug 7, 2013
    Posts:
    1
    You need to install the latest Mono (http://www.mono-project.com/download/). Microsoft updated OmniSharp to the latest version which requires a new Mono version. Alternatively, you can use homebrew to install Mono as mentioned by noio on his last comment =)
     
  30. frjtrifork

    frjtrifork

    Joined:
    Mar 12, 2015
    Posts:
    29
    I don't use brew - but this worked (as in removes the Error: Failed to start Omnisharp problem) for me (On Beta 6 of El Capitan):
    1. Uninstall old mono versions using the provided script from the bottom of this page: http://www.mono-project.com/docs/about-mono/supported-platforms/osx/
    2. Download and install the Mono Preview package for El Capitan (OS X 10.11) from http://www.mono-project.com/download/
    3. Restart Visual Studio Code
    Note that at first I just downloaded the El Capitan version and installed that without removing the older mono versions. The installer set the Current symlink to point to the 4.2.0 version that was installed -but VS Code still showed the Omnisharp error. I had to remove the old versions of Mono before it used the 4.2 version.

    Is there a VS Code setting where I can provide a path to the mono version that I want to use?

    BTW: I also have the very high cpu usage issue when trying to do completion while typing code - the delay for completion is 5-10 seconds on my system.

    Update: The delay has disappered on my system - now I have instant completion and mono-sgen rarely goes beyond 25% cpu usage.
    I clicked the Flame icon in the bottom of VSCode and chose Change projects and opened the same project as before. Shortly after that completion was speedy again
     
    Last edited: Aug 14, 2015
    noio likes this.
  31. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    226
    I uninstalled the homebrew version of mono and got it from your link, @lprunk , the problems with slowness have gone away now.

    @frjtrifork I did not get the El Capitan version, and my CPU usage looks OK for now.

    OOPS NOPE. Too soon. It's still super slow. Like you said, code completion takes about 10 seconds. This is virtually unusable. Has anyone found a solution?
     
  32. CheesyPixel

    CheesyPixel

    Joined:
    May 24, 2015
    Posts:
    3
    Still no luck with updated mono. Looks like there is some arg that OmniSharp doesn't like in my .sln file, maybe a version number? Can anyone upload a .sln file that VSCode likes so i can compare?

    I just installed the latest mono download from the link above 4.0.3, however when I run terminal mono --version it shows 4.0.2. Is that ok?
     
    Last edited: Aug 14, 2015
  33. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    226
    This is mine. It gives only a couple of warnings but it does work.

    Except for the slowness.

    I have to keep doing this repeatedly. The slowness crawls back in every half hour or so.
     
  34. frjtrifork

    frjtrifork

    Joined:
    Mar 12, 2015
    Posts:
    29
    I posted the 'workaround' of change projects to make completion speedy again - but at that time I had not had VSCode running long enough apparently.
    I have the same issue, completion is speedy immediately after doing the Change projects, but after a while it becomes slow again. Luckily doing the Change projects and selecting the same project again takes only a couple of seconds on my project so although it is annoying, I can live with that workaround for now.
     
  35. TheAngrycrow

    TheAngrycrow

    Joined:
    Jun 16, 2013
    Posts:
    12
    I've been using omnisharp-atom for the last couple months and only heard about this editor recently. Is there anyone who has used both here and has a good sense of the pros/cons? Slowness is certainly an issue. But the most recent release of omniSharp-atom is actually pretty smooth on OSX. My first impression is that this would be the ideal editor for Unreal4 (MS Code ).. I'll have a peak on the boards over there to see what's up..
     
  36. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    226
    I haven't encountered problem in the latest version of VSCode! :)
     
  37. sfjohansson

    sfjohansson

    Joined:
    Mar 12, 2013
    Posts:
    369
    Just grabbed the latest version last night and I have to say...it's getting more promising for everyday :) One thing I've noticed when trying is the syntax highlighting....there is a lot of white... =O Is there a way to remedy this?
     
  38. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Anyone have issues when building/know the solution for this? Apologies if it's been brought up and it's somewhere in the thread.

    Build fails immediately with error:

     
  39. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    226
    I assume that you're using dotBunny's VSCode script. That is an editor script so it should be in a subfolder named "Editor". The "UnityEditor" namespace is not available in builds.
     
  40. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Thanks, I did manage to sort it out eventually. I noticed though that the latest update to VSCode seemed to break the integration somewhat? I couldn't get any of the autocompletion stuff to work anymore. Not sure if that was just me though...

    I've switched back to MonoDevelop for now. It's just easier I think.
     
  41. xpation

    xpation

    Joined:
    Mar 8, 2013
    Posts:
    9
    So it seems like there's a bunch of stuff that Unity is willing to accept and compile fine but Omnisharp doesn't. For example I get this:

    [ERROR:OmniSharp.MSBuild.MSBuildProjectSystem] Use of possibly unassigned field `bounds'

    Now the problem is that there's no reference whatsoever in terms of a line ref or at least a file ref when Omnisharp spews out errors so I have no idea where to go to fix this. This makes the tool totally useless. I have no idea where this offending "bounds" is.

    Is there a way to get file and line refs for OmniSharp errors?

    Thanks!
     
  42. v2-Ton-Studios

    v2-Ton-Studios

    Joined:
    Jul 18, 2012
    Posts:
    238
  43. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    v2-Ton-Studios likes this.