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 Im using regular .NET 2.0 or .NET 2.0 Subset? (So I dont 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!

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
***/

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.