firebase / firebase-unity-sdk

The Firebase SDK for Unity
http://firebase.google.com
Apache License 2.0
234 stars 38 forks source link

[Question] Cloud Functions for Firebase support for 2nd Gen functions #1113

Closed aixaCode closed 2 months ago

aixaCode commented 2 months ago

What is your question?

Does Unity SDK support 2nd Gen functions?

Firebase Unity SDK Version

12.1.0

Unity editor version

2022.3.4.47

Installation Method

.unitypackage

Problematic Firebase Component(s)

Functions

Other Firebase Component(s) in use

No response

Additional SDKs you are using

No response

Targeted Platform(s)

Apple Platforms, Android

Unity editor platform

Windows

Scripting Runtime

IL2CPP

Release Distribution Type

Pre-built SDK from https://firebase.google.com/download/unity

google-oss-bot commented 2 months ago

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

argzdev commented 2 months ago

Hey @aixaCode, thanks for reaching out. Yes, the Firebase Functions of Unity SDK can call 1st gen and 2nd gen functions. There's no need to change the client side code implementation for either generations. You may check out our documentations how to do this. Likewise, you may also check out our Firebase Unity Quickstarts to get set up a lot faster. Though our quickstarts which includes the server side code is still using 1st gen.

Here's a sample 2nd gen server side code with NodeJS:

const { onCall, HttpsError } = require("firebase-functions/v2/https");
const { logger } = require("firebase-functions/v2");

exports.addNumbersV2 = onCall((request) => {
    const firstNumber = request.data.firstNumber;
    const secondNumber = request.data.secondNumber;

    if (!Number.isFinite(firstNumber) || !Number.isFinite(secondNumber)) {
        throw new HttpsError("invalid-argument", "The function must be called " +
            "with two arguments \"firstNumber\" and \"secondNumber\" which " +
            "must both be numbers.");
    }

    return {
        firstNumber: firstNumber,
        secondNumber: secondNumber,
        operator: "+",
        operationResult: firstNumber + secondNumber,
    };
});

Your Unity code should look like this:

protected IEnumerator AddNumbers(int firstNumber, int secondNumber) {
      var func = functions.GetHttpsCallable("addNumbers");
      var data = new Dictionary<string, object>();
      data["firstNumber"] = firstNumber;
      data["secondNumber"] = secondNumber;

      var task = func.CallAsync(data).ContinueWithOnMainThread((callTask) => {
        if (callTask.IsFaulted) {
          // The function unexpectedly failed.
          DebugLog("FAILED!");
          DebugLog(String.Format("  Error: {0}", callTask.Exception));
          return;
        }

        // The function succeeded.
        var result = (IDictionary)callTask.Result.Data;
        DebugLog(String.Format("AddNumbers: {0}", result["operationResult"]));
      });
      yield return new WaitUntil(() => task.IsCompleted);
    }

I believe this answers your question. That said, I'll go ahead and close this thread. Feel free to open another issue in case you have further issues. You may also file a support ticket to our Firebase support channel for general inquiry questions. Thanks!