Search Unity

Sublime as external editor on OS X, always opens a new window

Discussion in 'Formats & External Tools' started by nicmarxp, Jan 7, 2018.

  1. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    When I double click to edit a script inside Unity, Sublime opens my project with the correct file loaded. The second time I do it, a new instance of Sublime opens. And this keeps on forever.

    I see a lot of thread about this, but no solution that works. I already have this in Sublime settings:
    "open_files_in_new_window": false,

    Doesn't make a difference.

    However, when I open sublime from the command line, like this:
    /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl test.txt:5

    I can run that many times, and it still only opens one Sublime instance. I tried placing this in a subl.command-file, but that can't be selected as the external editor.

    I tried to make an "App" called "Sublime Unity.app" using the Appify script from the command file, and assign the app as an external editor, but when I click files to edit, nothing happens.

    I'm out of ideas, so currently I just switch to Sublime, and pick the file to edit from the project tree structure, but sometimes it would be easier to jump straight from Unity.

    Any tips would help!
     
  2. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    Have you filed a bug? This happens to me, too, and it's maddening.
     
  3. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
    I can confirm this is the case for me too, is there anyone at Unity willing to look into this?

    At least we need to know whether this is a Unity problem or Sublime problem (but as demonstrate by OP, using `subl` command we can't reproduce this problem). I would love to know how Unity invoke sublime, the exact command I mean.
     
  4. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
    By the way, if I have Sublime closed when I click in Unity console, then all subsequent window will be the same Sublime window. But if I have Sublime opened, then Unity will launch a separate Sublime window no matter what I do.
     
  5. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    Are you still getting this problem in 2017.3.1? It was fixed for me with this version. I did have to clear the cline arguments from the external editor preferences in Unity, though.
     
  6. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
    On 2017.3.0f3 at the moment, didn't know there is a 3.1, will look into it later today.

    By the way this is my setup:

    Screen Shot 2018-02-12 at 14.07.44.png
     
    tetsuoR likes this.
  7. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
    OK looks like they fix it in 2017.3.1, I can't reproduce this issue now.
     
    Marble likes this.
  8. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Great I will look into this!
     
  9. booferei

    booferei

    Joined:
    Sep 29, 2016
    Posts:
    21
    The bug still occurs if the "External Script Editor Args" field is set. Clear it. (see unity preferences image above)
     
    Pthorndycraft and James_Cooper like this.
  10. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    Thanks, however I just switched to VS Code, since the Omnisharp/resharper features work so much better. There are still some settings I couldn't reproduce in VS Code, but I find it more stable, and I was surprised that it was so similar to Sublime in how it works, for being a Microsoft product :)
     
  11. melgeorgiou

    melgeorgiou

    Joined:
    Nov 3, 2012
    Posts:
    772
    Hi everyone,

    Sorry to revive an old thread but as a sublime text fan myself, this issue was driving me nuts so I came up with my own workaround for Mac. The issue occurs because Unity doesn't allow us to select the 'real' app inside the Sublime Text folder and it is somehow launching the app in a strange way which opens multiple instances.

    Anyway, my workaround *should* work even if you update Sublime and also allows you to open files at a specific line. So here it is:

    1) Make sure you can easily open Sublime Text 3 from the terminal by setting it up in your bash profile. I followed the step by step instructions here so you can easily launch it by typing 'sublime':

    https://techtalktone.wordpress.com/2015/02/01/open-sublime-text-3-from-terminal-on-mac-os-x/

    2) This is where the solution gets creative, lol. Open the built-in 'Automator' app on the Mac (it's awesome!) and choose to create a new Application. You only need a single 'Run Shell Script' action using the code below (make sure you set Pass Input 'as arguments'):

    AutomatorSetup.png

    Once you've done that, save the automator application to your Applications Folder. I called mine "Sublime Text Launcher For Unity".

    3) Back in Unity, you can now select your External Script Editor as your newly created 'Sublime Text Launcher For Unity' application with the usual arguments:

    Screenshot 2020-05-21 at 11.26.59.png

    That's it. Every time you open a script in Unity, it will send it to your Sublime Text Launcher automator app and be handled the right way to only open it in a single instance of Sublime. You shouldn't notice any weirdness, things should just work.

    I'm obviously not going to run support on this but it does work for me on Mac OS X 10.15.4 :)

    - Mel
     
    Last edited: May 21, 2020
    UNSH likes this.
  12. Sailendu

    Sailendu

    Joined:
    Jul 23, 2009
    Posts:
    254
    @melgeorgiou thanks a lot for posting this solution, I was facing the same issue, now it's working.
     
  13. melgeorgiou

    melgeorgiou

    Joined:
    Nov 3, 2012
    Posts:
    772
    UNSH likes this.
  14. UNSH

    UNSH

    Joined:
    Jul 2, 2012
    Posts:
    51
    Other solution set unity preferences to open file by extension and set the default extension in finder (cmd+i) for .cs to Sublime.
     
  15. drpelz

    drpelz

    Joined:
    Dec 7, 2017
    Posts:
    69
    Just wanted to say thank you for this great post! It works like a charm on Mac OS X 11.0.1 Big Sur!:)
     
    melgeorgiou likes this.
  16. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    406
    I can't stress enough, use an IDE like Visual Studio or Rider. It will save you so much time and headaches, I'm using Rider which is excellent. First I thought it would be cpu intensive, but I'm super happy with it. Maybe sublime has similar extensions now, but I can't imagine how I worked with plain sublime 3 years ago :)
     
  17. BrainAndBrain

    BrainAndBrain

    Joined:
    Nov 27, 2014
    Posts:
    115
    I was having this exact problem, and the Automator solution worked great. Thanks, @melgeorgiou !

    For Unity's reference, I'm on macOS Catalina 10.15.7, Unity 2020.2.2f1, and SublimeText build 3210.
     
    melgeorgiou likes this.
  18. melgeorgiou

    melgeorgiou

    Joined:
    Nov 3, 2012
    Posts:
    772
  19. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,799
    I can't stress the opposite.

    Unity, how about you fix your sublime support on Mac? The workaround @melgeorgiou does work, although it's a bit slow if you open a whole bunch of files at the same time so a proper way is needed.
     
  20. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,799
    So, since I often stumble upon this thread, since I often have to deal with this issue and I keep forgetting the various solutions, I will add another solution, which IMO is the best one.

    It's from here: https://www.reddit.com/r/Unity3D/comments/7vtpeu/unity_opens_multiple_sublime_text_instances/

    And the solution is:

    Write this in Terminal:

    defaults write com.unity3d.UnityEditor5.x kScriptsDefaultApp "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl"

    And then use this for args:

    "$(File)":$(Line)

    And then it should just work.