• Stars
    star
    287
  • Rank 144,232 (Top 3 %)
  • Language
    C#
  • License
    MIT License
  • Created over 4 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Change the C# compiler (csc) used on your Unity project, as you like!

C# Compiler Settings For Unity

⚠️ NOTE: Do not use the obsolete tags and branches to reference the package. They will be removed in near future. ⚠️

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:

  • 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, C# 9.0 and more.
Let's enjoy new C# features with your Unity project!

Features

  • 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.
    • 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.
      • 3.9.0: C# 9.0 Supported. (default in Unity 2021.2.0)
      • 3.10.0: C# 9.0 Supported.
      • 3.11.0: C# 9.0 Supported.
      • 4.0.1: C# 10.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 (default in Unity 2020.2.0)
      • 9.0 (default in Unity 2021.2.0)
      • 10.0 (latest)
  • 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 ...
  • Change the C# compiler settings for each *.asmdef file.
    • 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 *.asmdef file.
      • Add/Remove specific symbols separated with semicolons (';') or commas (',').
      • The symbols starting with '!' will be removed.
      • e.g. SYMBOL_TO_ADD;!SYMBOL_TO_REMOVE;...
  • Modify langversion property in *.csproj file.
  • If 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.
      • 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.
  • Analyzer/Source Generator Packages option to analyze/generate your code on compile.

Feature plans

  • Show package description.

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

  • Unity 2018.3 or later

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.

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





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:

  • 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.dll to project.
  • Stackalloc in nested expressions -> System.Memory
    • Download nuget package and extract it.
    • Import lib/netstandard2.0/System.Memory.dll to project.

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:

  • Package Name: Microsoft.Net.Compilers
  • Package Version: 3.9.0 or later
  • Language Version: latest or CSharp_9

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:

  • Package Name: Microsoft.Net.Compilers.Toolset
  • Package Version: 4.0.1 or later
  • Language Version: latest or CSharp_10

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





Development Notes

How to update CSharpCompilerSettings

  • See installation section to update package
  • Click Project Settings > C# Compoler > Reload to reload all CSharpCompilerSettings_*.dll assemblies



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.

  • 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

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

  • MIT

Author

See Also

More Repositories

1

UIEffect

UIEffect is an effect component for uGUI element in Unity. Let's decorate your UI with effects!
C#
5,584
star
2

ParticleEffectForUGUI

Render particle effect in UnityUI(uGUI). Maskable, sortable, and no extra Camera/RenderTexture/Canvas.
C#
4,182
star
3

SoftMaskForUGUI

Enhance Unity UI (uGUI) with advanced soft-masking features to create more visually appealing effects!
C#
1,973
star
4

UnmaskForUGUI

A reverse masking solution for uGUI element in Unity.
C#
811
star
5

UpmGitExtension

This package extends the UI of Unity Package Manager (UPM) for the packages installed from git repository.
C#
764
star
6

ShinyEffectForUGUI

Shiny effect of uGUI, which does not need mask or normal map.
C#
380
star
7

GitDependencyResolverForUnity

This plugin resolves git url dependencies in the package for Unity Package Manager. You can use a git url as a package dependency!
C#
249
star
8

StaticBluredScreen

Non-realtime (=static) screen blur for Unity. Easy to use background for dialogs.
C#
189
star
9

MeshEffectForTextMeshPro

Mesh effect components for TextMeshPro in Unity.
C#
153
star
10

UIEffectSnapshot

Capture a screenshot with effect and display it. Light-weight, non-realtime, no-camera (and no-PostProcessingStack), but be effective enough. The captured snapshot can be used as a background for a UI panel.
C#
142
star
11

CompositeCanvasRenderer

CompositeCanvasRenderer bakes multiple source graphics into a bake-buffer (RenderTexture) and renders it. Additional material modification, mesh modification, and baking effects are supported! Let's enjoy the effects that were difficult to implement with UI shaders, such as blur, soft outline, and soft shadow!
C#
136
star
12

AtlasImage

AtlasImage is a graphic component use SpriteAtlas for uGUI. In addition, add useful sprite selector and border editor to the inspector.
C#
130
star
13

