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

Resolved Platform dependent compilation not working properly? Game view VS WebGL

Discussion in 'Scripting' started by ralf_b, Aug 25, 2021.

  1. ralf_b

    ralf_b

    Joined:
    Jul 9, 2013
    Posts:
    48
    Hello everybody,

    I just wondered if it is possible to run some code only in the game view and other only in a built WebGL?
    Sofar I thought PlatformDependentCompilation would do exactly that, but I get Logs for both cases in the game view, although it should be one or the other, right?

    If I do run this:
    Code (CSharp):
    1.     public void Awake() {
    2.         #if UNITY_EDITOR
    3.         source_local = true;
    4.         Debug.Log("source_local = true");
    5.         #endif
    6.  
    7.         #if UNITY_WEBGL
    8.         source_local = false;
    9.         Debug.Log("source_local = false");
    10.         #endif
    11.     }
    Although I should only get the log from the #if UNITY_EDITOR I get in the game views console in Unity 2021.1.15f1 on Windows I get both logs:

    Code (CSharp):
    1. source_local = true
    2. UnityEngine.Debug:Log (object)
    3.  
    4. source_local = false
    5. UnityEngine.Debug:Log (object)
    6.  
    7.  
     
    Last edited: Aug 25, 2021
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    Unity sets the platform symbols also in the editor for the active build target. E.g. if WebGL is your active build target,
    UNITY_WEBGL
    will also be defined in the editor.

    If you want something to be compiled for only when not in the editor, you'd have to use e.g.
    #if !UNITY_EDITOR && UNITY_WEBGL
    .
     
    ralf_b likes this.
  3. ralf_b

    ralf_b

    Joined:
    Jul 9, 2013
    Posts:
    48
    A great many thanks Adrian :)
    Although it seems counter intuitive that UNITY_WEBGL is true in the editor your approach works fine.