Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Exclude scripts from webgl build

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

  1. yangjon1997

    yangjon1997

    Joined:
    Jun 10, 2022
    Posts:
    4
    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:
    830
    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