Change the C# compiler (csc) used on your Unity project, as you like!
<< Description | Installation | Usage | Contributing >>
A good news! Unity 2020.2.0a12 or later now supports C# 8.0!
Many developers (including you!) have been eagerly awaiting support for C# 8.0.
C# 8.0 includes some features and some useful syntax-sugars:
- Readonly members
- Default interface methods
- Pattern matching enhancements:
- Switch expressions
- Property patterns
- Tuple patterns
- Positional patterns
- Using declarations
- Static local functions
- Disposable ref structs
- Nullable reference types
- Asynchronous streams
- Asynchronous disposable
- Indices and ranges
- Null-coalescing assignment
- Unmanaged constructed types
- Stackalloc in nested expressions
- Enhancement of interpolated verbatim strings
However, unfortunately, there are no plans to backport to Unity 2020.1 or earlier...
This package changes the C# compiler (csc) used on your Unity project, to support C# 8.0.
Let's enjoy C# 8.0 features with your Unity project!
- Easy to use.
- This package is out of the box!
- Change the C# compiler (csc) used on your Unity project.
- Change the nuget package name.
- Microsoft.Net.Compilers: Official compiler (default, run on Unity built-in mono)
- Microsoft.Net.Compilers.Toolset: Official compiler (run on dotnet)
- Resolve the issue #2
- OpenSesame.Net.Compilers: Allows access to internals/privates in other assemblies (run on Unity built-in mono)
- OpenSesame.Net.Compilers.Toolset: Allows access to internals/privates in other assemblies (run on dotnet)
- Resolve the issue #2
- Or, your custom nuget package
- Change the nuget package version.
- 3.4.0: C# 8.0 Supported.
- 3.5.0: C# 8.0 Supported. (default in Unity 2020.2.0)
- 3.6.0: C# 8.0 Supported.
- 3.7.0: C# 8.0 Supported.
- 3.8.0 (preview): C# 9.0 Supported.
- For other versions, see the nuget package page above.
- Change the C# language version.
- 7.0
- 7.1
- 7.2
- 7.3
- 8.0 (latest, default in Unity 2020.2.0)
- 9.0 (preview)
- Change the nuget package name.
- Add the scripting define symbols based on language version on compiling.
- e.g.
CSHARP_7_3_OR_LATER,CSHARP_8_OR_LATER,CSHARP_9_OR_LATER
- e.g.
- Change the C# compiler settings for each
*.asmdeffile.- Portability: The assembly works even in the projects that do not have this package installed.
- The best option when distributing as a package.
- Publish as Dll: Published dll works without this package.
- Modify the scripting define symbols for each
*.asmdeffile.- Add/Remove specific symbols separated with semicolons (
';') or commas (','). - The symbols starting with
'!'will be removed. - e.g.
SYMBOL_TO_ADD;!SYMBOL_TO_REMOVE;...
- Add/Remove specific symbols separated with semicolons (
- Portability: The assembly works even in the projects that do not have this package installed.
- Modify
langversionproperty in *.csproj file. - If
dotnetis required, install it automatically. CompilerType.BuiltIncompiler option to disable this plugin.Enable Loggingoption to display compilation log.Nullableoption to enable Nullable reference types (C# 8.0).enable: The nullable annotation context is enabled. The nullable warning context is enabled.- Variables of a reference type, string for example, are non-nullable. All nullability warnings are enabled.
warnings: The nullable annotation context is disabled. The nullable warning context is enabled.- Variables of a reference type are oblivious. All nullability warnings are enabled.
annotations: The nullable annotation context is enabled. The nullable warning context is disabled.- Variables of a reference type, string for example, are non-nullable. All nullability warnings are disabled.
disable: The nullable annotation context is disabled. The nullable warning context is disabled.- Variables of a reference type are oblivious, just like earlier versions of C#. All nullability warnings are disabled.
Analyzeroption to analyze your code on compile.
- Add a dropdown menu to select version.
- Verify the selected pakcage name and version.
- Show package description.
https://forum.unity.com/threads/unity-c-8-support.663757/page-2#post-5738029
Unity doesn't support using your own C# compiler. While this is possible, it is not something I would recommend, as we've not tested customer C# compiler versions with Unity.
- Unity 2018.3 or later
This package is available on OpenUPM.
After installing openupm-cli, run the following command
openupm add com.coffee.csharp-compiler-settings
Find the manifest.json file in the Packages directory in your project and edit it to look like this:
{
"dependencies": {
"com.coffee.csharp-compiler-settings": "https://github.com/mob-sakai/CSharpCompilerSettingsForUnity.git",
...
},
}
To update the package, change suffix #{version} to the target version.
- e.g.
"com.coffee.csharp-compiler-settings": "https://github.com/mob-sakai/CSharpCompilerSettingsForUnity.git#1.0.0",
Or, use UpmGitExtension to install and update the package.
- Open project setting window. (
Edit > Project Settings)

- Select
C# Compilertab - Set
Compiler TypetoCustom Package, to use custom compiler package.

- Input
Package Name,Package Version,Language Versionfor compilation.- See features section.
- Press
Applybutton to save settings. - It will automatically request a recompilation.
The selected nuget package will be used for compilation. - Enjoy!
The project setting asset for C# Compiler will be saved in ProjectSettings/CSharpCompilerSettings.asset.
{
"m_UseDefaultCompiler": false,
"m_Version": 110,
"m_CompilerType": 1,
"m_PackageName": "Microsoft.Net.Compilers.Toolset",
"m_PackageVersion": "3.8.0-3.final",
"m_LanguageVersion": 2147483646,
"m_EnableLogging": true,
"m_ModifySymbols": ""
}- Select a
*.asmodeffile - Turn on
Enable C# Compilier Settingsto configure.

- Set
Compiler TypetoCustom Package, to use custom compiler package. - Input
Package Name,Package Version,Language VersionandModify Symbolsfor compilation.- See features section.
- Press
Applybutton to save settings.
Reload: Reload C# compiler settings dll for the assembly.
Publish as dll: Publish the assembly as dll to the parent directory.
If you want to use the C# 8.0 features, set it up as follows:
- Package Name: Microsoft.Net.Compilers
- Package Version: 3.5.0 or later
- Language Version: latest or
CSharp_8
Some features required external library.
- Async stream -> UniTask v2
- Install to project.
- Indices and ranges -> IndexRange
- Download nuget package and extract it.
- Import
lib/netstandard2.0/IndexRange.dllto project.
- Stackalloc in nested expressions -> System.Memory
- Download nuget package and extract it.
- Import
lib/netstandard2.0/System.Memory.dllto project.
NOTE: Default interface methods feature is not available. It requires .Net Standard 2.1.
If you want to use the C# 9.0 features, set it up as follows:
- Package Name: Microsoft.Net.Compilers
- Package Version: 3.8.0-2.final or later
- Language Version: preview
NOTE: Some features is not available. It requires .Net 5.
Issues are very valuable to this project.
- Ideas are a valuable source of contributions others can make
- Problems show where this project is lacking
- With a question you show where contributors can improve the user experience
Pull requests are, a great way to get your ideas into this repository.
See CONTRIBUTING.md and sandbox branch.
This is an open source project that I am developing in my spare time.
If you like it, please support me.
With your support, I can spend more time on development. :)
- MIT
- GitHub page : https://github.com/mob-sakai/CSharpCompilerSettingsForUnity
- Releases : https://github.com/mob-sakai/CSharpCompilerSettingsForUnity/releases
- Issue tracker : https://github.com/mob-sakai/CSharpCompilerSettingsForUnity/issues
- Change log : https://github.com/mob-sakai/CSharpCompilerSettingsForUnity/blob/master/CHANGELOG.md
- Test Space : https://mob-sakai.testspace.com/spaces/130862/current


