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

Using Unity with Sublime Text 2 (How to get everything set up)

Discussion in 'Scripting' started by JacobPennock, Mar 19, 2012.

  1. JacobPennock

    JacobPennock

    Joined:
    Jul 7, 2011
    Posts:
    31
    Hey guys, I've seen a couple of threads around here asking questions about using Sublime Text 2 with Unity. I just want to say that I have been coding for Unity in Sublime for about the past year and it has been really wonderful.

    I also wanted to share with you some of my plugins that I use to make the Unity/Sublime experience even better.
    Here is a video about them so check it out.


    They were all integrated into Sublime's package control system about a week ago so they are very easy to install.
    there are 3 plugins in total and they include:
    1. Adding Syntax highlighting for C# and Javascript
    2. A pluging that lets you highlight any Unity Class/function and right click to do a Unity reference search
    3. A plugin that gives some snippets and autocompletes of Unity functions (this one is still a work in progress, fork my github package and help me out)

    You can find more info about them and other tips for using Sublime (like how to get it to jump to the correct line from the console) on my blog post here.
     
  2. JacobPennock

    JacobPennock

    Joined:
    Jul 7, 2011
    Posts:
    31
    I just figured out how to call into unity's mono build system from within sublime text 2, so you can program in sublime and get error reporting back within sublime's console, all without even running the unity editor.

    Comment if you are interesting in knowing how.
     
  3. mydingefnysen

    mydingefnysen

    Joined:
    Dec 15, 2011
    Posts:
    51
    Oh yes, tell us how! :D

    And thanks for the other Sublime goodies btw!
     
    Last edited: Mar 25, 2012
  4. Kratorspore

    Kratorspore

    Joined:
    Sep 22, 2011
    Posts:
    7
    Yes please!!
     
  5. bitomule

    bitomule

    Joined:
    Oct 27, 2008
    Posts:
    173
    JacobPennock please share it!
     
  6. Several

    Several

    Joined:
    Apr 16, 2012
    Posts:
    3
    Magnificent! Thanks for sharing these.

    I'd be curious to learn about connecting with unity's mono build system, too.
     
  7. Bluepeak

    Bluepeak

    Joined:
    Feb 23, 2012
    Posts:
    4
    Thanks for making this, awesome :)
     
  8. rstehwien

    rstehwien

    Joined:
    Dec 30, 2007
    Posts:
    101
    Just discovered sublime today and loving it. Got your plugins and very pleased. How can I kick mono builds? Also got a c# complete that I want to config for unity completion.
     
  9. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Jacob thanks, thanks, and thanks again. I'm loving Sublime Text and you're my new hero :)
     
  10. rstehwien

    rstehwien

    Joined:
    Dec 30, 2007
    Posts:
    101
    While waiting for Jacob to continue hooking together Sublime and Unity (especially jumping from errors in Unity to Sublime), I wrote a Sublime plugin that can run MonoDevelop builds. Just install the package and create a sublime project at the root level of the project (where the main .sln file is) and you can run a build from anywhere in that project. mdtools must be on your path.

    https://github.com/rstehwien/Sublime-MonoDevelop

    Works with Unity and normal MonoDevelop projects. At least lets you build, check for errors, and jump to those errors with F4 and Shift+F4. Doesn't provide any runtime integration with Unity though.

    Still trying to get CompleteSharp to work at all for me so I can get code completion.
     
    mariomario likes this.
  11. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    AMAZING! as soon as I get time I shall be doing this. I have none of those features at the moment, just using it as a glorified notepad... it is my fave IDE by far though. Thanks! :D
     
  12. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Hey, just posted a recap of all the (current) steps to integrate Sublime Text with Unity - mainly for personal use, but someone might find it useful. Here it is.

    P.S. Jacob don't disappear please! :)
     
  13. rstehwien

    rstehwien

    Joined:
    Dec 30, 2007
    Posts:
    101
    You might also want to include updating the C# bundle so Ctrl+R works properly. Basically go to the git below and copy the contents of Preferences, Syntaxes, and Snippets over what you have in your C# Package:
    https://github.com/wintermi/csharp-tmbundle

    Got that tip from the link below where you can also find a msbuild buildfile for those of you using visual studio.(where I got the beginnings of my monodevelop build).
    http://shawnoster.com/blog/post/C-and-Sublime-Text-2-PROTIPS.aspx

    Ditto :)
     
  14. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Updated. Thanks a lot for the additional tips :)
     
  15. rstehwien

    rstehwien

    Joined:
    Dec 30, 2007
    Posts:
    101
    I was able to get code completion working in sublime using CompleteSharp:
    https://github.com/quarnster/CompleteSharp

    Setup something like the following in your CompleteSharp.sublime-settings
    Code (csharp):
    1. "completesharp_assemblies": [
    2.     "D:\\Program Files (x86)\\Unity\\Editor\\Data\\Managed\\UnityEngine.dll",
    3.     "D:\\Program Files (x86)\\Unity\\Editor\\Data\\Managed\\UnityEditor.dll",
    4.     "D:\\Program Files (x86)\\Unity\\Editor\\Data\\Managed\\nunit.framework.dll",    "D:\\Users\\RES\\Documents\\code\\ArcaneProcedures\\ArcaneProceduresLib\\bin\\Debug\\ArcaneProceduresLib.dll"
    5.     ],
    The first few are unity's (including NUnit) the last is one of my own libs... change paths an add more libs as needed. I had some trouble where system classes wouldn't complete or would cause sublime to go unresponsive. I had both .NET and Mono on my path... think CompleteSharp got confused. Possible you will need to add mscorlib.dll with full path... but at this time I dont (and it could cause unresponsiveness if not same as sublime finds).
     
  16. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Yey! Thanks rstehwien :)
     
  17. Sock Puppet

    Sock Puppet

    Joined:
    Jan 9, 2011
    Posts:
    77
    rstehwien - First off great work, thanks a lot for doing this.

    I've got everything working except being able to build with monodevelop (OSX).

    I've added the mdtool path in the settings file but i'm not sure if i've got everything set correctly?

    Code (csharp):
    1.  
    2. {
    3. "working_dir": "${project_path:${folder}}",
    4. "cmd": ["/Applications/Unity/MonoDevelop.app/Contents/MacOS/mdtool", "build", "-t:Build"],
    5. "file_regex": "^\\s*(.*\\.cs)\\(([0-9]*)\\,([0-9]*)\\)\\:\\s*(.*)$",
    6. "selector": "source.monodevelop, source.cs"
    7. }
    8.  
    Thanks again.
     
  18. JacobPennock

    JacobPennock

    Joined:
    Jul 7, 2011
    Posts:
    31
    Hey guys, I have not disappeared. I'm sorry that I have not been as active in this thread as I should have been.
    I've been busy with client work and been traveling some. I am on the unity IRC most days under the name of UnicornForest if you ever are trying to chat with me.

    These new autocompletes are just awesome! Great work rstehwien. I have been working on a new completes file that added all of Mathf and a couple completes for GetComponet and stuff for working with coroutines but now with this new autocompletes I don't really feel like it is needed.


    As far as piping Unity's complier through to sublime's console. I never released anything because, for one, its not as easy to setup as the other plugins and I don't have a ton of time to provide support for it and I was worried lots of folks would have have trouble getting it to work and I would be bombarded with emails and support questions, secondly I could not really get it working well enough for me to feel very safe using it.

    What I was doing was creating sublime project files that called unity's xbuild (xbuild is mono's version of msbuild) to compile everything instead of calling mdtool. Xbuild lets you inject a custom dll from the command line that gets used to collect the logging output. So I created my custom dll for logging to format the output in a way sublime would like. but that was a little tricky as it seems unity ships with a older version of xbuild and I have to go way back in the msdn reference to find out what functions I need. Also it seems there is a bug in the older version of xbuild that sometimes reports a strange error but its basically meaningless to us using Unity.

    Also even through doing this way I have complete control over what is being feed to sublime I could not get the regrex to work correctly with the multiline output in my first screenshot. I can get it to work if I print all the errors and warnings each to a single line, but it doesn't look as nice, but you can click on the errors to go to the line and step through them with the hotkeys and see the error info in sublime's status bar.

    All of this is windows only as well.

    Anyway if one of you guys want to keep working at it, my project files look like this:
    Code (csharp):
    1. {
    2.     "folders":
    3.     [
    4.         {
    5.             "path": "Assets",
    6.             "file_exclude_patterns": ["*.unity","*.guiskin","*.dll","*.meta" ,"*.prefab" ,"*.anim", "*.physicMaterial", "*.flare" , "*.cubemap" , "*.font" , "*.fontsettings" , "*.renderTexture"]
    7.         }
    8.     ],
    9.     "build_systems":
    10.     [
    11.         {
    12.             "name": "Unity Mono",
    13.             "cmd": ["C:\\Program Files (x86)\\Unity\\Editor\\Data\\Mono\\bin\\xbuild.bat", "${project_path}/${project_base_name}.sln","/noconsolelogger", "/logger:${project_path}\\SublimeUnity\\SublimeLogger.dll"],
    14.             "file_regex": "([^(]*)\\((\\d*),(\\d*)\\)\\s*(.*)"
    15.         }
    16.     ]
    17. }
    18.  
    Here is a link my compiled logger and a template project file. To try and get it to work unzip the SublimeUnity folder to the root of you unity project, then rename the file MyUnityProject.sublime-project to the name of your project (this all might break if your project has spaces in its name) Then move it outside the SublimeUnity folder and into the root of your project (the folder containing assets and all the .sln files). Open one of you scripts in sublime and hit ctr+B to build and hope it works.

    I've also included the source of my logger dll in case you want to try and mess with it. I've been building it with monodevelop.

    Good luck everyone, and great work on getting a better auto complete solutions then I had going.

    P.S. The nice things about them being project files is you can see and open all the scripts in you unity project through sublime.

    P.P.S Really good luck everyone, I am sorry but I will not likely have a lot of time to try and provide support for this, so use at your own risk. I personally would not use it with production code. If you do have a problem the best way to ask me about it is to find me on the Unity IRC.
     
  19. rstehwien

    rstehwien

    Joined:
    Dec 30, 2007
    Posts:
    101
    Here is what worked for me... but I don't have Unity installed on my mac machine; the mac is for my day job writing geospatial data visualization apps mainly for the insurance industry (not super exciting but good coworkers and it pays well)
    Code (csharp):
    1. {
    2.     "working_dir": "${project_path:${folder}}",
    3.     "cmd": ["/Applications/MonoDevelop.app/Contents/MacOS/mdtool", "build", "-t:Build"],
    4.     "file_regex": "^\\s*(.*\\.cs)\\(([0-9]*)\\,([0-9]*)\\)\\:\\s*(.*)$",
    5.     "selector": "source.monodevelop, source.cs"
    6. }
    7.  
    Not really different than yours. Make sure your sublime project file is in the same directory as you .sln file. Also try going to the command line in that folder and running the same command to make sure it works
    Code (csharp):
    1. rstehwien$ /Applications/MonoDevelop.app/Contents/MacOS/mdtool build -t:Build
     
    Last edited: Jun 5, 2012
  20. Sock Puppet

    Sock Puppet

    Joined:
    Jan 9, 2011
    Posts:
    77
    I missed "Unity" from the mdtool path for some reason - works now. Thanks!

    Also out of interest is Sublime ever something UT could support officially? I've seen a fair few threads on the forum grumbling about Monodevelop and Sublime is definitely the best alternative I've come across.
     
  21. stimarco

    stimarco

    Joined:
    Oct 17, 2007
    Posts:
    721
    Seconded!

    MonoDevelop is slow, buggy, and prone to bugs and crashing Unity. (It will spawn a new Unity instance if you're not careful with the debugging features. As Unity isn't designed to support multiple instances, this can—and often will—cause problems.)
     
  22. figbash

    figbash

    Joined:
    May 13, 2008
    Posts:
    60
    Hey, I've been playing with completesharp on the mac. If you add the dlls in ScriptAssemblies, then autocomplete will also work for classes/etc in your own game project. Here's my CompleteSharp.sublime-settings:

    "completesharp_assemblies": [

    "/Applications/Unity/Unity.app/Contents/Frameworks/Managed/UnityEngine.dll",

    "/Applications/Unity/Unity.app/Contents/Frameworks/Managed/UnityEditor.dll",

    "/Applications/Unity/Unity.app/Contents/Frameworks/Managed/nunit.framework.dll",

    "/Users/figbash/Desktop/Game/Library/ScriptAssemblies/Assembly-CSharp.dll",

    "/Users/figbash/Desktop/Game/Library/ScriptAssemblies/Assembly-CSharp-Editor.dll",

    "/Users/figbash/Desktop/Game/Library/ScriptAssemblies/Assembly-CSharp-firstpass.dll"

    ],
     
  23. Sock Puppet

    Sock Puppet

    Joined:
    Jan 9, 2011
    Posts:
    77
    Nice one figbash. I wonder if there is a way to get it working without using the explicit file path?

    I tried this...

    Code (csharp):
    1.  
    2. "completesharp_assemblies":
    3.     [
    4.         "/Applications/Unity/Unity.app/Contents/Frameworks/Managed/UnityEngine.dll",
    5.         "/Applications/Unity/Unity.app/Contents/Frameworks/Managed/UnityEditor.dll",
    6.         "${project_path:${folder}}/Library/ScriptAssemblies/Assembly-CSharp.dll",
    7.         "${project_path:${folder}}/Library/ScriptAssemblies/Assembly-CSharp-Editor.dll"
    8.     ],
    9.  
    ...but it didn't work.
     
  24. figbash

    figbash

    Joined:
    May 13, 2008
    Posts:
    60
    I'm not sure, I've been fiddling with that too but no luck. Bit of a pain with multiple projects, though certainly not as painful as monodevelop :)
     
  25. rstehwien

    rstehwien

    Joined:
    Dec 30, 2007
    Posts:
    101
    It is a bit of a pain with multiple projects. I need to check with the maker of CompleteSharp to see if it is possible to inject assemblies into the completion at runtime (rather than in the config file) or have a project specific config file. If it is then inspecting the .sln and .csproj files in my MonoDevelop plugin and updating assemblies is possible (the .sln and .csproj files are pretty simple).
     
  26. JacobPennock

    JacobPennock

    Joined:
    Jul 7, 2011
    Posts:
    31
    Hey we are supposed to be able to override settings in our project files, I know it works with general user settings but I'm not sure if it works with plugin settings. Has anyone tried some like this in a project file?

    Code (csharp):
    1. {
    2.     "folders":
    3.     [
    4.         {
    5.          .....
    6.         }
    7.     ],
    8.     "settings":
    9.     {
    10.         "completesharp_assemblies":
    11.         [
    12.  
    13.             "/Applications/Unity/Unity.app/Contents/Frameworks/Managed/UnityEngine.dll",
    14.  
    15.             "/Applications/Unity/Unity.app/Contents/Frameworks/Managed/UnityEditor.dll",
    16.  
    17.             "${project_path:${folder}}/Library/ScriptAssemblies/Assembly-CSharp.dll",
    18.  
    19.             "${project_path:${folder}}/Library/ScriptAssemblies/Assembly-CSharp-Editor.dll"
    20.         ],
    21.     },
    22.     "build_systems":
    23.     [
    24.         {
    25.           ...
    26.         }
    27.     ]
    28. }
     
    Last edited: Jun 6, 2012
  27. figbash

    figbash

    Joined:
    May 13, 2008
    Posts:
    60
    Good catch! That seems to work great over here.
     
  28. figbash

    figbash

    Joined:
    May 13, 2008
    Posts:
    60
    Jacob: I've been using your syntax highlighting and it's worlds better than the default C#, but I have a couple issues:
    Do you know why command+R not only lists methods, but built in Unity stuff? Is there any way to fix that?
    Any clue why using Edit->Comment->Toggle Comment or Toggle Block Comment doesn't work in your syntax? I have found http://www.holoville.com/blog/wp-content/uploads/2012/05/Comments.zip which fixes it.
     
  29. rstehwien

    rstehwien

    Joined:
    Dec 30, 2007
    Posts:
    101
    My guess is that Jacob's C# templates are based on the older (and busted for command+R) C# templates. A new one can be found here
    https://github.com/wintermi/csharp-tmbundle

    Probably could merge the highlighting from Jacob's with that one. That package is mentioned at the bottom of this blog (and points to another blog with some pro tips):
    http://www.holoville.com/blog/?p=512
     
  30. figbash

    figbash

    Joined:
    May 13, 2008
    Posts:
    60
    I whipped up a script to create sublime text 2 projects. Save it as SyncSublimeText2.cs and just run Assets->Sync Sublime Text 2 Project. I haven't tested it much :) It will add the subfolders and extensions you would like to see, as well as the Unity managed DLLs and any assembly DLLs it finds so you can use autocomplete:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.IO;
    5.  
    6. // Create a Sublime Text 2 project from a Unity project
    7. // Includes folders and file types of your choosing
    8. // Includes all assemblies for autocompletion in CompleteSharp package
    9.  
    10. public class SyncSublimeText2 : Editor
    11. {
    12.     // Put all Assets subfolders you want to include here
    13.     private static string[] includeFolders = new[]{"/Scripts/", "/Shaders/", "/Plugins/"};
    14.    
    15.     // Put all extensions you want to include here
    16.     private static string[] includeExtensions = new[]{"cs", "js", "txt", "shader", "cginc", "xml"};
    17.  
    18.     // Put a custom path for the Unity Managed DLLs here, if you want one
    19.     private const string customUnityDLLPath = "";
    20.  
    21.  
    22.     private static string unityDLLPath = "";
    23.  
    24.     [MenuItem("Assets/Sync Sublime Text 2 Project")]
    25.     static void SyncST2Project()
    26.     {
    27.         // Set the managed DLL path
    28.         if(customUnityDLLPath != "")
    29.         {
    30.             unityDLLPath = customUnityDLLPath;
    31.         }
    32.         else
    33.         {
    34.             if (Application.platform == RuntimePlatform.OSXEditor)
    35.             {
    36.                 unityDLLPath = EditorApplication.applicationContentsPath + "/Frameworks/Managed/";
    37.             }
    38.             else
    39.             {
    40.                 unityDLLPath = EditorApplication.applicationContentsPath + "/Managed/";
    41.             }
    42.         }
    43.  
    44.         // Output file string
    45.         string outFile = "";
    46.         // Output file location
    47.         string outFolder = Application.dataPath.Substring(0, Application.dataPath.Length - 7);
    48.         // Get folder name for current project
    49.         string projectFolderName = outFolder.Substring(outFolder.LastIndexOf("/") + 1);
    50.  
    51.         // Add project folders
    52.         outFile = "{\n";
    53.         outFile += "\t\"folders\":\n";
    54.         outFile += "\t[\n";
    55.  
    56.         for(int n = 0; n < includeFolders.Length; n++)
    57.         {
    58.             string cFolder = includeFolders[n];
    59.  
    60.             outFile += "\t\t{\n";
    61.             outFile += "\t\t\t\"file_include_patterns\":\n";
    62.             outFile += "\t\t\t[\n";
    63.            
    64.             for(int i = 0; i < includeExtensions.Length; i++)
    65.             {
    66.                 string cExtension = includeExtensions[i];
    67.  
    68.                 outFile += "\t\t\t\t\"*." + cExtension + "\"";
    69.                 if(i != includeExtensions.Length-1)
    70.                 {
    71.                     outFile += ",";
    72.                 }
    73.                 outFile += "\n";
    74.             }
    75.  
    76.             outFile += "\t\t\t],\n";
    77.             outFile += "\t\t\t\"path\": \"" + Application.dataPath + cFolder + "\"\n";
    78.             outFile += "\t\t}";
    79.  
    80.             if(n != includeFolders.Length-1)
    81.             {
    82.                 outFile += ",";
    83.             }
    84.             outFile += "\n";
    85.         }
    86.  
    87.         outFile += "\t],\n";
    88.         outFile += "\n";
    89.  
    90.         // Add autocompletion assemblies
    91.         outFile += "\t\"settings\":\n";
    92.         outFile += "\t{\n";
    93.         outFile += "\t\t\"completesharp_assemblies\":\n";
    94.         outFile += "\t\t[\n";
    95.         outFile += "\t\t\t\"" + unityDLLPath + "UnityEngine.dll\",\n";
    96.         outFile += "\t\t\t\"" + unityDLLPath + "UnityEditor.dll\",\n";
    97.        
    98.         outFile += "\t\t\t\"" + Application.dataPath + "/../Library/ScriptAssemblies/Assembly-CSharp.dll\",\n";
    99.         outFile += "\t\t\t\"" + Application.dataPath + "/../Library/ScriptAssemblies/Assembly-CSharp-Editor.dll\"";
    100.  
    101.         string[] dllFiles = Directory.GetFiles(Application.dataPath, "*.dll", SearchOption.AllDirectories);
    102.        
    103.         if(dllFiles.Length > 0)
    104.         {
    105.             outFile += ",\n";
    106.         }
    107.         else
    108.         {
    109.             outFile += "\n";
    110.         }
    111.  
    112.         foreach(string file in dllFiles)
    113.         {
    114.             outFile += "\t\t\t\"" + file + "\"";
    115.             if(file != dllFiles[dllFiles.Length-1])
    116.             {
    117.                 outFile += ",";
    118.             }
    119.             outFile += "\n";
    120.         }
    121.         outFile += "\t\t]\n";
    122.         outFile += "\t}\n";
    123.         outFile += "}\n";
    124.  
    125.         // Write the file to disk
    126.         StreamWriter sw = new StreamWriter(outFolder + "/" + projectFolderName + " - ST2.sublime-project");
    127.         sw.Write(outFile);
    128.         sw.Close();
    129.     }
    130. }
     
  31. rstehwien

    rstehwien

    Joined:
    Dec 30, 2007
    Posts:
    101
    Yes putting the following in my project file (and removing it from the global complete sharp settings) does the trick.

    Code (csharp):
    1. {
    2.     "folders":
    3.     [
    4.         {
    5.             "path": "/D/Users/RES/Documents/code/ArcaneProcedures"
    6.         }
    7.     ],
    8.     "settings":
    9.     {
    10.         "completesharp_assemblies": [
    11.             "D:\\Users\\RES\\Documents\\code\\ArcaneProcedures\\ArcaneProceduresLib\\bin\\Debug\\ArcaneProcedures.dll"
    12.             ]
    13.  
    14.     }
    15. }
    16.  
    Had trouble getting it to work with ${project_path:${folder}} or even /D/Users but at least this way I don't need to put my project libraries in the global settings.
     
  32. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    Hi all,

    Firstly thanks for all the effort into getting Sublime to work with Unity. Also to figbash for the great Editor script.

    I have read the thread but it seems to me that it isn't working that well for OSX users. I guess we are spoiled by that slow behemoth of an app MD!

    Currently I don't seem to have autocomplete working, even though my project settings are pointing to the right DLLs and I have complete sharp installed, and the other Unity bundles, and I've run figbash's Editor script. For example typing in Vector3 shows nothing, but Transform does and GameObject doesn't... I don't see completes for static or private but I do for public! o_O

    So what are the current steps for OSX users of getting autocomplete, syntax highlighting and commenting working?

    Also as I understand it we OSX users will need a 3rd party app (Applescript I reckon) to allow for us to intercept the jump to file and line from Unity and pipe that to Sublime....

    So does anyone know where one can find info about Barebones' API for doing this (I've had no luck with searching for this). I believe this is what Unity is using to communicate to text editors, see:

    http://sublimetext.userecho.com/top...-on-osx-to-get-unity-3d-working-with-sublime/

    From my point of view I'd love to use Sublime with Unity on OSX, but for me it just isn't there yet, either because I not 100% sure what steps to take to get it to, or because we are hampered by lack of support within Sublime and/or lack of info.

    Cheers.
     
  33. rstehwien

    rstehwien

    Joined:
    Dec 30, 2007
    Posts:
    101
    After reading the link you gave and following it to a few others, I wrote the following app which should work on OSX too (I tested on windows, but my mac is busted and the mac-mini fund went to Unity 4 upgrades)
    https://github.com/rstehwien/UnityExternalScriptEditorHelper

    If anyone tests it, tell me if it works or what doesn't work and I'll fix it.

    I should write up a blog post on how I setup sublime text to work with Unity and MonoDevelop. It would be windows focused until I can get a mac running (maybe I can resurrect an old laptop). Need to settle on a wordpress theme for my site and actually start blogging.
     
    Last edited: Jun 23, 2012
  34. undergroundhero

    undergroundhero

    Joined:
    Jan 30, 2012
    Posts:
    3
    Hey figbash, thanks for the great editor script to create the project file. It gave me some errors related to invalid escape sequences (as paths in Windows are returned with a single '\' in them), so I fixed it up for Windows users:

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.IO;
    4.  
    5. // Create a Sublime Text 2 project from a Unity project
    6. // Includes folders and file types of your choosing
    7. // Includes all assemblies for autocompletion in CompleteSharp package
    8.  
    9. public class SyncSublimeText2 : Editor {
    10.     // Put all Assets subfolders you want to include here
    11.     private static string[] includeFolders = new[]{"/Code/", "/Shaders/", "/Plugins/"};
    12.  
    13.     // Put all extensions you want to include here
    14.     private static string[] includeExtensions = new[]{"cs", "js", "txt", "shader", "cginc", "xml"};
    15.  
    16.     // Put a custom path for the Unity Managed DLLs here, if you want one
    17.     private const string customUnityDLLPath = "";
    18.     private static string unityDLLPath = "";
    19.  
    20.     [MenuItem("Assets/Sync Sublime Text Project")]
    21.     static void SyncST2Project() {
    22.         // Set the managed DLL path
    23.         if(customUnityDLLPath != "") {
    24.             unityDLLPath = customUnityDLLPath;
    25.         }
    26.         else {
    27.             if (Application.platform == RuntimePlatform.OSXEditor)
    28.                 unityDLLPath = EditorApplication.applicationContentsPath + "/Frameworks/Managed/";
    29.             else
    30.                 unityDLLPath = EditorApplication.applicationContentsPath + "/Managed/";
    31.  
    32.         }
    33.         // Output file string
    34.         string outFile = "";
    35.         // Output file location
    36.         string outFolder = Application.dataPath.Substring(0, Application.dataPath.Length - 7);
    37.         // Get folder name for current project
    38.         string projectFolderName = outFolder.Substring(outFolder.LastIndexOf("/") + 1);
    39.         // Add project folders
    40.         outFile = "{\n";
    41.         outFile += "\t\"folders\":\n";
    42.         outFile += "\t[\n";
    43.         for (int n = 0; n < includeFolders.Length; n++) {
    44.             string cFolder = includeFolders[n];
    45.             outFile += "\t\t{\n";
    46.             outFile += "\t\t\t\"file_include_patterns\":\n";
    47.             outFile += "\t\t\t[\n";          
    48.             for(int i = 0; i < includeExtensions.Length; i++) {
    49.                 string cExtension = includeExtensions[i];
    50.                 outFile += "\t\t\t\t\"*." + cExtension + "\"";
    51.                 if(i != includeExtensions.Length-1)
    52.                     outFile += ",";
    53.                 outFile += "\n";
    54.             }
    55.             outFile += "\t\t\t],\n";
    56.             outFile += "\t\t\t\"path\": \"" + Application.dataPath + cFolder + "\"\n";
    57.             outFile += "\t\t}";
    58.             if(n != includeFolders.Length-1)
    59.                 outFile += ",";
    60.             outFile += "\n";
    61.         }
    62.         outFile += "\t],\n";
    63.         outFile += "\n";
    64.         // Add autocompletion assemblies
    65.         outFile += "\t\"settings\":\n";
    66.         outFile += "\t{\n";
    67.         outFile += "\t\t\"completesharp_assemblies\":\n";
    68.         outFile += "\t\t[\n";
    69.         outFile += "\t\t\t\"" + unityDLLPath + "UnityEngine.dll\",\n";
    70.         outFile += "\t\t\t\"" + unityDLLPath + "UnityEditor.dll\",\n";
    71.         outFile += "\t\t\t\"" + Application.dataPath + "/../Library/ScriptAssemblies/Assembly-CSharp.dll\",\n";
    72.         outFile += "\t\t\t\"" + Application.dataPath + "/../Library/ScriptAssemblies/Assembly-CSharp-Editor.dll\"";
    73.         string[] dllFiles = Directory.GetFiles(Application.dataPath, "*.dll", SearchOption.AllDirectories);
    74.         if(dllFiles.Length > 0)
    75.             outFile += ",\n";
    76.         else
    77.             outFile += "\n";
    78.         for (int i = 0; i < dllFiles.Length; i++) {
    79.             string file = dllFiles[i];
    80.             file = file.Replace((char)92, (char)47);
    81.             outFile += "\t\t\t\"" + file + "\"";
    82.             if(i < dllFiles.Length-1)
    83.                 outFile += ",";
    84.             outFile += "\n";
    85.         }
    86.         outFile += "\t\t]\n";
    87.         outFile += "\t}\n";
    88.         outFile += "}\n";
    89.         // Write the file to disk
    90.         string projectFile = outFolder + "/" + projectFolderName + " - ST2.sublime-project";
    91.         StreamWriter sw = new StreamWriter(projectFile);
    92.         sw.Write(outFile);
    93.         sw.Close();
    94.     }
    95. }
     
  35. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    Sublime Text 2 is out of beta and supports "ODB Editor Suite" natively on OSX! Now just need Unity recognise ST like it does TM and send the line number as it doesn't seem to at the moment. But at least for us OSX users this is a big step in the right direction.

    http://www.sublimetext.com/blog/articles/sublime-text-2-0-released
     
  36. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Great info and much appreciated help here. I've also came across ST and would like to use with Unity. Problem is that I code in Unity Javascript rather than c#. Can we get autocomplation for JS in ST?
    Thanks in advance
     
  37. DMinsky

    DMinsky

    Joined:
    May 20, 2012
    Posts:
    30
    Thanks for your job!
    But isn't fully work on my mac. Sublime open correct file, but jump to line not work. When I execute from command line something like this
    Code (csharp):
    1.  
    2. "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" "/Users/Dmitry/Work/Empty/Assets/Scripts/NewBehaviourScript.cs":2
    3.  
    All is ok.
    There is UnityExternalScriptEditorHelper.xml
    Code (csharp):
    1.  
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <config>
    4.     <application><![CDATA["/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl"]]></application>
    5.     <arguments><![CDATA["*file*":*line*]]></arguments>
    6. </config>
    7.  
    How can I help to fix it?
     
  38. rstehwien

    rstehwien

    Joined:
    Dec 30, 2007
    Posts:
    101
    Could you either PM or Post the UnityExternalScriptEditorHelper.log

    According to the sublm command line sending file:line should work. My suspicion is that Unity on mac isn't sending the line in the same way (if it is at all). The log file should expose the entire command line sent to the helper and I may need to add some mac/windows specific code to parse out the line.
     
  39. DMinsky

    DMinsky

    Joined:
    May 20, 2012
    Posts:
    30
    Hmm, looks like UnityExternalScriptEditorHelper don't create log file on mac. I try to rewrite logging and when run binary separately all is ok. But when put it in MonoDevelop.app, then open some script from Unity, logs are no longer created, but script is loaded in Sublime.
     
  40. rstehwien

    rstehwien

    Joined:
    Dec 30, 2007
    Posts:
    101
    Sorry about the delay, been busy with work.

    My guess is that the log doesn't work on OSX because it doesn't have permission to write into the unity app directory. I can change the path to write to a temp directory instead (or you can if you check out my code and feel comfortable making the change). I still need to get one of my dead macs working.
     
  41. DMinsky

    DMinsky

    Joined:
    May 20, 2012
    Posts:
    30
    I tried to change logPath to "/tmp/UnityExternalScriptEditorHelper.log" no result, log file was not create.
     
  42. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    For me it also doesn't work (on Mac). Here's the log:

    Code (csharp):
    1. *****args*****
    2. /Volumes/Data/Documents/Projects/ProjTest/Assets/Test.cs
    3.  
    It doesn't even open the application. No line numbers..

    I also had problems selecting the app in unity, because it's an exe file which isn't recognized as app. I tried to wrap it into an automator app which executes following bash script:
    Code (csharp):
    1. mono /Applications/Unity/STHelper.exe "$@"
    I don't know if this is the problem.
     
  43. figbash

    figbash

    Joined:
    May 13, 2008
    Posts:
    60
    Thanks for the edit underground hero, glad you find the script useful.

    Everyone that's trying to get line jumping on OSX: The secret to it is to not use the Sublime Text App itself. The author provides a separate app specifically for taking in command line arguments on OS X:
    /Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl

    arguments are PATHTOFILE:LINENUMBER:COLUMNNUMBER
     
  44. figbash

    figbash

    Joined:
    May 13, 2008
    Posts:
    60
    The problem you are having is probably that Sublime will not autocomplete type names. What CompleteSharp does is autocomplete stuff in the assembly, so:

    Typing Vec will not autocomplete to Vector3 (unless you have typed Vector3 in the file beforehand, this is Sublime's internal autocomplete)
    But with Complete Sharp doing:
    Vector3 Test;
    Test.
    will come up with X, Y, Z etc for the Vector3 type.


     
  45. whazlewo

    whazlewo

    Joined:
    May 17, 2012
    Posts:
    6


    Thats better than what I'm getting... I just get a process serial number...


    *****args***** [1]
    -psn_0_1405271
     
    Last edited: Jul 24, 2012
  46. robot-ink

    robot-ink

    Joined:
    Jul 1, 2011
    Posts:
    18
    Thanks for this. Got as far as building this on OSX which makes an .exe file that Unity doesn't want to open as an editor. Never built a .app so i'm trying to figure that out now... hopefully that should work?
     
  47. pabu

    pabu

    Joined:
    Mar 17, 2010
    Posts:
    30
    Hi,

    I wrote a Sublime Text 2 wrapper for OSX. Double clicking a compiler error in the Unity console window will open the affected file with the cursor on the correct line. The app is signed to work with GateKeeper enabled on 10.8.

    Disclaimer: I have never written an OSX app before and didn't really have the time to read up on the subject before digging in. It works on my machine as a result of trial and error, and may very well fail on someone else's machine or with different versions of OS (10.8.0), Unity (3.5.4) or SublimeText (2.0.1). The path to Sublime Text 2 is hardwired in the wrapper and assumes that Sublime Text 2 resides in the global Applications folder.

    Instructions: Run the program and follow the on-screen instructions.

    Download: http://buckadero.se/unity/SublimeWrapper.zip

    Cheers,
    pabu
     
    Last edited: Aug 2, 2012
  48. Kratorspore

    Kratorspore

    Joined:
    Sep 22, 2011
    Posts:
    7
    Awesome pabu!!! Nice work! This was the missing piece!

    S.
     
  49. Kratorspore

    Kratorspore

    Joined:
    Sep 22, 2011
    Posts:
    7
    Hey pabu, fyi, I found your wrapper fails to work if you have a multi-column layout in ST. Works fine for a single col layout.

    Cheers,

    Sam.
     
  50. Kratorspore

    Kratorspore

    Joined:
    Sep 22, 2011
    Posts:
    7
    Huh? Seems to be working in multi-col layout now, scratch that! :p

    S.