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
CSharpVNextSupport 4.1.1archive from the downloads page.
CSharp70Supportfolder from the archive into your Unity project's root. It should be placed next to the
Assetsfolder, not inside it.
CSharp70Support/CSharpVNextSupport.unitypackageinto the project.
If you see any compilation errors at this point, restart the editor or run
Delete all the .csproj files in the project folder to force Unity to recreate them.
[Optional] If on Windows, run
CSharp70Support/ngen install.cmdonce 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:
Once loaded into the memory, it patches Unity editor code on the fly using Harmony, so that the editor would run
CSharpCompilerWrapper.exeinstead of the stock Mono C# compiler.
CSharpCompilerWrapper.exethen redirects the compilation requests to Roslyn compiler and, if compilation is successfull, converts
pdb-files that Roslyn produces to
mdbformat 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.
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
latestif Roslyn is found or to
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).
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.
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 2018.2.16+ support - Updated Roslyn to v18.104.22.168
- Disabled Portable PDB for legacy NET 3.5 projects - Added UnityTaskScheduler class and AsyncTools.MainThreadScheduler static property - Updated Roslyn to v22.214.171.124830 (supports C# 7.3) - Added C# 7.3 feature tests
- Switched from regular PDB to Portable PDB on all platforms for Unity 2018.1.0b12 or newer
- Unity 2018.1.0b10 support - Updated Roslyn to v126.96.36.199620
- Fixed macOS support
- Added missing reference to System.Xml.Linq.dll
- 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 (https://github.com/pardeike/Harmony)