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.

Question Exclude scripts from webgl build

Discussion in 'WebGL' started by yangjon1997, Mar 14, 2023.

  1. yangjon1997

    yangjon1997

    Joined:
    Jun 10, 2022
    Posts:
    3
    Hi,

    When building a WebGL project, we found that firebase doesnt work. We learned that we could use the JS version to make firebase work, however we still want to use firebase for our non webgl version. By having scripts that import firebase causes our build to webgl to fail, so we were wondering if there is a way to exclude scripts that include firebase to not be built for webgl builds.

    We saw that there are assembly definitions but we havent been able to get them to work, since we still want all scripts to know about each other. Is there another way to do this?

    Thanks for the help,
    Jon
     
  2. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    801
    Assembly definition files is for sure the proper solution to your problem. Additionally you can use scripting symbols to make code run on one platform and not another. See also https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

    E.g. for your Firebase initialization you could do something like this:
    Code (CSharp):
    1. void Start()
    2. {
    3. #if UNITY_ANDROID || UNITY_IOS
    4. Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
    5.   var dependencyStatus = task.Result;
    6.   if (dependencyStatus == Firebase.DependencyStatus.Available) {
    7.     // Create and hold a reference to your FirebaseApp,
    8.     // where app is a Firebase.FirebaseApp property of your application class.
    9.        app = Firebase.FirebaseApp.DefaultInstance;
    10.  
    11.     // Set a flag here to indicate whether Firebase is ready to use by your app.
    12.   } else {
    13.     UnityEngine.Debug.LogError(System.String.Format(
    14.       "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
    15.     // Firebase Unity SDK is not safe to use here.
    16.   }
    17. });
    18. #endif
    19. }
    in general I would recommend to create a wrapper class that holds an implementation for the platform, to avoid to many scripting symbol checks everywhere. This would be achieved with the locator pattern: https://gameprogrammingpatterns.com/service-locator.html