Constantin Beer
Constantin Beer

Reputation: 5835

Adding Firebase Admin SDK to Unity project

I'm new to Unity as well as to C# (real noob to C# and all the .NET stuff) and I want to use the Firebase Realtime Database in my Unity project. I was following the instructions on the Firebase docs to set things up, but after I created a new p12 file and added this code to my project

  void Start() {
    // Set these values before calling into the realtime database.
    FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://YOUR-FIREBASE-APP.firebaseio.com/");
    FirebaseApp.DefaultInstance.SetEditorP12FileName("YOUR-FIREBASE-APP-P12.p12");
    FirebaseApp.DefaultInstance.SetEditorServiceAccountEmail("[email protected]");
    FirebaseApp.DefaultInstance.SetEditorP12Password("notasecret");
  }

I got the following hint:

Method 'Firebase.Unity.Editor.FirebaseEditorExtensions.SetEditorP12FileName' is obsolete: Service account in editor is no longer supported. Please use standard sign-in methods instead, ex. FirebaseAuth.SignInWithEmailAndPasswordAsync()

This brought me to the Firebase Deprecated page which says:

deprecation note

Now I don't know how to add the Admin SDK to my Unity project. I can only find references about how to add the Admin SDK to a server. But how can I add the Admin SDK to an Unity project?

Upvotes: 2

Views: 2095

Answers (3)

Jakob Leitner
Jakob Leitner

Reputation: 1

I'm currently trying to do the same thing. Unfortunaltely,FirebaseAdmin has a dependency on Newtonsoft.Json 12.0.0.0, but Unity has Newtonsoft.Json 13.0.0.0 built in. I tried a dependency-redirect. But i guess I am doing something wrong.

I created a "csc.rsp" file and added:

-appconfig:app.config
  • I created a "app.config" file and added:

      <configuration>
        <runtime>
          <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
              <assemblyIdentity name="Newtonsoft.Json" 
        publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
              <bindingRedirect oldVersion="0.0.0.0-13.0.0.0" 
        newVersion="13.0.0.0" />
            </dependentAssembly>
          </assemblyBinding>
        </runtime>
        </configuration>
    

Did you stumble across the same issue?

Upvotes: 0

user3554809
user3554809

Reputation: 71

In case you would like to write to some database locations from unity editor where you would not allow your regular user, you could work around it with firebase rules and setting up a "passkey".

In your unity script, add a passkey entry to the data (assuming they are represented by a dictionary dict) you are about to upload by dict.Add("passkey", "12345");. Additionally, put the code assigning the passkey value inside a conditional block which only compiles in the unity editor: #if UNITY_EDITOR, so that it does not make it into your app.

In your rules, set up a write condition in the relevant location as {".read": false, ".write": "newData.child('passkey').val() === '12345'"}.

However, I provide no guarantee that this is good practice.

Upvotes: 0

Constantin Beer
Constantin Beer

Reputation: 5835

I wrote a mail to the Firebase Support and even wrote in the Firebase Community Slack channel and the reference on how to add the Admin SDK to a server seems to be the way to go for unity projects as well.

Here is what I did (in case someone want to save some time):

  1. I installed the NuGet plugin for Unity and then searched for the Firebase Admin SDK (installed the SDK of course as well)
  2. I followed the instructions here and copied the code snippet into my project:
FirebaseApp.Create(new AppOptions()
{
    Credential = GoogleCredential.GetApplicationDefault(),
});

Important: after copying the code snippet and importing the dependencies, my IDE imported the Firebase package instead of the FirebaseAdmin package which of course don't work and the parameter Credential can't be found as part of AppOptions. So make sure to use the correct dependency.

Upvotes: 2

Related Questions