Using Sublime Text as a script editor

From Unify Community Wiki
Revision as of 09:59, 10 April 2014 by Jānis Lukss (Talk | contribs)

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. 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 actually).
  4. 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.

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)

Unity Shader Syntax highlighting

Install the "Unity3D ShaderLab" Sublime Text package.

Code Autocompletion

Setting C# System Type Autocompletion

Install the "CompleteSharp" 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 assemblies to the settings either through "Preferences" -> "Settings - User" or "Project" -> "Edit Project"(opens project settings file):

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

	// Uncomment for OS X/Linux - Add the correct path to your Mono command if "mono" does not launch Mono in the Terminal.
	//"completesharp_mono_path": "/Applications/Unity/MonoDevelop.app/Contents/MacOS/bin/MonoDevelop",

	"completesharp_assemblies": [
		"<path-to-Unity-folder>\\Editor\\Data\\Mono\\lib\\mono\\unity\\System.Core.dll",
		"<path-to-Unity-folder>\\Editor\\Data\\Mono\\lib\\mono\\unity\\System.dll"
	],

	"completioncommon_inhibit_sublime_completions": true,

	"completioncommon_shorten_names": true

...
}

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

Setting C# Unity API Classes Autocompletion

Install "CompleteSharp", "Unity Completions Package" and "Unity3D Snippets and Completes" Sublime Text packages.

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:

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

	// Uncomment for OS X/Linux - Add the correct path to your Mono command if "mono" does not launch Mono in the Terminal.
	//"completesharp_mono_path": "/Applications/Unity/MonoDevelop.app/Contents/MacOS/bin/MonoDevelop",

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

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

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

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

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

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

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

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

		"<path-to-Unity-folder>\\Editor\\Data\\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 autocompletion ready. Note that the default behavior of Sublime Text's autocompletion is to take elements available within the opened file. To see if any errors pop-up open the console by using Ctrl+`(Tilde key).

Boo, C# and JavaScript

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