HTTPS SSH

C# 7.2 support for Unity 2017.2+

Tested on

  • 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

  1. Download CSharpVNextSupport 4.0.2 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, that will make compilation process in Unity a bit faster.

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.

License

All the source code is published under WTFPL version 2.

Want to talk about it?

http://forum.unity3d.com/threads/c-6-0.314297/#post-2108999

History

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
(https://github.com/pardeike/Harmony).