Which .net version unity supports importing dlls?

I have been searching for answers couple of days on the internet and couldn’t find a satisfactory answer. I am building a dll for unity as a managed plugin. Apparently .Net version is very important when creating one. And the versions of the .Net is very confusing. I know some of them are standards not a framework but when you want to build a class library in visual studio it lists every .Net there is. I have used .Net Standard 2.1, .Net 8, .Net 5 and they apparently are considered as frameworks according to Visual studios dropdown menu which complicates things even further for the beginners in dll work like me.

Then I have found a somewhat answer from a year ago that unity uses .Net Framework 4.7, so i switched it to it and it worked like a charm. Only problem was some of the coding snippets were not supported by it so i had to change them. This look very strange to me because before i wanted to convert my source code into dll, unity had no problem compiling the code whatsoever. But turning the code into dll you have to downgrade? Now that I don’t understand.

As conclusion I would like to know if there is any source that i can keep track of which version of .Net unity uses without confusing myself.

Edit: Using Unity version 2022.3.22f1

Version details are found here: Unity - Manual: .NET profile support

It depends on your Player Settings API Compatibility level which is either .NET Framework or .NET Standard.

1 Like

try .net 4.5

Okay i think i started things more clearly. But one thing is not clear. How come when i use a code piece like
List<int> someInts = new()
is compiled by unity and can’t be compiled by Visual studio that’s outside of unity with given the latest supported version of .Net? I can understand .Net 4.x doesnt have this feature but how come we are able to write code with unsupported c# version? Hope i described it clearly

Already tried .net framework 4.7 and it works. My problem was to understand the explanation behind it

Target-typed new is a C# 9 feature. Unity’s compilation process is set up to compile with C# 9 features enabled. For other projects, depending on the target framework, you’ll get different default C# language versions. From what I recall, things like net472 and netstandard2.0 range from C# 7.3 to C# 8 or something like that. You can override the language version with <LangVersion>9.0</LangVersion> in the csproj file for non-Unity C# projects. As long as the C# compiler in your SDK supports the specified language version, and you don’t use any runtime functionality dependent C# features that wouldn’t be supported in that target framework, you should be good.

Oh that’s interesting. Do you have any real-life examples for where target framework doesn’t support c# features? It would really help to understand what is possible or not. And also do these types of mismatches show themselves as an error or result in an unexpected behavior?

There’s a list of what the default language versions are, and it’s mentioned that diverging from that can result in compile-time/runtime errors.

We commonly see language features that are essentially syntax sugar that emit IL that can be consumed with no additional runtime changes, so those features can be used pretty broadly. Unity itself doesn’t support several C# 9 features. Back further in time, it didn’t support some of C# 8’s features like default interface methods. In the case of C# 9’s module initializers and records / init-only setters, this seems to be just a limitation of the class library that ships with Unity, as adding the attributes to your own assembly allows you to make use of those features. Unity’s lack of support for C# 8’s default interface methods was a runtime-level limitation that was addressed by updating the runtime. I’m not personally familiar with what potential chaos results from usage of unsupported features, having tended to avoid doing that in the first place. I’m also not sure about where mono (outside of Unity’s fork) ends up with C# compatibility, the latest they list on the site is “partial support for C# 7” but the page seems severely out of date considering updates to the mono runtime since it was published and use with current versions of the .NET SDK when using Xamarin.

I would recommend sticking with .Net Standard 2.1 to be compatible with future versions of Unity. To clarify however the mono that Unity uses (starting with 2021.3) supports up to .Net Framework 4.8.1

2 Likes

Everyone thank you so much, this thread really helped a lot! I will stick to the .Net Standard 2.1 as Alex mentioned. As for the c# version and compatibility it might just work since i don’t really use new features. just some “new()” here and there.