Select Category - Game Dev

Category: Game Dev

Unity3D and Sublime Text 3: Breaking the Chains of MonoDevelop

This is a step-by-step tutorial for setting up a Unity C# environment in Sublime Text 3. This is already mostly laid out on the OmniSharp GitHub page, but I wanted to condense the info and also sort out the problems I ran into.

##The Goal

Auto-completion, intellisense, and compiler errors/warnings all within Sublime itself. These steps were tested on my Mac OSX machine so I can't say how different they will be on other operating systems, but they should be mostly the same.

##Drawbacks

In order for these features to work, you need to have the whole Sublime project open, not just a C# file from the project. One issue I have not been able to resolve is that if you double click a C# file inside Unity, and it isn't already open in a tab in the Sublime project, it will open in a fresh sublime window, and it will not have any of the auto-complete or error checking features. Therefore you'll generally be navigating Sublime's hierarchy rather than Unity's when opening files.

##The Tutorial

Lets start by downloading Sublime Text 3 and opening it. Then:

  1. Install Package Control.

    • Open Sublime's console via View > Show Console or CMD + ` .
    • Copy the long command found at https://packagecontrol.io/installation.
    • Paste the command into the console and hit enter to run the command.
  2. Install OmniSharp using Package Control.

    • Open Sublime's Command Palette via Tools > Command Palette or CMD + SHIFT + P.
    • Search for and select Package Control: Install Package.
    • Search for and select OmniSharp. This will install OmniSharp which will allow for a C# dev environment in Sublime.
  3. Download and install Mono from http://www.mono-project.com/. This is required for getting auto-complete info.

  4. Open your Unity project folder in Sublime by going to File > Open and selecting the base directory for your Unity project.

  5. Go to Project > Save Project As and save this Sublime project in your Unity project's base directory. We'll call it myproj.sublime-project, but you can call it anything.

  6. Open myproj.sublime-project, and add the line "solution_file": "MyUnityProj-csharp.sln". Your file should look something like the following. Note: if you don't have any .sln files, you'll need to generate them through Unity via Assets > Sync MonoDevelop Project.

     {
         "folders":
         [
             {
                 "follow_symlinks": true,
                 "path": ".",
             }
         ],
         "solution_file": "MyUnityProj-csharp.sln"
     }
    
  7. Now we need to let OmniSharp generate a C#.sublime-settings file. This file tells Sublime to show Intellisense whenever certain actions are taken, such as pressing period.

    • In order for it to generate the file, you'll first need to have a C# file open and selected (It's odd, but the next step will generate a filetype.sublime-settings file based on whatever file type is focused).
    • Go to Sublime Text > Preferences > Settings - More > Syntax Specific - User. This should create and open the c#.sublime-settings file. Fill it in with the following information.

      {
          "auto_complete": true,
          "auto_complete_selector": "source - comment",
          "auto_complete_triggers": [ {"selector": "source.cs", "characters": ".<"} ],
      }
      
  8. Now close the Sublime project via the Project menu. Restart both Unity and Sublime, and then open the project again in Sublime.

  9. Finally, inside Unity, go to Preferences > External Tools and set the External Script Editor to Sublime Text.

That's it. Everything should now be working. In addition to Intellisense and auto-complete, whenever you save a file, errors and warnings will show up at the bottom of the Sublime window. For every new Unity project, you'll need to complete steps 4, 5, 6, and 8. Keep reading if you want some tips on cleaning up your new C# workspace.

##Removing Clutter from your Project's Hierarchy

One thing you might want to do with any unity project is remove unnecessary files and folders from the Sublime hierarchy. To do that, you simply add exclude and include patterns in myproj.sublime-project.

In my case, I've hidden several unity proj files as well as .meta files. In addition, I've hidden all folders that aren't my Assets folder. However, I wanted to keep some files in the base directory visible, like myproj.sublime-project, so I havn't excluded everything from the base. This is all down to preference. You may even want to narrow it down further and hide everything except the Assets/Scripts folder.

{
    "folders":
    [
        {
            "follow_symlinks": true,
            "path": ".",
            "file_exclude_patterns": [
                "*.meta",
                "*.csproj",
                "*.booproj",
                "*.unityproj"
            ],
            "folder_include_patterns": [
                "Assets",
                "Assets/*"
            ]
        }
    ],
    "solution_file": "MyUnityProj-csharp.sln"
}

##Eliminating Unecessarily Strict C# Warnings

Another problem you may have noticed is how strict the warnings are when you save a C# file. Some warnings are great, but other ones may conflict with your style or how Unity C# code is supposed to be written. Luckily we can suppress these warnings by doing the following.

  1. Go to Sublime Text > Preferences > Browse Packages. This will open up the Packages directory in Finder.

  2. Open Packages/OmniSharp/PrebuiltOmniSharpServer/config.json.

  3. You should see a section named "IgnoredCodeIssues". Here you can add all the regular expressions you want to eliminate unnecessary warnings. Here's my list with several common warnings. I'll try to add more as I find them.

     "IgnoredCodeIssues": [
         "^Keyword 'private' is redundant. This is the default modifier.$",
         ".* should not separate words with an underscore.*",
         "^Redundant argument name specification$",
         "^Convert to '.*' expre.",
         "^Use 'var' keyword$",
         "^Name should have prefix.",
         "^Redundant ToString.",
         "^Initializing field by default value is redundant$",
         "^Parameter can be IComparable$",
         "^Convert to constant.*",
         "^Variable could be moved to a nested scope$",
         "^Method never reaches its end or a 'return' statement.$",
         ".* should start with an upper case letter. \\(Rule 'Enum Members'\\)$"
     ],
    
  4. Note that if you ever update OmniSharp, the config.json will be overwritten. We need to copy the file elsewhere in order to prevent this.

    • Make a copy of config.json and put it anywhere on your computer.
    • Open Packages/User/OmniSharpSublime.sublime-settings and set "omnisharp_server_config_location" to wherever you put the config.json file.

So that's it. We've got a nice clean Unity3D C# environment in Sublime Text 3. Feel free to send me an email or tweet me if you have any questions or any advice on improving this post.

Cyberpunk Jam- Cyb3r Pukes

cyber_pukes_thumb

I recently submitted an entry for the Cyberpunk Jam. If you like rock music, or music, or pink squares, or tiles, or colors, check out my deadly punk concert sim, Cyb3r Pukes.

Back to top