How to support both Unity 3 and Unity 4 gameObject active behaviour in one codebase?

So Unity now has a new system for marking GameObjects as active, which is fine, except that I maintain a library that needs to work for both users of Unity 3.5 and Unity 4. I’m trying to figure out the simplest/easiest way to have code that will work in either version of Unity… I’m assuming I’ll probably have to do some sort of reflection to see which methods are available on GameObject.

Ideally I could just do this with defines (#if UNITY_4_0) but there’s no way to say “if it’s Unity 4.0 or greater” with those… I guess I could do #if and then every major 3.0 version, but that seems like overkill… is there any way to just check if it’s any version of 3?

EDIT:

For now I’ve gone with

#if UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5
	_gameObject.active = false;
#else
	_gameObject.activeSelf = false;
#endif

I noticed that there is an Application.unityVersion variable, which will return the version of the Unity runtime at runtime as a string, like “3.5.6f3”.

It’s a little annoying that it comes as a string, but if you do a bit of string parsing into a major/minor/revision structure of some kind, you could probably check if major >= 4 or not to get the effect you’re looking for.

If you have to do a number of #ifs, then this makes it less annoying:

#if UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5
#define UNITY_3
#endif

#if UNITY_3
    // do Unity 3.x stuff
#else
    // do Unity4.x+ stuff
#endif

That won’t work, because it’s a runtime thing, and Unity 4 code such as GameObject.SetActive will not compile in Unity 3 (and Unity 3 code such as GameObject.active will generate warnings in Unity 4). It’s necessary to use conditional compilation instead.

–Eric