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
相关产品推荐
相关产品推荐

