Check if my platform is Android in the editor

I am trying to check it (C#) with

Application.platform == RuntimePlatform.Android

but this gives false, since Application.platform equals “WindowsEditor” when I am on my editor.

I still need to make this check (and avoiding making any test on my Android mobile). How should I do it?

Not tried this myself but I believe you can exit the check with something like this before testing for Android: if (EditorApplication.isPlaying) return; Like I said not tested myself so only a comment not an answer.

I still wouldn't know if I'm testing Android or iOS.

You're trying to test if you're on an Android device without running it on an Android device and want it to return positive, is that right? I think you might struggle with this! If you just want to test some further code to see if it changes things just fake it. Comment out that line and use if(1 == 1)

No, I want to check if my editor is using Android as platform. You can choose that from Build Setting and then Swith Platform.

You want to check? Look at the Title Bar. Jokes aside: If Android mode is set, Editor will be in both Editor and Android mode and both #if directives will run. Android only code will run in Editor.

4 Answers

4

Quick and dirty solution that’s still better from using #if directives:

    public class ApplicationUtil
    {
        public static RuntimePlatform platform 
        { 
            get
            {
#if UNITY_ANDROID
                return RuntimePlatform.Android;
#elif UNITY_IOS
                return RuntimePlatform.IPhonePlayer;
#elif UNITY_STANDALONE_OSX
                return RuntimePlatform.OSXPlayer;
#elif UNITY_STANDALONE_WIN
                return RuntimePlatform.WindowsPlayer;
#endif
            }
        }
    }

Usage:

if (ApplicationUtil.platform == RuntimePlatform.Android)
{
   // Do Android code
}

The problem with using #if is that you can’t refactor your code safely (at least in Visual Studio). You have to do text search and replace even for just renaming a variable. It effectively makes you afraid to change the code, which, in my book, is always a bad thing.

On the flip side, the benefit of using #if is that the code will not be included in the compiled package. So it’s sliiiightly more optimized and results in a smaller binary.

It’s a trade-off that you have to decide.

Android code will run in editor under the

#if UNITY_ANDROID directive

if the editor is in Android mode. This is to make sure that you can still test your game there.

You can use

Application.isEditor or !Application.isEditor to check for or prevent this.

Check the Application page for others, like isMobilePlatform.

The runtime platform is different to directive and should tell you what the actual platform is.

Directive code is for the Pre-Compiler to decide if the code should be compiled or not. The Application check will always happen and eat up your precious cpu cycles :slight_smile:

I know this is an older thread but I came across it looking for the same answer, So for my solution I use EditorUserBuildSettings.activeBuildTarget in combination with checking the runtime platform to check which build target / device I am running.

using UnityEngine;

#if UNITY_EDITOR 
using UnityEditor;
#endif

public class BBApplication : MonoBehaviour
{
    private RuntimePlatform platform
    {
        get
        {
#if UNITY_ANDROID
            return RuntimePlatform.Android;
#elif UNITY_IOS 
            return RuntimePlatform.IPhonePlayer;
#elif UNITY_STANDALONE_OSX
             return RuntimePlatform.OSXPlayer;
#elif UNITY_STANDALONE_WIN
            return RuntimePlatform.WindowsPlayer;
#endif
        }
    }

    public bool isTouchInterface
    {
        get
        {
#if UNITY_EDITOR
            // Game is being played in the editor and the selected BuildTarget is either Android or iOS
            if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android ||
        EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS)
            {
                return true;
            }
#endif
            // Game is being played on an Android or iOS device
            if (platform == RuntimePlatform.Android ||
            platform == RuntimePlatform.IPhonePlayer)
            {
                return true;
            }
            // Game is being played on something other then an Android or iOS device
            else
            {
                return false;
            }
        }
    }

}

See Unity - Manual: Conditional Compilation

If there’s stuff you want to run only on Android, code will look like:

#if UNITY_ANDROID

//do stuff unique to Android

#endif

Of course, whatever you put in that code block will never run in the editor, because the editor is not Android.

If you selected android for the build then the code will run in editor mode.

I don't to check if the device is Android. I want to check if my editor is using Android as platform. You can choose that from Build Setting and then Swith Platform.

I don't quite get then, if you can see it why are you asking?

@Tatanan, I also don't follow why you would need this from script, but here you go http://docs.unity3d.com/412/Documentation/ScriptReference/EditorUserBuildSettings.html.

Incidentally @Tatanan @Noah Dyer's answer is technically correct although the links appear dead (even though they look right - think there's a trailing full stop). I'll try another one [PlatformDependantCompilation][1] If it helps it's good practice to tick it correct. [1]: http://docs.unity3d.com/Manual/PlatformDependentCompilation.html