How to make code that automaticly switch wich part to use when on different platforms?

I read some time ago that i can make code that can automaticly use the code part on wich platform it runs but do not find the right words to find it here or on google.

For example i want to make code for stearing a character that use in the editor wsad but when it runs on the ipad it should use a virtuel joystick.

I read that i can make this but do not find it anymore and that way i do not have to switch fo and on things when build for different platforms.

Hope someone can point me to the right direction.

You can use Directives here is a list Unity3D offers

You’re looking for “Platform Dependent Compilation”, and there are two ways to go about it.

First are compiler directives: Unity - Manual: Conditional compilation
These let you wrap code in #if blocks for each platform.
So you could do this:

#if UNITY_EDITOR
// code to use WASD
#elif UNITY_IPHONE
// code to use joystick
#endif

Or, you can check Application.RuntimePlatform: Unity - Scripting API: RuntimePlatform
This is another way to check what platform you are running on. It lets you do this:
if (Application.RuntimePlatform == RuntimePlatform.WindowsEditor)
// code for WASD
else if (Application.RuntimePlatform == RuntimePlatform.IPhonePlayer)
// code for joystick

Looks like Polymorphik was a few seconds faster than me. Oh well :stuck_out_tongue:

Hope that helps.

This is great and exactly what i am searching for.
Thanky you Polymorphik :slight_smile:

Edit:
Oh a second way also. Thank you ReliCWeb.
I have to learn new things :slight_smile:

You can also add custom defines to make the build vary from session to session. Particularly useful for the WebPlayer when deploying to a bunch of different portals with different API.

You could also use it to split between a paid and free app version. I haven’t tried this approach.