Hi,
I just read this blog post about Generic Attributes in C# 11 here https://prographers.com/Blog/first_class_support_for_attributes_in_cs11 and I wonder how I can enable C# 11 preview in Unity. So that I can use this feature as it would be very useful for my use case?
Thank you
Is it even supported in Unity? Start there… check the main Unity technical docs and official blogs, not some random blogger.
that could cause unity to crash unexpectedly if you can even call it that, its like using a Windows 11 sdk to program a Windows 10 app. some functions and stuff will not be available in the older version, in this case Unity’s version of c# may only be 10.0 which would result in
X does not exist in the current context
the namespace X could not be found
could not import X
could not find X
and so on.
so the answer, check unity for c# version, if its below 11.0, then dont touch anything.
You absolutely can use the csharp 11 compiler features, but NOT the more modern dotnet features that come with it (ref fields, static abstract, etc)
ZLogger provides a tutorial in their installation guide: GitHub - Cysharp/ZLogger: Zero Allocation Text/Structured Logger for .NET with StringInterpolation and Source Generator, built on top of a Microsoft.Extensions.Logging.
Prerequirements:
Install NuGetForUnity
Required to install the dlls of ZLogger and its dependencies.
Install CsprojModifier
Required to develop in the IDE with a new language version.
Install ZLogger.Unity package via git url.
Add https://github.com/Cysharp/ZLogger.git?path=src/ZLogger.Unity/Assets/ZLogger.Unity to Package Manager
Installation steps:
Setup the C# compiler for unity.
Add a text file named csc.rsp with the following contents under your Assets/.
-langVersion:10 -nullable
Note:
If you are using assembly definition, put it in the same folder as the asmdef that references ZLogger.
If you are using Unity 2022.3.12f1 or newer, you can use langVersion:preview allows parts of C# 11 features.
Setup the C# compiler for your IDE.
Add a text file named LangVersion.props with the following contents
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Open Project Settings and [C# Project Modifier] section under the [Editor].
Add the .props file you just created, to the list of [Additional project imports].
Note:
If you are using assembly definition, add your additional csproj in the list of [The project to be addef for import].
If you want to use ZLoggerMessage Source Generator, require Unity 2022.3.12f1 and change to <LangVersion>11</LangVersion>
2 Likes