Using Sublime Text as a script editor

From Unify Community Wiki
Jump to: navigation, search

Sublime Text is a popular cross-platform text and source code editor. It is extendable and its community has contributed numerous packages(plugins/extensions/addons) for Sublime Text.

Contents

Directions

  1. Sublime Text can be found at the Official Website. Download and install.
  2. (Mac users should look at 4th step before proceeding) Change your External Script Editor to Sublime Text. (In Unity, "Edit" -> "Preferences" -> "General")
  3. Unity should now open Sublime Text whenever you double-click a script (or any text file).
  4. Make Sublime Text jump to line that causes an error/warning:

Windows: You should set the External Script Editor Args field to: "$(File)":$(Line), so that Sublime Text jumps to the line where the error, warning or notice occurs, when they are double clicked upon.

Mac OS X: If the previous External Script Editor Args is not available in your version of Unity, there is a workaround using a shell script, save this to a file, make it executable(if required) and set that file as the script editor in Unity(Linux users running the Windows version using Wine may also do this):

#!/bin/sh
 
# Change '/opt/sublime_text/sublime_text' to the path to your executable of Sublime Text.
 
# Sublime Text 2:
# "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" $1:$2:$3
 
# Sublime Text 3:
"/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" $1:$2:$3
 
echo "Opening '$1' on line '$2' column '$3' with Sublime Text"
exit 0

Project Setup

Creating a project file has some advantages:

  • Context - specify your script folder(s) for easy search & navigation between your files
  • Settings - filter out irrelevant files (e.g., .meta files)

To create a project use "File" -> "Open Folder" from the header menu and open your Unity3D project's folder(parent of the "Assets" folder). Then use "Tools" -> "Save Project As" to create a my_project.sublime-project file in your project root directory (outside Assets).

For example, this will include only the Assets/Scripts folder and exclude .meta and .dll binary files:

{
	"folders":
	[
		{
			"path": "Assets/Scripts",
			"file_exclude_patterns":
			[
				"*.dll",
				"*.meta"
			]
		}
	]
}

Package Control

Sublime Text has a package called "Package Control" that allows to easily search and manage installed packages. Follow the few instructions described by the official maintainer of package: https://sublime.wbond.net/installation

"Package Control" for Sublime Text 3 uses "Git" distributed revision control and source code management (SCM) system to retrieve and update packages, so if you use ST3 install Git for your operating system.

To access "Package Control" functions from within Sublime Text:

  1. Open the "Command Palette" through "Tools" -> "Command Palette" from the header menu or use the default Command+Shift+P(OS X) or Ctrl+Shift+P(Windows/Linux) shortcut.
  2. Write "Package Control"(without quotes) and you will see functions made available by the package including installing and removing packages.

Syntax highlighting

Unity C# and JavaScript Syntax highlighting

Install the "C#"(could be installed by default) and "Unity3D Syntax Highlighting" package either by using the Package Control or by cloning the package using Git from Github.

'Once installed you can select "View" -> "Syntax" -> "Unity3D" and then either "Unity C#" or "Unity JavaScript". You may also want to select the “Open all with current extension” option so that Sublime Text 2 will always use Unity syntax highlighting when viewing C# or JavaScript Files.' - Jacob Pennock

For plain C# Syntax highlighting compatibility

Note that using "Unity C#" with Sublime Text's built-in "Open all with current extension" will not differentiate between C# scripts that use Unity API and external ones that do not, since they both have the "cs" file extension. One way to avoid this is to use a package called "ApplySyntax" and add the following to its settings:

"syntaxes": [
...
{
	// Unity 3D C# format detection.
	"name": "Unity3D/UnityC#",
	"match": "all",
	"rules": [
		{"file_name": ".*\\.cs$"},
		{"contains": "using UnityEngine;\n"}
	]
}
...
]

(The "Unity3D" package might still lay compressed within Sublime Text's "Installed Packages" folder, which contains "UnityC#.tmLanguage" file that ApplySyntax will be looking for within non-existant "Packages/Unity3D" folder)

Boo Syntax highlighting

Install the "Boo" package using Package Control.

Unity Shader Syntax highlighting

Install the "Unity3D ShaderLab" Sublime Text package.

Code Autocompletion

Setting C# Unity API Classes Autocompletion

Install the following Sublime Text packages by following the instructions on their respective Github README:

Sublime Text package by following the instructions on Github README. User "quarnster" was the original author of CompleteSharp, but has discontinued working on it. "ewilde" version is a bit more up to date and includes a already compiled "CompleteSharp.exe" file which the package uses, which otherwise has to be compiled using Mono(gmcs command).

Add extra assemblies for CompleteSharp to the settings either through "Preferences" -> "Settings - User" or "Project" -> "Edit Project"(opens project settings file) so that Unity3D classes are recognized ("..." represent other possible content between JSON elements):

"folders": [
...
], //<- comma required
...
"settings": {
...

	// Uncomment the next line for OS X/Linux, if "mono" does not launch Mono in the Terminal.
	//"completesharp_mono_path": "/Applications/Unity/MonoDevelop.app/Contents/MacOS/bin/MonoDevelop",

	// Possible default directory paths for Unity:
	// Windows: C:\Program Files\Unity\Editor\Data\
	// Windows 64-bit: C:\Program Files (x86)\Unity\Editor\Data\
	// Mac OS X: /Applications/Unity/Unity.app/Contents/Frameworks/

	"completesharp_assemblies": [
		"<path-to-Unity-folder>\\Managed\\UnityEngine.dll",

		"<path-to-Unity-folder>\\Managed\\UnityEditor.dll",

		"<path-to-Unity-folder>\\Mono\\lib\\mono\\unity\\UnityScript.dll",

		"<path-to-Unity-folder>\\Mono\\lib\\mono\\unity\\System.Core.dll",

		"<path-to-Unity-folder>\\Mono\\lib\\mono\\unity\\System.dll",

		"<path-to-Unity-folder>\\Managed\\nunit.framework.dll",

		"<path-to-Unity-folder>\\Mono\\lib\\mono\\unity\\mscorlib.dll",

		"<path-to-Unity-folder>\\Mono\\lib\\mono\\unity\\System.Core.dll",

		"<path-to-Unity-folder>\\Mono\\lib\\mono\\unity\\System.dll",

		"${project_path:Library}\\ScriptAssemblies\\Assembly-CSharp.dll",

		"${project_path:Library}\\ScriptAssemblies\\Assembly-CSharp-Editor.dll",

		"${project_path:Library}\\ScriptAssemblies\\Assembly-UnityScript-Editor.dll",

		"${project_path:Library}\\ScriptAssemblies\\Assembly-CSharp-firstpass.dll"
	],

	"completioncommon_inhibit_sublime_completions": true,

	"completioncommon_shorten_names": true

...
}

After saving these settings, this should automatically reload CompleteSharp's python scripts and have its autocompletion ready. Note that the default behavior of Sublime Text's autocompletion is to take elements available within the opened file, compare against that. To see if any errors pop-up open the console by using Ctrl+`(Tilde key).

Boo and JavaScript Unity API AutoCompletion

Easy installation: using Package Control => Install Package => Unity Completions

Community Support

See "Using Unity with Sublime Text" forum thread

Helpful Sublime Text packages

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox