Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Build different target platform -> wrong code being compiled

Discussion in 'Editor & General Support' started by ArjanBroosSioux, Apr 14, 2022.

  1. ArjanBroosSioux

    ArjanBroosSioux

    Joined:
    Jun 1, 2021
    Posts:
    17
    The documentation mentions:
    "Note: Be aware that changes to scripting symbols only take effect at the next domain reload, when scripts are recompiled."
    "It also means that the built-in scripting symbols defined for the current active target platform (such as UNITY_STANDALONE_WIN, or UNITY_ANDROID) remain in place even if you try to build for a different target platform, which can result in the wrong code being compiled into your build."

    How then, do I build two different target platforms from script?

    We first call BuildPipeline.BuildPlayer() with these options:
    Code (CSharp):
    1. var options = new BuildPlayerOptions
    2. {
    3.     ...
    4.     subtarget = (int)StandaloneBuildSubtarget.Server,
    5.     target = BuildTarget.StandaloneWindows64,
    6.     ...
    7. };
    Followed by a call to BuildPipeline.BuildPlayer() with these options:
    Code (CSharp):
    1. var options = new BuildPlayerOptions
    2. {
    3.     ...
    4.     subtarget = (int)StandaloneBuildSubtarget.Player,
    5.     target = BuildTarget.StandaloneWindows64,
    6.     ...
    7. };
    It ends up building with the wrong scripting symbols.
     
  2. ArjanBroosSioux

    ArjanBroosSioux

    Joined:
    Jun 1, 2021
    Posts:
    17
    SwitchActiveBuildTarget is not available when running the Editor in batch mode. It recommends using the -buildTarget CLI parameter. Is there no -buildSubTarget CLI parameter then?
     
  3. Unity_Joseph

    Unity_Joseph

    Unity Technologies

    Joined:
    Nov 3, 2016
    Posts:
    16
    Hi Arjan,

    You should avoid having code that is required at build time within platform specific blocks. Instead use runtime checks such as EditorUserBuildSettings.activeBuildTarget.
    If the code should only be compiled on a specific platform and in the editor, combine the platform define with an editor define like so: UNITY_EDITOR || UNITY_ANDROID.
     
  4. ArjanBroosSioux

    ArjanBroosSioux

    Joined:
    Jun 1, 2021
    Posts:
    17
    Hi Joseph,

    Thanks for your response!
    I'm not sure I understand it, though. How does it help me ensure that scripting symbols are switched when building a dedicated server followed by building a player, from script in batch mode?
     
  5. Ryanc_unity

    Ryanc_unity

    Unity Technologies

    Joined:
    Jul 22, 2015
    Posts:
    332
    Hi ArjanBroosSioux,
    The note in the documentation is about the scripting defines in the current running editor code, not the resulting script build output. This is important to note for callbacks that impact the build, if they rely on scripting defines, then they will not work correctly until the editor has a chance to reload the domain which will not occur in batch mode.

    From the limited code you provided so far, the output assemblies should be correct. If not, please open a bug with a repro case for us to look into further.
     
  6. ArjanBroosSioux

    ArjanBroosSioux

    Joined:
    Jun 1, 2021
    Posts:
    17