5
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Combining #if UNITY_32 and #if UNITY_STANDALONE_OSX doesnt work? \$\endgroup\$ – wondra Apr 18 '17 at 6:40
  • \$\begingroup\$ 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. \$\endgroup\$ – akroy Apr 18 '17 at 13:30
  • \$\begingroup\$ 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. \$\endgroup\$ – wondra Apr 18 '17 at 16:57
  • \$\begingroup\$ 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. :( \$\endgroup\$ – akroy Apr 18 '17 at 17:55
4
\$\begingroup\$

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
}
\$\endgroup\$
  • 2
    \$\begingroup\$ You know...sometimes a hacky solution is the only solution. I certainly wouldn't have thought about checking the size of a pointer. \$\endgroup\$ – Draco18s Apr 18 '17 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.