- Home /
Check API Compatibility Level
Hello guys,
I'm evolving another solution to put at Asset Store. That solution works ok using .NET 2.0 Subset but I can really improve things using .NET 2.0.
-How can I check if I`m using regular .NET 2.0 or .NET 2.0 Subset? (So I don`t need to force someone to use .NET 2.0 or avoid implement the .NET 2.0 solution)
-It`s possible to add just a single part of .NET 2.0 into a .NET 2.0 subset deploy?
Thanks!
Answer by Bunny83 · Mar 15, 2016 at 02:45 AM
Well, something like that would require you to wrap your code in conditional preprocessor tags.
However, as far as i know there's no
preprocessor define for the used .NET level. So you can't really detect the API level automatically that way. Everything else that involves checks at runtime won't work as your code wouldn't compile if a used feature isn't available. If the code doesn't compile you can check anything.
edit
Unity now has all sorts of symbols defined for various purposes. Just check the documentation. So the following example is kinda outdated but may be still relevant for custom feature switches.
So the usual solution is to wrap your code in preprocessor tags and simply use your own custom define. If it's just a single c# file you could add the define at the beginning of the file, so the user can comment it in / out. If you have many files which would be affected, the user would need to add the define to the project settings (scripting define symbols). By default you probably want to use .NET 2.0 subset.
// uncomment the following line if you use .Net 2.0 and not .Net 2.0 subset
//#define USE_DOTNET20
#if USE_DOTNET20
// Some code that requires the full .Net 2.0 support
#else
// code that is used when working with .Net 2.0 subset.
#endif
Again if multiple files need that check you should define the symbol in the player settings. You can add a comment to your code that informs the user that he can add that symbol if he want to.
/***
If you use .Net 2.0 and not .Net 2.0 subset you should add the following symbol to the scripting define symbols in the player settings(Edit->Project Settings->Player->Other Settings):
USE_DOTNET20
***/
Wonderful answer, Bunny83!
The proposed solution is more our less what I have in $$anonymous$$d as plan B (The plan A is to find a way to "automatically" define USE_DOTNET20. By "Automatically" I mean without user interference).
But you really helped me in the sense of reinforce the possibility of Plan A non-existence.
Best Regards,
Answer by booferei · Apr 01, 2021 at 02:25 PM
The accepted answer from Mar 15, 2016 is obsolete. Unity added support for the relevant #define directives on version 5.3.4, which, Incidentally, came out on this exact same date - Mar 15, 2016.
These two directives are relevant to the OP's question:
NET_2_0: Defined when building scripts against .NET 2.0 API compatibility level
NET_2_0_SUBSET: Defined when building scripts against .NET 2.0 Subset API compatibility level
For more see: Platform dependent compilation.
Your answer
Follow this Question
Related Questions
Need help using Google Drive API from NuGet with Unity 0 Answers
Can Unity work with a "Managed .NET assembly (NatNetML.dll) for use in .Net compatible clients"? 1 Answer
unity is not creating build on .NET 2.0 SUBNET and getting ArgumentException. 1 Answer
No option for API Compatibility Level .NET 4x 1 Answer
does Unity support. NET 4? 1 Answer