WebGL Build Preprocessor Directive Bug with Firebase References in Unity

Description: I’ve encountered a potential bug in Unity’s handling of preprocessor directives when working with WebGL builds. The issue arises when trying to exclude Firebase (or other unsupported services) from WebGL builds while allowing the code to run in the Unity Editor for testing.

The expected behavior is that the following directive should allow Firebase references to be included while running in the Editor, but excluded during WebGL builds:

csharp

#if !UNITY_WEBGL || UNITY_EDITOR
    private Firebase.Auth.FirebaseAuth Auth;
    private Firebase.Database.FirebaseDatabase Database;
#endif

However, during WebGL builds, this condition still results in Firebase-related errors, stating that Firebase namespaces cannot be found, even though Firebase shouldn’t be included in WebGL. To work around the issue, I need to use this directive instead:

csharp

#if !UNITY_WEBGL
    private Firebase.Auth.FirebaseAuth Auth;
    private Firebase.Database.FirebaseDatabase Database;
#endif

While this does successfully compile for WebGL, it prevents the code from running inside the Unity Editor, which is critical for testing and debugging. To get it working in the Editor, I must change the directive back to include UNITY_EDITOR, like this:

csharp

#if !UNITY_WEBGL || UNITY_EDITOR
    private Firebase.Auth.FirebaseAuth Auth;
    private Firebase.Database.FirebaseDatabase Database;
#endif

Hey! Have you tried spliting the logic to see if we can evaluate one first then evaluate the other, for example:

#if UNITY_EDITOR
    // Editor-specific
    private Firebase.Auth.FirebaseAuth Auth;
    private Firebase.Database.FirebaseDatabase Database;
#else
    #if !UNITY_WEBGL
    private Firebase.Auth.FirebaseAuth Auth;
    private Firebase.Database.FirebaseDatabase Database;
    #endif
#endif

Note that UNITY_WEBGL is also defined if the current platform is switched to WebGL, not just when building. This may further complicate things.

You could add a custom symbol to the webgl platform in Player Settings if that helps, and maybe toggle it with a custom build script. See: Unity - Manual: Build Player Pipeline

try this instead:

#if !UNITY_WEBGL && UNITY_EDITOR
    private Firebase.Auth.FirebaseAuth Auth;
    private Firebase.Database.FirebaseDatabase Database;
#endif