Is there a way to tell if Xcode is set to Debug vs. Release within Unity?

Is there a way to tell if Xcode is set to Debug or Release within C# / Unity? Some kind of static class or # define?

I’m specifically talking about the “Build Configuration” dropdown in the Edit Scheme menu. This can be set within Unity using the “Run in Xcode As” dropdown in Build Settings.

Mind you I’m not talking about the Development Build checkbox in Unity. Just Xcode (or platform, whatever). I’d like to run platform-specific build-configuration-specific code.

You have to write a plugin to handle this at runtime. Because the Build Config can be set in Xcode after Unity has generated the project, all the code at the Unity level (C#) must be compiled into the Xcode project. Whether you run it or not needs to be gotten from the native level.

Here’s some code that I whipped together but did not test and may need revisions, such as exception handling, updated preprocessor directives, and a different default return value based on your use cases. You could call it once when your game launches and then keep the bool value in a C# script somewhere so you only incur the overhead of calling native code once.

Something like in an Objective-C plugin (.mm) you have:

extern "C"
{
    bool _IsBuildConfigDebug()
    {
    #if DEBUG
        return YES;
    #else
        return NO;
    #endif
    }
}

And then you have to have in a C# script:

using System.Runtime.InteropServices;

[DllImport ("__Internal")]
private static extern bool _IsBuildConfigDebug();

public static bool IsBuildConfigDebug()
{
	#if !UNITY_EDITOR
		#if UNITY_IPHONE || UNITY_TVOS
		return _IsBuildConfigDebug();
		#endif
	#endif
	return false;
}

There might be a simpler way to do this by reading the Build Config setting you set in the drop down of build options, but it won’t be able to change if you change the setting in Xcode. You would have to rebuild your Xcode project from Unity if you want to change from a Debug to a Release build config, which I recommend staying away from.

Hopefully this helps. Maybe I can post an example, but don’t hold your breath.

It’s been a while since I checked, but I’m pretty sure that XCode uses the DEBUG define when in a debug build, so #ifdef DEBUG should be what you’re looking for.

You can set your own defines per schema, but as Unity will rebuild your XCode project that’s not so useful (unless you want to start running post build processes to modify the project, which is an option).

For those who want to get the editor setting and use it in an editor script:

For (at least) Unity 5.4 or higher, there is a property in EditorUserBuildSettings called iOSBuildConfigType. This may be exposed in Unity 5.5 but it isn’t in 5.4. That said, you can still retrieve it using reflection:

System.Reflection.PropertyInfo pi = typeof(EditorUserBuildSettings).GetProperty("iOSBuildConfigType", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);

// Static class has no instance, so null's as variables
int val = (int)pi.GetValue(null, null);

// 0 == Debug, 1 == Release

I ended up deciding not to do this. My goal was to tie my own debug code (debug menus, in-game tools, etc.) to the Xcode build configuration setting so that they would never get out of sync and I’d only have to set Debug vs Release in one place.

Ultimately the realization was that at some point I will want to test a release candidate version of the game (none of my “debug” code) with all the native Debug mode stuff on (profiling, debugging, etc.). Tying them together would rob me of this flexibility.

For now I’m just making a checkbox somewhere.