# Unity: how to change runtime code for different platforms?

In Unity, how can I programmatically control what source code is compiled?

I have a script with various BuildPipeline.BuildPlayer calls, allowing me to build all versions of my game. However, I'm using the AVPro Video asset to play video, and it doesn't support 32bit Mac. As such, I need to fall back other video playing code on 32bit Mac. There's no preprocessor directive to differentiate 32bit OSX (like you can do for other platforms https://docs.unity3d.com/Manual/PlatformDependentCompilation.html ).

However, since I'm building each version in sequence, it seems like I should be able to set some state so that during compilation, the right code is used. I tried this:

VersionHandler.onMac32 = true;
BuildPipeline.BuildPlayer(levels, path, BuildTarget.StandaloneOSXIntel, BuildOptions.None);
VersionHandler.onMac32 = false;


And then checking that static variable in game. However, that static is "never set", presumably because the the state of the variable during compilation doesn't affect the value at runtime. Is there any other way I can "get at" the compiled code and set a flag for certain builds?

As a fallback, I can manually swap code in every time I build for OSX 32-bit, but I'd prefer not to do that.

• Combining #if UNITY_32 and #if UNITY_STANDALONE_OSX doesnt work? – wondra Apr 18 '17 at 6:40
• That doesn't work; it doesn't look like UNITY_32 is a real directive. :/ As far as I can tell, there isn't a 32vs64 directive. – akroy Apr 18 '17 at 13:30
• That is rather weird, since the ticket about adding it was closed as "completed" . Maybe they didnt add it after all or maybe the final implementation has define of a bit different name? The comments there also suggest alternative solution. – wondra Apr 18 '17 at 16:57
• Hmm, those comments are quite old and are talking about the future features in Unity 5, so I can only assume it was dropped, because it's not in the list of all the other directives. :( – akroy Apr 18 '17 at 17:55

This solution feels a bit hacky, but it works:

using System;

public static bool IsMac32() {
#if (UNITY_STANDALONE_OSX)
return IntPtr.Size == 4;
#else
return false;
#endif
}

• You know...sometimes a hacky solution is the only solution. I certainly wouldn't have thought about checking the size of a pointer. – Draco18s Apr 18 '17 at 15:46