C# 7.3 support for Unity 2017.2+

What is it

This is a hack that replaces Unity's C# compiler with the latest Roslyn, which lets you write scripts in Unity using C# 7.3.

If you already use this

For Unity prior to 2018.3 have a look at official Unity Incremental C# Compiler.

Unity 2018.3 supports C# 7.3 natively.

How to install

  1. Download CSharpVNextSupport 4.1.1 archive from the downloads page.

  2. Unpack CSharp70Support folder from the archive into your Unity project's root. It should be placed next to the Assets folder, not inside it.

  3. Import CSharp70Support/CSharpVNextSupport.unitypackage into the project.

  4. If you see any compilation errors at this point, restart the editor or run Reimport All.

  5. Delete all the .csproj files in the project folder to force Unity to recreate them.

  6. [Optional] If on Windows, run CSharp70Support/ngen install.cmd once with administrator privileges. It will precompile csc.exe and pdb2mdb.exe using Ngen, which will shave off a couple of seconds of compilation time per each C# project.

How it works

CSharpVNextSupport.dll is an editor extension that does two main things:

  1. Once loaded into the memory, it patches Unity editor code on the fly using Harmony, so that the editor would run CSharpCompilerWrapper.exe instead of the stock Mono C# compiler. CSharpCompilerWrapper.exe then redirects the compilation requests to Roslyn compiler and, if compilation is successfull, converts pdb-files that Roslyn produces to mdb format that Unity can consume. Note, that the patch is applied in memory, no Unity files will be modified, other Unity projects won't be affected in any way.

  2. It also applies the following changes to all .csproj files each time they are regenerated by Unity:

    • enables unsafe code,

    • sets C# language version to latest if Roslyn is found or to default if not,

    • adds a reference to Microsoft.CSharp.dll to enable 'dynamic' feature of C# 4.0,

    • removes references to Boo and UnityScript assemblies (just for Visual Studio, you will no longer see their stuff in the Intellisense dropdown menu),

    • sets the TargetFramework to 4.7 (for C# 7.0 tuples to work out of the box without extra reference to System.ValueTuple.dll from NuGet).

Files in AsyncTools folder are completely optional. They mostly contain various awaiters to make asynchronous code in Unity a bit easier to write. This stuff is used by the demo project, but may be useful in other projects too.

macOS limitations

  • You will not be able to debug code compiled with Roslyn compiler on macOS.

  • Line numbers in Unity console messages will not be available.

NOTE: Resolved with introducing Portable PDB support in Unity 2018.1.0b12


All the source code is published under WTFPL version 2.

Want to talk about it

Unity forum thread


CSharpVNextSupport 4.1.1

- Unity 2018.2.16+ support
- Updated Roslyn to v2.10.0.0

CSharpVNextSupport 4.1.0

- Disabled Portable PDB for legacy NET 3.5 projects
- Added UnityTaskScheduler class and AsyncTools.MainThreadScheduler static property
- Updated Roslyn to v2.8.0.62830 (supports C# 7.3)
- Added C# 7.3 feature tests

CSharpVNextSupport 4.0.4

- Switched from regular PDB to Portable PDB on all platforms for Unity 2018.1.0b12 or newer

CSharpVNextSupport 4.0.3

- Unity 2018.1.0b10 support
- Updated Roslyn to v2.7.0.62620

CSharpVNextSupport 4.0.2

- Fixed macOS support

CSharpVNextSupport 4.0.1

- Added missing reference to System.Xml.Linq.dll

CSharpVNextSupport 4.0.0

- Unity 2018.1 support

- The integration mechanism was changed. These's no longer need to mess with files
in Unity installation folder. UnityEditor.dll is patched in memory using Harmony (