Visual Studio Code can be a great companion to Unity for editing and debugging C# files. All of the C# features are supported and more. In the screen below, you can see code colorization, bracket matching, IntelliSense, CodeLens and that's just the start.
Use dotnet -version from the terminal to get the version installed. Create an Uno Platform project Install Uno Platform Template. Launch Visual Studio Code and open a new terminal. In the terminal, type the following to install the Uno Platform templates: dotnet new -i. The full version of Visual Studio is 'Visual Studio', 'Code' is what differentiates 'Visual Studio Code'. As far as an integrated command line inside Visual Studio goes, the Package Manager Console is it. It's basically just powershell, with some addins from Visual Studio and any extensions or NuGet packages you have installed.
Read on to find out how to configure Unity and your project to get the best possible experience.
Prerequisites
From Using .NET Core in Visual Studio Code:
- Install the .NET Core SDK, which includes the Runtime and the
dotnet
command. - [Windows only] Logout or restart Windows to allow changes to
%PATH%
to take effect. - [macOS only] To avoid seeing 'Some projects have trouble loading. Please review the output for more details', make sure to install the latest stable Mono release.Note: This version of Mono, which is installed into your system, will not interfere with the version of MonoDevelop that is installed by Unity.
- Install the C# extension from the VS Code Marketplace.
Setup VS Code as Unity Script Editor
Dotnet Test Visual Studio Code
Open up Unity Preferences, External Tools, then browse for the Visual Studio Code executable as External Script Editor.
The Visual Studio Code executable can be found at
/Applications/Visual Studio Code.app
on macOS, %localappdata%ProgramsMicrosoft VS CodeCode.exe
on Windows by default.Unity has built-in support for opening scripts in Visual Studio Code as an external script editor on Windows and macOS. Unity will detect when Visual Studio Code is selected as an external script editor and pass the correct arguments to it when opening scripts from Unity. Unity will also set up a default
.vscode/settings.json
with file excludes, if it does not already exist (from Unity 5.5 Release notes).Unity version 2019.2 or above
Since 2019.2, it is required to use the Visual Studio Code editor package. The built-in support for opening scripts from Unity and getting
csproj
and sln
files generated has been removed.Editing Evolved
With the solution file selected, you are now ready to start editing with VS Code. Here is a list of some of the things you can expect:
- Syntax Highlighting
- Bracket matching
- IntelliSense
- Snippets
- CodeLens
- Peek
- Go-to Definition
- Code Actions/Lightbulbs
- Go to symbol
- Hover
Two topics that will help you are Basic Editing and C#. In the image below, you can see VS Code showing hover context, peeking references and more.
Unity Extensions
The community is continually developing more and more valuable extensions for Unity. Here are some popular extensions that you might find useful. You can search for more extensions in the VS Code Extension Marketplace.
The extensions shown above are dynamically queried. Select an extension tile above to read the description and reviews to decide which extension is best for you. See more in the Marketplace.
Enabling code completion (For recent versions of Unity)
If you are installing VS Code for the first time, you might be missing targeting packs required for Unity's code-completion (IntelliSense) in VS Code.
Targeting pack download links:
Steps:
- Stop VS Code or Unity running.
- Download and install the targeting pack for your targeted framework version / preferred version from one of the above links.
- Start Unity.
- Create and/or open an existing script in VS Code, through Unity, and you should now see code completions.
Enabling Unity warnings
Unity has a set of custom C# warnings, called analyzers, that check for common issues with your source code. These analyzers ship out of the box with Visual Studio but need to be set up manually in Visual Studio Code.
Due to how Unity handles its
.csproj
files, it does not seem possible to install packages automatically. You will need to download the analyzers from the NuGet website manually. When you're done, open the package file using a tool such as 7zip and extract Microsoft.Unity.Analyzers.dll
onto your project's root folder. You can place it inside a folder named NuGet
, for example. Do not place it inside Assets
or Packages
, as that will cause Unity to try to process the .dll
, which will make it output an error in the console.Kaleidoscope is here for every moment, whether you’re looking for wear-on-repeat styles or wanting to refresh your home. Elevate your everyday. Shop for stylish ladies fashion, footwear and homeware online at Kaleidoscope. At kaleidoscopeshop.com we feature unique gifts and collectible kaleidoscopes by more than 200 artists. Find antique kaleidoscopes, wooden kaleidoscopes, stained glass kaleidoscopes plus toys, games, more. We are located in Atlanta, GA. Online shopping for Prisms & Kaleidoscopes from a great selection at Toys & Games Store. Kaleidoscope online shopping. Online Shop European Casual - Relaxed Elegance At Kaleidoscope you will experience the beauty of the right colors and shapes to create the feeling in your home that reflects your lifestyle. Every Project is Unique – Every Client is Unique. Proudly serving Springfield, Missouri for over 48 years, Kaleidoscope is pleased to welcome you to our online store. Now offering our trademark shirts, hoodies, beanies.
Next, create an
omnisharp.json
file at the root folder of your project, as explained here. Analyzer support in OmniSharp is experimental at the moment, so we need to enable it explicitly. We also need to point it to the .dll
file we just extracted.Your
omnisharp.json
file should end up looking like this:where
'./NuGet/microsoft.unity.analyzers.1.9.0'
is a relative path pointing to the folder containing the .dll
file. Depending on where you placed it, your path may look different.The Unity analyzers should now be working in your project. You can test them by creating an empty
FixedUpdate()
method inside one of your MonoBehavior
classes, which should trigger a The Unity message 'FixedUpdate' is empty
warning (UNT0001).Note that while it is possible to activate these analyzers, the suppressors they ship with the package (that turn off other C# warnings that may conflict with these custom ones) may not be picked up by OmniSharp at the moment, according to this thread. You can still turn off specific rules manually by following these steps:
- Create a
.editorconfig
file in your project's root folder (next to Unity's.csproj
files). - Add the following contents to the file:
root=true
tells OmniSharp that this is your project root and it should stop looking for parent .editorconfig
files outside of this folder.dotnet_diagnostic.IDE0051.severity = none
is an example of turning off the analyzer with ID IDE0051
by setting its severity level to none
. You can read more about these settings in the Analyzer overview. You can add as many of these rules as you wish to this file.[*.cs]
indicates that our custom rules should apply to all C# scripts (files with the .cs
extension).You are now ready to code in Visual Studio Code, while getting the same warnings as you would when using Visual Studio!
Visual Studio Code Dotnet Version
Next steps
Read on to learn more about:
- Basic Editing - Learn about the powerful VS Code editor.
- Code Navigation - Move quickly through your source code.
- Debugging - how to use the debugger with your project
- C# - learn about the C# support in VS Code
Common questions
I don't have IntelliSense
You need to ensure that your solution is open in VS Code (not just a single file). Open the folder with your solution and you usually will not need to do anything else. If for some reason VS Code has not selected the right solution context, you can change the selected project by clicking on the OmniSharp flame icon on the status bar.
Choose the
-CSharp
version of the solution file and VS Code will light up.How can I change the file exclusions?
Unity creates a number of additional files that can clutter your workspace in VS Code. You can easily hide these so that you can focus on the files you actually want to edit.
To do this, add the following JSON to your workspace settings.
As you can see below this will clean things up a lot..
Before | After |
---|
How can I debug Unity?
Install the Debugger for Unity extension. And check out Debugging with VS Code to learn more about VS Code debugging support.
5/2/2017
-->This tutorial shows how to automate unit testing by adding a test project to a solution.
Prerequisites
- This tutorial works with the solution that you create in Create a .NET class library using Visual Studio Code.
Create a unit test project
Unit tests provide automated software testing during your development and publishing. The testing framework that you use in this tutorial is MSTest. MSTest is one of three test frameworks you can choose from. The others are xUnit and nUnit.
- Start Visual Studio Code.
- Open the
ClassLibraryProjects
solution you created in Create a .NET class library using Visual Studio Code. - Create a unit test project named 'StringLibraryTest'.The project template creates a UnitTest1.cs file with the following code:The source code created by the unit test template does the following:
- It imports the Microsoft.VisualStudio.TestTools.UnitTesting namespace, which contains the types used for unit testing.
- It applies the TestClassAttribute attribute to the
UnitTest1
class. - It applies the TestMethodAttribute attribute to define
TestMethod1
.
Each method tagged with [TestMethod] in a test class tagged with [TestClass] is executed automatically when the unit test is run. - Add the test project to the solution.
Add a project reference
For the test project to work with the
StringLibrary
class, add a reference in the StringLibraryTest
project to the StringLibrary
project.- Run the following command:
Add and run unit test methods
When Visual Studio runs a unit test, it executes each method that is marked with the TestMethodAttribute attribute in a class that is marked with the TestClassAttribute attribute. A test method ends when the first failure is found or when all tests contained in the method have succeeded.
The most common tests call members of the Assert class. Many assert methods include at least two parameters, one of which is the expected test result and the other of which is the actual test result. Some of the
Assert
class's most frequently called methods are shown in the following table:Assert methods | Function |
---|---|
Assert.AreEqual | Verifies that two values or objects are equal. The assert fails if the values or objects aren't equal. |
Assert.AreSame | Verifies that two object variables refer to the same object. The assert fails if the variables refer to different objects. |
Assert.IsFalse | Verifies that a condition is false . The assert fails if the condition is true . |
Assert.IsNotNull | Verifies that an object isn't null . The assert fails if the object is null . |
You can also use the Assert.ThrowsException method in a test method to indicate the type of exception it's expected to throw. The test fails if the specified exception isn't thrown.
In testing the
StringLibrary.StartsWithUpper
method, you want to provide a number of strings that begin with an uppercase character. You expect the method to return true
in these cases, so you can call the Assert.IsTrue method. Similarly, you want to provide a number of strings that begin with something other than an uppercase character. You expect the method to return false
in these cases, so you can call the Assert.IsFalse method.Since your library method handles strings, you also want to make sure that it successfully handles an empty string (
String.Empty
) and a null
string. An empty string is one that has no characters and whose Length is 0. A null
string is one that hasn't been initialized. You can call StartsWithUpper
directly as a static method and pass a single String argument. Or you can call StartsWithUpper
as an extension method on a string
variable assigned to null
.You'll define three methods, each of which calls an Assert method for each element in a string array. You'll call a method overload that lets you specify an error message to be displayed in case of test failure. The message identifies the string that caused the failure.
To create the test methods:
- Open StringLibraryTest/UnitTest1.cs and replace all of the code with the following code.The test of uppercase characters in the
TestStartsWithUpper
method includes the Greek capital letter alpha (U+0391) and the Cyrillic capital letter EM (U+041C). The test of lowercase characters in theTestDoesNotStartWithUpper
method includes the Greek small letter alpha (U+03B1) and the Cyrillic small letter Ghe (U+0433). - Save your changes.
- Run the tests:The terminal output shows that all tests passed.
Handle test failures
If you're doing test-driven development (TDD), you write tests first and they fail the first time you run them. Then you add code to the app that makes the test succeed. For this tutorial, you created the test after writing the app code that it validates, so you haven't seen the test fail. To validate that a test fails when you expect it to fail, add an invalid value to the test input.
- Modify the
words
array in theTestDoesNotStartWithUpper
method to include the string 'Error'. - Run the tests:The terminal output shows that one test fails, and it provides an error message for the failed test: 'Assert.IsFalse failed. Expected for 'Error': false; actual: True'. Because of the failure, no strings in the array after 'Error' were tested.
- Remove the string 'Error' that you added in step 1. Rerun the test and the tests pass.
Test the Release version of the library
Now that the tests have all passed when running the Debug build of the library, run the tests an additional time against the Release build of the library. A number of factors, including compiler optimizations, can sometimes produce different behavior between Debug and Release builds.
- Run the tests with the Release build configuration:The tests pass.
Debug tests
If you're using Visual Studio Code as your IDE, you can use the same process shown in Debug a .NET console application using Visual Studio Code to debug code using your unit test project. Instead of starting the ShowCase app project, open StringLibraryTest/UnitTest1.cs, and select Run All Tests between lines 7 and 8. If you're unable to find it, press Ctrl+Shift+P to open the command palette and enter Reload Window.
Visual Studio Code starts the test project with the debugger attached. Execution will stop at any breakpoint you've added to the test project or the underlying library code.
Additional resources
Next steps
In this tutorial, you unit tested a class library. You can make the library available to others by publishing it to NuGet as a package. To learn how, follow a NuGet tutorial:
If you publish a library as a NuGet package, others can install and use it. To learn how, follow a NuGet tutorial:
A library doesn't have to be distributed as a package. It can be bundled with a console app that uses it. To learn how to publish a console app, see the earlier tutorial in this series: