Shouldn't there be pre-processor directives for all versions greater than a certain version?

I want something like:

#if UNITY_4_5_PLUS

//Code that only happens in Unity versions 4.5 and greater.

#endif

and while I’m at it something like

#if UNITY_3_0_TO_UNITY_4_3

//Code that only happens in Unity versions between 3.0 and 4.3.

#endif

For example, If a new feature is added for Unity 4.5 that requires version specific code, then I want to assume that it’ll continue to work on future versions. When 4.6 comes out, I don’t want to go through every piece of version specific code and add “|| UNITY_4_6”. These lines of code quickly begin getting ridiculous.

For KeyWord searching: Platform Dependent Compilation. Compile code selectively depending on the version of the engine you are working on.

Unfortunately that would completely pollute the compiler directive list. You’d have to do 4_5_Plus, 4_5_2_Plus, 4_5_3_Plus… and on and on and on for every version. What you could do though is use a define but you’d have to add it to the top of every file you need it as such:

#if UNITY_3_5 || UNITY_4_2
#define YES_COMPILE
#endif

Then in your code:

#if YES_COMPILE
//do your thing
#endif

1 Like

I can work with that. At least with that I only have to change one line each time Unity updates. Thanks!

Well one line in each file where it’s needed, but a global find/replace would be sufficient.

1 Like

Oh I skipped that part… I see. Well, that’s still better than what I was doing. But I really don’t like adding obscure development steps that I have to hope I remember to do each time a new version comes out. I like the idea of a “UNITY_4_5_PLUS” concept because everything will simply work until the day unity creates a version where the functionality no longer works. And that’s when I’d WANT a crash or compilation error.

Is there any documentation I could comb through so I can better understand why my suggestion would pollute the compiler directive list to an unacceptable degree? I’m not exactly sure what that means. Would this bog anything down even if the list was thousands of options long?

No documentation but for each version you would have something like this:

Unity 3.5 → UNITY_3_5_PLUS
Unity 4 → UNITY_3_5_PLUS, UNITY_4_0_PLUS
Unity 4.2 → UNITY_3_5_PLUS, UNITY_4_0_PLUS, UNITY_4_2_PLUS
Unity 4.5 → UNITY_3_5_PLUS, UNITY_4_0_PLUS, UNITY_4_2_PLUS, UNITY_4_5_PLUS

They’d have to add new directives for every single release and it would get really hairy. There are already a lot of directives in there. You should see my asset. I have directives for multiple platforms but also for iOS for different versions of Unity.

Necroing this 8 year old thread for anyone googling a solution; there is: UNITY_X_Y_OR_NEWER.

You can also compile code selectively based on the earliest version of Unity required to compile or execute a given portion of code. Given the same version format as above (X.Y), Unity exposes one global #define in the format UNITY_X_Y_OR_NEWER, that you can use for this purpose.

You can apply conditional compilation like this:

#if UNITY_2021_1_OR_NEWER && !UNITY_2022_2_1_OR_NEWER
// Your code here
#endif
1 Like