Unleash the full potential by being in sync with the latest C# versions that are configured individually for each .asmdef.
-
Editor Patching: The
UnityEditorPatchis responsible for updating the built-in dotnet SDK within the Unity editor. Unity, by default, ships with dotnet version6.0.21. -
Language Version Tracking:
UnityPackagekeeps track of the C# language version specified in yourcsc.rspfile 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,
UnityPackagemodifies the.csprojfile 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.
- Editor OS support: Mac / Linux / Windows.
- Target OS support: Mac / Windows / Linux / iOS / Android / WebGL.
- IDE support: Rider / VSCode / Visual Studio.
- Project - everything that is located in
Assets/folder of your project. - 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.
Warning
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)
DON'T put .asmdef with its csc.rsp at the root of the project (Assets/).
Unity in that case will apply the specified C# version to everything, that may cause compile errors (e.g. field keyword is used as a name in some third party library).
Place it in any subfolder (usually Scripts, but doesn't matter, just not at the root)
Opening the editor in safe mode will not apply the patch, so you may see a lot of errors in IDE only. It will be applied when you will exit the safe mode, so when all compile errors from editor console are fixed.
- Add the package via git url
https://github.com/kandreyc/unity-csharp-patch.git#v1.6.0or download latest package from releases - Ensure Unity Editor is closed.
- Ensure latest dotnet sdk is installed. Download Page
- Open terminal at folder
EditorPatch~inside the added package. - Patch the editor (administrative privileges are required):
Mac / Linux:
$ dotnet UnityEditorPatch.dll apply --editor '/Applications/Unity/Hub/Editor/2022.3.21f1' --allow-prerelease
Windows:
$ dotnet UnityEditorPatch.dll apply --editor "C:/Program Files/Unity/Hub/Editor/2022.3.21f1" --allow-prerelease
where:
--editor- path to the unity editor--allow-prerelease- (optional) allows to use prerelease dotnet sdk
In case if you want to revert the patch:
Mac / Linux:
$ dotnet UnityEditorPatch.dll revert --editor '/Applications/Unity/Hub/Editor/2022.3.21f1'
Windows:
$ dotnet UnityEditorPatch.dll revert --editor "C:/Program Files/Unity/Hub/Editor/2022.3.21f1"
where:
--editor- path to the unity editor
- Open the Unity Editor with your project and create a
csc.rspfile alongside desired.asmdefwith the following content:
-langVersion:14
-nullable:enable
where:
langVersion(optional) - C# version you want to be used for this.asmdef. Values are10,11,12,13,14nullable(optional) - allows to use nullables likestring?without defining#nullable enable/disablein 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.