You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity调用Firebase Functions HelloWorld示例遇内部错误求助

问题根源与解决思路

你遇到的核心问题是Firebase云函数类型不匹配:你部署的是onRequest类型的普通HTTP函数,但Unity中使用的GetHttpsCallable方法仅能调用onCall类型的Callable云函数,两者协议不兼容,导致出现内部错误。

解决方案1:将云函数改为onCall类型

修改Node.js代码为Callable函数格式,Firebase会自动处理请求/响应序列化、身份验证等逻辑,适配Unity的HttpsCallable调用:

const functions = require("firebase-functions");

exports.helloWorld = functions.https.onCall((data, context) => {
  functions.logger.info("Hello logs!", {structuredData: true});
  return "Hello from Firebase!";
});

部署修改后的函数后,原Unity代码即可正常工作。

解决方案2:Unity改用普通HTTP请求调用onRequest函数

若不想修改云函数,可在Unity中用UnityWebRequest直接发起HTTP请求,调用函数的公开URL:

using UnityEngine;
using UnityEngine.Networking;

public class CloudFunctionCaller : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(CallHelloWorld());
    }

    IEnumerator CallHelloWorld()
    {
        string url = "https://us-central1-infinite-space-rpg.cloudfunctions.net/helloWorld";
        using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
        {
            yield return webRequest.SendWebRequest();

            if (webRequest.result == UnityWebRequest.Result.Success)
            {
                Debug.Log("result: " + webRequest.downloadHandler.text);
            }
            else
            {
                Debug.LogError("Error: " + webRequest.error);
            }
        }
    }
}

额外排查点

  • 确保Unity中Firebase SDK初始化完成:调用函数前先执行FirebaseApp.CheckAndFixDependenciesAsync(),等待依赖初始化完成。
  • 检查云函数权限:确认函数的IAM权限已设置为allUsers拥有cloudfunctions.functions.invoke权限。
  • 查看Unity网络日志:开启Unity网络日志(Edit > Project Settings > Player > Other Settings > Logging > Enable Network Logging),获取更详细的HTTP请求/响应信息辅助排查。

内容的提问来源于stack exchange,提问作者FlyVC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 13:55:23