I have an asset on the asset store for which I added in-editor tooltips, which is a feature of Unity 4.5. In the script, I handle this by having a section that goes:
#if UNITY_4_5
//some code with tooltips
#endif
#if UNITY_4_3
//some other code
#endif
How can I write this instead, so I don’t have to update this bit every time a new version of unity comes out? I’m looking for a way to say “if editor version is older than 4.5, do x, else do y”
I can see there’s an “#else” command, but I don’t know how to properly close the region it opens…
Starting from Unity 5.3.4, you can
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.Z), Unity exposes one
global #define in the format
UNITY_X_Y_OR_NEWER, that can be used
for this purpose.
If you check for the compatible you will always have to keep adding updates as the version goes up. The following code will break as soon as 5.1 comes out. You could try guessing the next series of numbers, the compiler allows you to write in defines that do not exist.
#if UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_5_0
// Do new stuff
#else
// Do old stuff
#endif
If you check for the old versions you will have a much longer line of code, but it will not need updating as new versions come out.
#if UNITY_2_6 || UNITY_2_6_1 ... UNITY_4_2
// Do old stuff
#else
//Do new stuff
#endif