C# 7.2 support for Unity 2017.2+

Tested on

  • Unity 2017.2.1p1, 2017.3.0p1, 2018.1.0b12

  • Windows 10 x64 (editor + mono and il2cpp builds)

  • macOS 10.12 (editor + mono build)

  • Android 7.1.1 (mono and il2cpp builds)

  • Visual Studio 2017 15.7.0 Preview 1.0

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.2.

How to install

  1. Download CSharpVNextSupport 4.0.4 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 recreate them.

  6. [Optional] On Windows, run CSharp70Support/ngen install.cmd once with administrator privileges. It will precompile csc.exe and pdb2mdb.exe using Ngen. It 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?


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