Firebase 函数在尝试从 Unity 调用时抛出内部错误

问题描述 投票:0回答:1

我在安装了 Firebase Auth 和 Firebase Functions SDK 的 Unity C# 应用程序上工作。

由于 Firebase 模拟器,我开发和测试了三个 callable 函数,它们似乎可以正常工作。

我部署了函数,在 Unity Editor 中启动了游戏,但每次调用函数时都会出错。错误总是

INTERNAL
.

我检查了 Google Cloud 仪表板上的日志,似乎我的功能甚至没有运行。

我设置断点并决定检查异常以收集一些信息。 但是,除了

"One or more errors occurred"
"INTERNAL"
之外,没有任何有用的信息。

在 Google/SO 上进行大量搜索后,我发现了两个可能的罪魁祸首:

  • SDK 过时 -> 我重新下载最新的 SDK 并将 Firebase Auth 和 Firebase Functions 重新导入我的 Unity 项目 -> 同样的错误。
  • 如果与us-central1
    不同,则在获取Functions实例时需要指定region
    。根据 Google 仪表板,我的功能位于
    us-central1
    区域,但以防万一我指定它 --> 同样的错误。 然后我注意到我的实际 Firebase 项目在
    europe-west3
    中,所以我在获取 Firebase Functions 或 App 的实例时在我的代码中指定了它 --> 仍然是同样的错误。

现在我开始怀疑一切,所以我注释掉

UseFunctionsEmulator()
并用模拟器再次测试->它工作正常。

可能是什么问题?

这是我的

FirebaseManager.cs
在其他任何事情之前运行:

public class FirebaseManager : MonoBehaviour
{
    private static bool gameQuitting = false;
    private static readonly object Lock = new object();
    private static FirebaseManager _instance;
    public static FirebaseManager Instance
    {
        get
        {
            // [check if instance exists etc..]
        }
    }

    private FirebaseApp _app;

    private FirebaseAuth _auth;
    public FirebaseAuth Auth
    {
        get
        {
            if (_auth == null)
            {
                _auth = FirebaseAuth.GetAuth(_app);
            }
            return _auth;
        }
    }

    private FirebaseUser _user;
    public FirebaseUser User
    {
        get
        {
            return _user;
        }
        set
        {
            _user = value;
        }
    }

    private FirebaseFunctions _func;
    public FirebaseFunctions Func
    {
        get
        {
            if (_func == null)
            {
                _func = FirebaseFunctions.DefaultInstance;
            }
            return _func;
        }
    }

    // Events to subscribe to for this service
    public delegate void FirebaseInitialized();
    public static event FirebaseInitialized OnFirebaseInitialized;
    public static bool IsInitialized = false;

    private void Awake()
    {
        if (_instance == null)
        {
            DontDestroyOnLoad(this.gameObject);
            _instance = this;
        }
        else { Destroy(this.gameObject); }
    }

    private void Start()
    {
        InitializeFirebase();
    }

    private void InitializeFirebase()
    {
        Debug.Log($"FirebaseManager.cs > INITIALIZING FIREBASE");
        Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task =>
        {
            var dependencyStatus = task.Result;
            if (dependencyStatus == Firebase.DependencyStatus.Available)
            {
                // Create and hold a reference to your FirebaseApp,
                // where app is a Firebase.FirebaseApp property of your application class.
                _app = FirebaseApp.DefaultInstance;
                // _app = FirebaseApp.GetInstance("europe-west3");

                // Create and hold a reference to FirebaseAuth
                _auth = FirebaseAuth.DefaultInstance;

                // Create and hold a reference to already logged in user (if any)
                _user = _auth.CurrentUser;

                // Create and hold a reference to Functions
                _func = FirebaseFunctions.DefaultInstance;
                // _func = FirebaseFunctions.GetInstance(FirebaseApp.DefaultInstance, "europe-west3");
                // _func = FirebaseFunctions.GetInstance("europe-west3");
                // #if UNITY_EDITOR
                // _func.UseFunctionsEmulator("http://localhost:5001"); // LOCAL FUNCS 
                // #endif

                // Subscribing to AuthStateChanged
                FirebaseManager.Instance.Auth.StateChanged += AuthService.AuthStateChanged;

                Debug.Log($"FirebaseManager.cs > INITIALIZED");
                IsInitialized = true;
                if (OnFirebaseInitialized != null) OnFirebaseInitialized.Invoke();
            }
            else
            {
                UnityEngine.Debug.LogError(System.String.Format(
                  "FirebaseManager.cs > Could not resolve all Firebase dependencies: {0}", dependencyStatus));
                // Firebase Unity SDK is not safe to use here.
            }
        });
    }

    private void OnDestroy()
    {
        if (_auth != null) _auth = null;
    }

    private void OnApplicationQuit()
    {
        FirebaseManager.Instance.Auth.StateChanged -= AuthService.AuthStateChanged;
        gameQuitting = true;
    }
}

我这样从 Unity 调用我的函数:

HttpsCallableReference function = FirebaseManager.Instance.Func.GetHttpsCallable("GetAllCharacters");

// Call the function and extract the operation from the result.
function.CallAsync().ContinueWithOnMainThread((task) =>
{
    if (task.IsFaulted)
    {
        // this foreach is a debug attempt
        foreach (var e in task.Exception.Flatten().InnerExceptions)
        {
            Debug.LogWarning($"Received Exception: {e.Message}");
        }

        foreach (var inner in task.Exception.InnerExceptions)
        {
            if (inner is FunctionsException)
            {
               var e = (FunctionsException)inner;
               // Function error code, will be INTERNAL if the failure
               // was not handled properly in the function call.
               var code = e.ErrorCode;
               var message = e.Message;

               Debug.LogError($"Code: {code} // Message: {message}");

               // [Handle error]
            }
        }
    }
    else
    {
        // [Handle data returned by the function]
    }
});

最后,我的 Firebase 函数看起来像这样:(

index.ts
)

import * as Characters from "./Characters/Characters";

// CHARACTERS
export const CreateNewCharacter = Characters.CreateNewCharacter;
export const GetAllCharacters = Characters.GetAllCharacters;
export const DeleteCharacterByID = Characters.DeleteCharacterByID;

Characters.ts

import * as functions from "firebase-functions";
import { initializeApp } from "firebase-admin/app";
// [custom classes imports]

// DATABASE INITIALIZATION
initializeApp();
const db = getFirestore();
 
// CREATE NEW CHARACTER
export const CreateNewCharacter =
  functions.https.onCall(async (data, context) =>
  {
      ...
  });

// GET ALL CHARACTERS
export const GetAllCharacters =
  functions.https.onCall(async (data, context) =>
  {
      ...
  });

// DELETE CHARACTER BY ID
export const DeleteCharacterByID =
  functions.https.onCall(async (data, context) =>
  {
      ...
  });

在逐步调试中,这是我发现的:

StackTrace
值是:

" at Firebase.Functions.HttpsCallableReference.b__9_0 (System.Threading.Tasks.Task'1[TResult] 任务) [0x00063] 在 <9b40ff23f61a4fcf97a5710997860279>:0 在 System.Threading.Tasks.ContinuationResultTaskFromResultTask`2[TAntecedentResult,TResult].InnerInvoke () [0x00024] 在 <4ba27aa039714eafa1e8892e51af2932>:0 在 System.Threading.Tasks.Task.Execute () [0x00000] 在 <4ba27aa039714eafa1e8892e51af2932>:0 "

c# node.js firebase unity3d google-cloud-functions
1个回答
-4
投票

我遇到了同样的问题,你找到解决办法了吗?

© www.soinside.com 2019 - 2024. All rights reserved.