[SOLVED] build targets in code (e.g. web, iPhone)

I’m working on a unity game that may be released on more than one platform.

Some parts of the game have to behave differently depending on the platform.

I’m used to solve this by using build targets in C++, and by checking them using preprocessor directives (e.g. #ifdef WIN32).

I’m now doing this in a similar way in Unity3D, but by checking boolean constants. Naturally, this results in warnings indicating unreachable code.

Does anyone have an idea of a better solution for this?

Additionally, I would like to use different assets for the iPhone version.

Nobody who ever made a game to be released on more than one platform, with some differences in the game depending on the platform? :slight_smile:

I made a script that has certain functions with iPhone-specific code, and for the computer version I replace that with another script with the same functions, but different code that works on the computer. The rest of the scripts are unchanged, although there are a few parts that use

if (iPhoneVersion) {
   // do iPhone stuff
}
else {
   // do computer stuff
}

–Eric

Do you manually replace that script, or is there a way to specify that a certain script is only enabled for certain build platforms (as it is possible in visual studio)?

Don’t you get any warnings on the check ‘if (iPhoneVersion)’?

I get warnings about unreachable code and would like to find a way to get rid of them, so more important warnings don’t go unnoticed.

There are a few such checks

I know for sure that there is a

UNITY_IPHONE define if you build from unity iphone

to use it you would use

#if UNITY_IPHONE

#else

#endif

I’m unsure if there are such flags for desktop or which ones (potentially a UNITY_WEB / OSX / WIN flag), as those platforms are far less distinct than iphone from the rest (pc to non pc)

Thanks, that was what I was looking for!
I didn’t know preprocessor directives existed in C#…

Is there a list somewhere with all directives that can be used in Unity? I googled it, but I didn’t find it.

[edit]
I’m now using #define in combination with #if, #else and #endif to toggle some features.
These define statements are in a file Features.cs.

The problem now is that these define statements are not ‘included’ in other source files, so the #if-test always returns false.

I think I may need to set assembly references, but I’m not sure if this is possible with Unity3D.
[/edit]

I think it’s not possible to do this.

I solved it by making these boolean constants I was used ‘static readonly’ instead of ‘const’. That solved the warnings.

I’m now setting the values of these booleans by using preprocessor directives, so they have different values on iPhone and the other platforms.

It would still be handy to have a list of the available preprocessor directives. Is this available somewhere, or is UNITY_IPHONE the only one?