:warning: NOTE: Do not use the obsolete tags and branches to reference the package. They will be removed in near future. :warning:
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:
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, C# 9.0 and more.
Let's enjoy new C# features with your Unity project!
CSHARP_7_3_OR_LATER
, CSHARP_8_OR_LATER
, CSHARP_9_OR_LATER
...*.asmdef
file.
*.asmdef
file.';'
) or commas (','
).'!'
will be removed.SYMBOL_TO_ADD;!SYMBOL_TO_REMOVE;...
langversion
property in *.csproj file.dotnet
is required, it will be installed automatically.CompilerType.BuiltIn
compiler option to disable this plugin.Enable Logging
option to display compilation log.Nullable
option to enable Nullable reference types (C# 8.0).
enable
: The nullable annotation context is enabled. The nullable warning context is enabled.warnings
: The nullable annotation context is disabled. The nullable warning context is enabled.annotations
: The nullable annotation context is enabled. The nullable warning context is disabled.disable
: The nullable annotation context is disabled. The nullable warning context is disabled.Analyzer/Source Generator Packages
option to analyze/generate your code on compile.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.
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.
"com.coffee.csharp-compiler-settings": "https://github.com/mob-sakai/CSharpCompilerSettingsForUnity.git#1.0.0",
Or, use UpmGitExtension to install and update the package.
Edit > Project Settings
)C# Compiler
tabCompiler Type
to Custom Package
, to use custom compiler package.Package Name
, Package Version
, Language Version
for compilation.
Apply
button to save settings.The project setting asset for C# Compiler will be saved in ProjectSettings/CSharpCompilerSettings.asset
.
*.asmodef
file*.asmodef
fileEnable C# Compilier Settings
to configure.Compiler Type
to Custom Package
, to use custom compiler package.Package Name
, Package Version
, Language Version
and Modify Symbols
for compilation.
Apply
button 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:
CSharp_8
Some features required external library.
lib/netstandard2.0/IndexRange.dll
to project.lib/netstandard2.0/System.Memory.dll
to 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:
CSharp_9
NOTE: Some features is not available. It requires .Net 5
.
If you want to use the C# 10.0 features, set it up as follows:
CSharp_10
NOTE: Some features is not available. It requires .Net 6
.
Project Settings > C# Compoler > Reload
to reload all CSharpCompilerSettings_*.dll
assemblies In Unity 2021.1 or later, the compile flow has changed significantly. (#11)
The source code must pass both the built-in compiler and the custom compiler.
Please use the CUSTOM_COMPILE
directive as follows:
using System;
using NUnit.Framework;
namespace IgnoreAccessibility
{
public class IgnoreAccessibilityContent
{
private int privateNumber = 999;
}
public class IgnoreAccessibilityTest
{
[Test]
public void GetPrivateField()
{
#if CUSTOM_COMPILE
Assert.AreEqual(new IgnoreAccessibilityContent().privateNumber, 999);
#else
throw new NotImplementedException();
#endif
}
}
}
Packages
directoryBy default, asmdefs under Packages
and Assets/Standard Assets/
are NOT compiled with the custom compiler package.
This is to prevent unintended compiler changes and to minimize the impact of compiler changes.
Packages (and store assets) should essentially be compiled with the default compiler.
If you want to compile an embedded package or your custom package with a custom compiler, set Packages/your_package_name/
instead of !Packages/
.
See #12 for details.
Issues are very valuable to this project.
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. :)