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

Unity C#中Firebase PhoneAuthOptions.Builder不存在的问题求助

解决Unity Firebase电话认证中PhoneAuthOptions无Builder定义的问题

错误原因

你混淆了Firebase原生平台(Android/iOS)与Unity SDK的API差异。在Unity Firebase Auth 11.0.0版本中,PhoneAuthOptions.Builder的实例不能通过new关键字创建,而是需要调用静态方法PhoneAuthOptions.NewBuilder()来获取构建器。

另外,Unity中无需手动调用SetActivity(this)——Firebase SDK会自动关联当前Unity应用的Android/iOS宿主活动,强行传入MonoBehaviour实例会导致额外错误。

修正后的代码

using Firebase.Auth;
using UnityEngine;

private void VerifyPhoneNumber()
{
    phoneAuthProvider = PhoneAuthProvider.GetInstance(firebaseAuth);

    // 核心修正:用NewBuilder替代new Builder()
    PhoneAuthOptions options = PhoneAuthOptions.NewBuilder(FirebaseAuth.DefaultInstance)
        .SetPhoneNumber(phoneNumber)
        .SetCallbacks(this) // 确保当前类实现了PhoneAuthProvider.ICallbacks接口
        .Build();

    phoneAuthProvider.VerifyPhoneNumber(options,
      verificationCompleted: (credential) =>
      {
          Debug.Log("Verification complete: " + credential);
      },
      verificationFailed: (error) =>
      {
          Debug.Log("The verification code was not sent: " + error);
      },
      codeSent: (id, token) =>
      {
          verificationId = id;
          verificationMessagePanel.SetActive(true);
          phoneNumberVerificationPanel.SetActive(false);
          Debug.Log("Verification code was successfully sent via SMS: " + id);
      },
      codeAutoRetrievalTimeOut: (id) =>
      {
          Debug.Log("`id` contains the verification id of the request that timed out: " + id);
      });
}

额外检查项

  • 确保脚本顶部已引用命名空间:using Firebase.Auth;
  • 在Unity Package Manager中验证:
    • Firebase Authentication包已安装(版本11.0.0)
    • Firebase Core包版本与Auth包完全一致(版本不匹配会导致API异常)
  • 确认当前类已实现PhoneAuthProvider.ICallbacks接口,否则SetCallbacks(this)会报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 01:13:00