Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

#if UNITY_ANDROID

Discussion in 'Android' started by HolBol, Jan 18, 2014.

  1. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    I'm having an issue. I'm assuming the #if UNITY_PLATFORM commands are for only running code on specific code. So I'm trying to check whether the current environment is in the Editor or running on an android device, and run different code for each platform. However using #if UNITY_EDITOR and #if UNITY_ANDROID when the build target for the editor is android, both of these run when the play button is pressed, instead of just the UNIT_EDITOR one. Can someone explain why?
     
  2. nikolic

    nikolic

    Joined:
    Oct 15, 2011
    Posts:
    162
    These are compile-time directives.

    The lines of code inside #if UNITY_ANDROID will be compiled if Platform is set to "Android" inside "Build Settings" dialog and you:
    • click "Build" or
    • click "Play" inside the Unity editor (and of course when the editor performs auto-build)
    The lines of code code inside #if UNITY_EDITOR will be compiled no matter what Platform is set but only if you:
    • click "Play" inside the Unity editor (and of course when the editor performs auto-build)
    When you build your game to an Android device, lines of code wrapped with #if UNITY_ANDROID will end up "inside" the .apk file, those wrapped with #if UNITY_EDITOR won't. #UNITY_EDITOR seems like it but is not another platform directive. Platform directives should be mutually exclusive and set to true if particular platform is built to.

    If you need checking at runtime, maybe this is for you:

    http://docs.unity3d.com/Documentation/ScriptReference/Application-platform.html
     
  3. afshin_a_1

    afshin_a_1

    Joined:
    Apr 26, 2015
    Posts:
    51
    i know it's a bit late but for future researches, this will execute code only on android device:
    1. #if UNITY_ANDROID && !UNITY_EDITOR
    2. //code here
    3. #endif
     
  4. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Already knew that when I wrote this post 3 years ago. ;)
     
    westdev likes this.
  5. Candor

    Candor

    Joined:
    Oct 15, 2015
    Posts:
    4
    This helped me understand how these defines work.

    Thank You
     
  6. westdev

    westdev

    Joined:
    Sep 12, 2017
    Posts:
    9
    thank you for this great informations all of you
     
  7. samudithakp

    samudithakp

    Joined:
    Feb 3, 2019
    Posts:
    1
    thank you. still useful in even 2022
     
    danidina330 and Michael_Berna like this.
  8. OM8890

    OM8890

    Joined:
    May 29, 2022
    Posts:
    34
    true
     
  9. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    154
    you could use
    #if UNITY_EDITOR
    .//editor code
    #elif UNITY_ANDROID
    //platform code
    #endif

    which will not proceed if the first check is true, has to be in that order

    however what id really like to knw is the differnce btwn
    PLATFORM_ANDROID and UNITY_ANDROID