mob-sakai / CSharpCompilerSettingsForUnity

Change the C# compiler (csc) used on your Unity project, as you like!
MIT License
289 stars 22 forks source link
csc csharp csharp8 development-tools unity unity3d unity3d-plugin upm upm-package

C# Compiler Settings For Unity

: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!

PRs Welcome

GitHub Workflow Status Test CodeCoverage

<< Description | Installation | Usage | Contributing >>





Description

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!

Features

Feature plans

NOTE: Please do so at your own risk!

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.





Installation

Requirement

via OpenUPM

This package is available on OpenUPM.
After installing openupm-cli, run the following command

openupm add com.coffee.csharp-compiler-settings

via Package Manager

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.

Or, use UpmGitExtension to install and update the package.





Usage

Configure C# compiler settings for the project

  1. Open project setting window. (Edit > Project Settings)
  2. Select C# Compiler tab
  3. Set Compiler Type to Custom Package, to use custom compiler package.
  4. Input Package Name, Package Version, Language Version for compilation.
  5. Press Apply button to save settings.
  6. It will automatically request a recompilation.
    The selected nuget package will be used for compilation.
  7. Enjoy!

The project setting asset for C# Compiler will be saved in ProjectSettings/CSharpCompilerSettings.asset.



Configure C# compiler settings for *.asmodef file

  1. Select a *.asmodef file
  2. Turn on Enable C# Compilier Settings to configure.
  3. Set Compiler Type to Custom Package, to use custom compiler package.
  4. Input Package Name, Package Version, Language Version and Modify Symbols for compilation.
  5. Press 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.



For C# 8.0 features

C# 8.0 features
samples

If you want to use the C# 8.0 features, set it up as follows:

Some features required external library.

NOTE: Default interface methods feature is not available. It requires .Net Standard 2.1.



For C# 9.0 features

C# 9.0 features
samples

If you want to use the C# 9.0 features, set it up as follows:

NOTE: Some features is not available. It requires .Net 5.



For C# 10.0 features

C# 10.0 features
samples

If you want to use the C# 10.0 features, set it up as follows:

NOTE: Some features is not available. It requires .Net 6.





Development Notes

How to update CSharpCompilerSettings



For Unity 2021.1 or later

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
        }
    }
}



How to include the asmdef in Packages directory

By 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.

image

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.





Contributing

Issues

Issues are very valuable to this project.

Pull Requests

Pull requests are, a great way to get your ideas into this repository.
See CONTRIBUTING.md and sandbox branch.

Support

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. :)






License

Author

See Also