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.
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)
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.
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.
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
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;
}
}
}
}
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.
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
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.
– MmmpiesI still wouldn't know if I'm testing Android or iOS.
– TatananYou'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)
– MmmpiesNo, I want to check if my editor is using Android as platform. You can choose that from Build Setting and then Swith Platform.
– TatananYou 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.
– meat5000