Unleash the full potential by being in sync with the latest C# versions that are configured individually for each .asmdef
.
-
Editor Patching: The
UnityEditorPatch
is responsible for updating the built-in dotnet SDK within the Unity editor. Unity, by default, ships with dotnet version6.0.21
. -
Language Version Tracking:
UnityPackage
keeps track of the C# language version specified in yourcsc.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
. -
Project File Adjustment: Finally,
UnityPackage
modifies the.csproj
file to reflect the C# version specified in thecsc.rsp
. This alerts your IDE to the correct language version to use, ensuring it can provide you with all the relevant language features.
- Assets folder
- Embedded Packages - everything that is located in Packages/ folder of your project.
- Local Packages - everything that is located anywhere on your pc with specified path with
file:
prefix in manifest.
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)
- Add the package via git url
https://github.com/kandreyc/unity-csharp-patch.git#v1.3.0
- Ensure Unity Editor is closed.
- Ensure latest stable dotnet sdk is installed. Download Page
- Open terminal at folder
EditorPatch~
inside the added package. - 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
- 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 are10
,11
,12
,13
,preview
nullable
(optional) - allows to use nullables likestring?
without defining#nullable enable/disable
in each file where it used. Values areenable
,disable
- Refresh the Editor. All required magic should be done here.
- Enjoy!
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.
This project was inspired and motivated by two key repositories:
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.