MirrorReflectionEffectForUGUI

A simple mirror reflection effect for a uGUI without reflection probes or shaders.
C#
118
star
14

DissolveEffectForTMPro

DissolveEffectForTMPro provide dissolve effect component for TextMeshPro in Unity.
C#
109
star
15

SubAssetEditor

Editor for SubAsset in unity project.
C#
106
star
16

OpenSesameCompilerForUnity

A Roslyn compiler to access internals/privates for Unity. In other words, you can access to any internals/privates in other assemblies, without reflection. Let's say, "Open sesame!"
C#
102
star
17

ProjectBuilder

A tool for easy automating and customizing build process for Unity.
C#
97
star
18

UnityEventDrawerEx

This plugin extends the UnityEventDrawer to display runtime calls in the inspector.
C#
94
star
19

ShinyEffectForTMPro

ShinyEffectForTMPro provide shiny effect component for TextMeshPro in Unity.
C#
93
star
20

UIMaterialPropertyInjector

This package provides a component that allows easy modification/animation of material properties for Unity UI (uGUI) without the need for shader-specific custom components.
C#
91
star
21

unity-activate

A tool to activate Unity license.
TypeScript
84
star
22

DissolveEffectForUGUI

A dissolve effect for uGUI, without material instancing. Please star and watch this project :)
C#
79
star
23

UITransition

C#
56
star
24

CompositeToggle

Composite toggle system for unity
C#
46
star
25

ButtonEx

C#
44
star
26

BlurringAtlasForUGUI

A blur effect for uGUI in Unity, that is effective even for atlas images including dynamic fonts.
C#
42
star
27

docker-unity

New series of CI-specialised docker images for Unity. Superseding the famous gableroux/unity3d images.
Dockerfile
34
star
28

SymbolCatalog

A tool for edit script symbol define for Unity.
C#
34
star
29

MainWindowTitleModifierForUnity

A demo to change the text displayed in UnityEditor's title bar
C#
32
star
30

unity-changeset

Get/List Unity editor changeset
TypeScript
31
star
31

OpenSesame

A custom Roslyn compiler that allows access to internals/privates in other assemblies. Say "Open, Sesame!"
C#
30
star
32

SimpleBuildInterface

A simple build interface for Unity CLI. Support all platforms without executeMethod option.
C#
25
star
33

RotateMe

Rotate the object. (This repo is a tutorial for "How to develop a package for UnityPackageManager")
Shell
24
star
34

unity.package-manager.metadata

Contains metadata used by the com.unity.package-manager package to fulfill client requests
Shell
24
star
35

InternalAccessibleCompilerForUnity

Compile a c# project to a internal accessible dll (for Unity)
C#
22
star
36

UISystem

C#
20
star
37

ScrollEx

C#
16
star
38

AssetSystem

C#
15
star
39

UnityBuiltinShaders

Unity's built-in shaders.
15
star
40

VertexTextureFetchForUGUI

C#
11
star
41

InternalAccessibleCompiler

Compile a c# project to a internal accessible dll
C#
8
star
42

UniIx

Interactive Extensions for Unity for Unity
C#
7
star
43

JobcanExtensionForChrome

JavaScript
7
star
44

Demos

6
star
45

git-snapshot

git-snapshot is a command-line tool to take a snapshot of the directory and creates/updates another branch, like git subtree split --squash.
JavaScript
5
star
46

Coffee.Internal

Tools and snippets for package development
C#
5
star
47

unity-changeset-action

[GitHub Action] Get Unity editor change set for specific version
JavaScript
5
star
48

AtlasSystem

Atlas system for Unity
C#
4
star
49

UnityAdventCalendar2021

https://qiita.com/items/37978a030d6a6e262065
C#
3
star
50

ApiSystem

C#
3
star
51

mob-sakai.github.io

3
star
52

mob-sakai

2
star
53

unity-list-action

[GitHub Action] Get Unity editor versions for next jobs
JavaScript
2
star
54

actionsflow-workflow-test

2
star
55

UnityGitDependencyTest

A test repo for GitDependencyResolverForUnity
C#
1
star