调用Firebase第二代Cloud Functions时遇System.AggregateException错误求助
Unity调用Cloud Functions第二代函数出现INTERNAL错误
问题相关代码
Cloud Functions 第二代函数代码
const functions = require('@google-cloud/functions-framework'); functions.http('canplace', (req, res) => { print("hi mark"); });
Unity调用代码
functions = FirebaseFunctions.DefaultInstance; functions.GetHttpsCallable("canplace").CallAsync().ContinueWith(task => { if (task.IsCompletedSuccessfully) { Debug.Log("finished"); Debug.Log(task.Result.Data); }else if (task.IsFaulted) { Debug.Log("Error: " + task.Exception.Message); }else if (task.IsCanceled) { Debug.Log("Canceled "); } });
错误日志
Error: System.AggregateException: One or more errors occurred. (INTERNAL) ---> Firebase.Functions.FunctionsException: INTERNAL at Firebase.Functions.HttpsCallableReference.<CallAsync>b__9_0 (System.Threading.Tasks.Task`1[TResult] task) [0x00057] in /Users/runner/work/firebase-unity-sdk/firebase-unity-sdk/functions/src/HttpsCallableReference.cs:88 at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2[TAntecedentResult,TResult].InnerInvoke () [0x00024] in <8f06425e63004caf99a79845675f751e>:0 at System.Threading.Tasks.Task.Execute () [0x00000] in <8f06425e63004caf99a79845675f751e>:0 --- End of inner exception stack trace --- ---> (Inner Exception #0) Firebase.Functions.FunctionsException: INTERNAL at Firebase.Functions.HttpsCallableReference.<CallAsync>b__9_0 (System.Threading.Tasks.Task`1[TResult] task) [0x00057] in /Users/runner/work/firebase-unity-sdk/firebase-unity-sdk/functions/src/HttpsCallableReference.cs:88 at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2[TAntecedentResult,TResult].InnerInvoke () [0x00024] in <8f06425e63004caf99a79845675f751e>:0 at System.Threading.Tasks.Task.Execute () [0x00000] in <8f06425e63004caf99a79845675f751e>:0 <--- UnityEngine.Debug:Log (object) FirebaseManager/<>c:<InitializeFirebase>b__15_0 (System.Threading.Tasks.Task`1<Firebase.Functions.HttpsCallableResult>) (at Assets/Scripts/FirebaseManager.cs:56) System.Threading._ThreadPoolWaitCallback:PerformWaitCallback ()
解决方案
1. 修正Cloud Functions代码的核心问题
你的函数代码存在两个致命问题:
- Node.js环境中没有
print函数,需替换为console.log。 - HTTP函数必须返回合法响应,否则请求会超时或返回内部错误,导致Unity端收到
INTERNAL异常。
方案A:兼容Firebase HttpsCallable的HTTP函数(使用functions-framework)
const functions = require('@google-cloud/functions-framework'); functions.http('canplace', (req, res) => { try { console.log("hi mark"); // 遵循Firebase Callable响应格式:返回包含data字段的JSON res.status(200).json({ data: "调用成功" }); } catch (err) { // 错误响应也需符合格式 res.status(500).json({ error: { message: err.message } }); } });
方案B:使用Firebase SDK编写标准Callable函数(更推荐)
如果不需要纯HTTP端点,直接用Firebase Functions SDK编写Callable函数,适配Unity的GetHttpsCallable调用:
const functions = require("firebase-functions"); exports.canplace = functions.https.onCall((data, context) => { console.log("hi mark"); // 直接返回数据,SDK会自动包装成Callable要求的格式 return { message: "调用成功" }; });
2. 调整Unity端调用代码
- 如果用方案A的HTTP函数:Unity不能直接用函数名调用,需使用函数的完整URL,替换
GetHttpsCallable为GetHttpsCallableFromUrl:
var functionUrl = "https://你的区域-你的项目ID.cloudfunctions.net/canplace"; var callable = functions.GetHttpsCallableFromUrl(functionUrl); callable.CallAsync().ContinueWith(task => { // 原逻辑不变 });
- 如果用方案B的标准Callable函数:原Unity代码无需修改,直接部署函数后即可调用。
3. 验证与排查
- 重新部署Cloud Functions,确保部署成功。
- 查看Firebase控制台的函数日志,确认函数是否被触发、是否执行无报错。
- 检查Unity项目的Firebase初始化是否正确,项目ID与Cloud Functions所属项目一致。
内容的提问来源于stack exchange,提问作者Keven Zanluchi
相关产品推荐
相关产品推荐

