Search code examples
firebaseunity-game-enginefirebase-storageunity-webgl

Firebase Storage SDK not working in Unity Web Build


I'm working on a project that uses Firebase Storage to download some resources at runtime.

The problem is I need to generate a web build of my app and when I try, Unity says that Firebase namespace cannot be found.

Is there a workaround to use Firebase Storage with WebGL build? I've read people say that it is possible with the REST API of Firebase, but I don't think it is too easy, because the project uses download progress and other stuff that is given by the SDK.

It would be nice to know some other services (where I can store files outside and download them within my app) that work well in Unity web build. Presently I know only Firebase, so I would like to know other references too.


Solution

  • Unfortunately, the Firebase Unity SDK does not support the web target at this time. Currently the way it binds through C++ would make it relatively difficult even with Emscripten. I would strongly encourage you (and anyone else coming across this question) file a feature request if this is something you want.

    That being said, if you plan to just target the web, I would recommend trying to use Unity's built in JavaScript bindings to integrate directly with the Web SDK. Even for web targets, the SDK is doing a lot of heavy lifting for you that you may not realize you're missing out on if you just use the REST API.

    Now if you're only planning on using Cloud Storage (and not, say, Firestore) and these are files that you generally expect to be public, you can just download them directly via a public URL. If you look at the example:

    // Create a reference from an HTTPS URL
    // Note that in the URL, characters are URL escaped!
    var httpsReference = storage.refFromURL('https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg');
    

    You can just grab that file via normal UnityWebRequest calls if it's marked as readable by the public. So the above might become:

    UnityWebRequest www = UnityWebRequest.Get("https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg");
    yield return www.SendWebRequest();
    // ... error handling
    byte[] results = www.downloadHandler.data;
    // ... create a texture from the byte[] array
    

    I hope this helps a little, and I'm sorry that I don't have a better answer!

    --Patrick