Skip to content

The goal of this project is to unlock latest C# features in Unity

License

Notifications You must be signed in to change notification settings

kandreyc/unity-csharp-patch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unity C# Patch

release Static Badge

Unleash the full potential by being in sync with the latest C# versions that are configured individually for each .asmdef.

How it works:

  1. Editor Patching: The UnityEditorPatch is responsible for updating the built-in dotnet SDK within the Unity editor. Unity, by default, ships with dotnet version 6.0.21.

  2. Language Version Tracking: UnityPackage keeps track of the C# language version specified in your csc.rsp file that is located alongside each .asmdef. This is required to help Unity to understand which C# version it should use while compiling the code of your .asmdef.

  3. Project File Adjustment: Finally, UnityPackage modifies the .csproj file to reflect the C# version specified in the csc.rsp. This alerts your IDE to the correct language version to use, ensuring it can provide you with all the relevant language features.

Supported AssemblyDefinition locations:

  1. Assets folder
  2. Embedded Packages - everything that is located in Packages/ folder of your project.
  3. Local Packages - everything that is located anywhere on your pc with specified path with file: prefix in manifest.

How to Install:

Note

OS support: Mac/Linux/Windows. You can backup your editor just in case, but should be fine.

Patch will modify the Editor installation, so all the projects that are using it will be affected (default for unity C# version will be used for compilation, but from newer (patched) dotnet sdk)

  1. Add the package via git url https://github.com/kandreyc/unity-csharp-patch.git#v1.3.0
  2. Ensure Unity Editor is closed.
  3. Ensure latest stable dotnet sdk is installed. Download Page
  4. Open terminal at folder EditorPatch~ inside the added package.
  5. Patch the editor (administrative privileges are required):
$ dotnet UnityEditorPatch.dll apply --editor '/Applications/Unity/Hub/Editor/2022.3.21f1'

where --editor - path to the unity editor

In case if you want to revert the patch:

$ dotnet UnityEditorPatch.dll revert --editor '/Applications/Unity/Hub/Editor/2022.3.21f1'

where --editor - path to the unity editor

  1. Open the Unity Editor with your project and create a csc.rsp file alongside desired .asmdef with the following content:
-langVersion:13
-nullable:enable

where:

  • langVersion (optional) - C# version you want to be used for this .asmdef. Values are 10, 11, 12, 13, preview
  • nullable (optional) - allows to use nullables like string? without defining #nullable enable/disable in each file where it used. Values are enable, disable
  1. Refresh the Editor. All required magic should be done here.
  2. Enjoy!

Language Support

C# Feature Support
preview The field keyword Yes
13 params collections Yes
13 New lock type and semantics No
13 New escape sequence - \e Yes
13 Method group natural type improvements Yes
13 Implicit indexer access in object initializers Yes
13 Enable ref locals and unsafe contexts in iterators and async methods Yes
13 Enable ref struct types to implement interfaces No
13 Allow ref struct types as arguments for type parameters in generics No
13 Partial properties and indexers Yes
13 Overload resolution priority PolySharp
12 Primary constructors Yes
12 Optional parameters in lambda expressions Yes
12 Alias for any type Yes
12 Inline arrays No
12 Collection expressions Yes
12 Interceptors No
11 Raw string literals Yes
11 static abstract/static virtual members in interfaces No
11 Checked user defined operators Yes
11 Relaxed shift operators Yes
11 Unsigned right-shift operator Yes
11 Generic attributes Crash
11 UTF-8 string literals Yes
11 Newlines in string interpolations Yes
11 List patterns Yes
11 File-local types Yes
11 Required members PolySharp
11 Auto-default structs Yes
11 Pattern match Span<char> or ReadOnlySpan<char> on a constant string Yes
11 Extended nameof scope Yes
11 Numeric IntPtr and UIntPtr Yes
11 ref fields No
11 ref scoped variables PolySharp
11 Improved method group conversion to delegate Yes
10 Record structs Yes
10 Improvements of structure types Yes
10 Interpolated string handler PolySharp
10 Global using directives Yes
10 File-scoped namespace declaration Yes
10 Extended property patterns Yes
10 Lambda expression improvements Yes
10 Constant interpolated strings Yes
10 Record types can seal ToString Yes
10 Assignment and declaration in same deconstruction Yes
10 Improved definite assignment Yes
10 CallerArgumentExpression attribute PolySharp
10 Enhanced #line pragma Yes

Support:

  • Yes - feature works exactly as expected.
  • No - requires runtime features or BCL changes that Unity does not have. Attempting to use the feature may result in compiler errors.
  • Crash - requires runtime features that Unity does not have. Attempting to use the feature may compile, but will result in crashes.
  • PolySharp - feature works when using PolySharp and/or manually implementing missing APIs.

Motivation

This project was inspired and motivated by two key repositories:

  1. CsprojModifier
  2. UnityRoslynUpdater

While the UnityRoslynUpdater serves its purpose by upgrading the C# version across all projects, my goal was to allow using custom C# version only where it is required. So that my package gives the control of what assemblies should be allowed to use newer C# version, which helps to prevent naming conflicts with embedded/thirdparty libs/sdks, or affect the projects that you don't want to

Inspired by CsprojModifier, I've automated modifying .csproj files based on .asmdef properties, making it possible for your IDE to use the newest C# features.

About

The goal of this project is to unlock latest C# features in Unity

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages