- Home /
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.
Answer by eshan-mathur · Feb 24, 2016 at 05:14 AM
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.
Answer by psydent · Feb 24, 2016 at 01:32 AM
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.
Answer by phil_me_up · Feb 23, 2016 at 07:39 PM
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).
Nope, for some reason even when Build Configuration is set to Debug in Xcode, the "#if DEBUG" define is still false in Unity/C# land. I just did a test by changing the "Run In Xcode As", which successfully changed the Build Configuration setting in Xcode, but had no impact on that define.
Ah sorry, I didn't read your question properly.
What you're trying to achieve I assume, is to write something in C# that will only happen when XCode is set to release build and not debug (or visa versa).
I would have thought that http://docs.unity3d.com/ScriptReference/Debug-isDebugBuild.html would be a good start, although I don't know for sure that it's what you're after or how it will be compiled out for release builds.
EDIT: Sorry, that's for the development build, not the XCode project settings... I'm sure I've done this before. I'll go check....
Answer by TimHeijden · Nov 19, 2016 at 04:00 PM
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