C# 7.2 support for Unity 2017.2+
Unity 2017.2.1p1, 2017.3.0p1, 2018.1.0b3
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.6 Preview 2.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
CSharpVNextSupport 4.0.2archive 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 recreate them.
[Optional] On Windows, run
CSharp70Support/ngen install.cmdonce with administrator privileges. It will precompile csc.exe and pdb2mdb.exe using Ngen, that will make compilation process in Unity a bit faster.
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.
All the source code is published under WTFPL version 2.
Want to talk about it?
- 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).