为了账号安全,请及时绑定邮箱和手机立即绑定

场景变化时鉴权失败

场景变化时鉴权失败

C#
撒科打诨 2022-11-21 15:57:06
我设置了我的 firebase 身份验证,它运行良好。但是,当我加载不同的场景并返回欢迎场景后,身份验证失败。场景切换时,如何重新授权或保持登录状态?我的授权欢迎场景代码:public void Start()    {        InitializeFirebase();        InitializePlayGamesPlatform();        SignInPlayGames();            }public void InitializeFirebase()    {        Debug.Log("UserManager: Setting up Firebase Auth");        auth = Firebase.Auth.FirebaseAuth.DefaultInstance;        auth.StateChanged += AuthStateChanged;        auth.IdTokenChanged += IdTokenChanged;        // Specify valid options to construct a secondary authentication object.        if (otherAuthOptions != null &&            !(String.IsNullOrEmpty(otherAuthOptions.ApiKey) ||              String.IsNullOrEmpty(otherAuthOptions.AppId) ||              String.IsNullOrEmpty(otherAuthOptions.ProjectId)))        {            try            {                otherAuth = Firebase.Auth.FirebaseAuth.GetAuth(Firebase.FirebaseApp.Create(                  otherAuthOptions, "Secondary"));                otherAuth.StateChanged += AuthStateChanged;                otherAuth.IdTokenChanged += IdTokenChanged;            }            catch (Exception)            {                Debug.Log("UserManager: ERROR: Failed to initialize secondary authentication object.");            }        }        AuthStateChanged(this, null);            }public void InitializePlayGamesPlatform()    {        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()            .RequestServerAuthCode(false)            .Build();        PlayGamesPlatform.InitializeInstance(config);        PlayGamesPlatform.Activate();        auth = FirebaseAuth.DefaultInstance;    }
查看完整描述

2 回答

?
慕容森

TA贡献1853条经验 获得超18个赞

我尝试了很多但我没有得到想要的行为(关于重新授权/应用程序不退出)

即使我改变场景,凭据仍然存在。

如果您想更改帐户,

重新启动应用程序,然后在欢迎场景中注销(facebook 或 google)


查看完整回答
反对 回复 2022-11-21
?
浮云间

TA贡献1829条经验 获得超3个赞

假设它第一次按预期工作,听起来DontDestroyOnLoad就是您要找的东西:

切换场景时不会破坏该对象 -> 因此不会Start再次运行该方法。但是,您还需要将它与singleton模式结合起来,以确保在返回第一个场景时不会再次添加/运行它:

public class AuthComponent : MonoBehaviour

{

    private static AuthComponent singleton;


    private void Awake()

    {

        // Check if already another AuthComponent running

        if(singleton)

        {

            Debug.Log("Already another AuthComponent running");

            Destroy(gameObject);

            return;

        }


        // Otherwise store a global reference of this AuthComponent

        singleton = this;


        // and make it DontDestroyOnLoad

        DontDestroyOnLoad(gameObject);

    }


    ...

}


查看完整回答
反对 回复 2022-11-21
  • 2 回答
  • 0 关注
  • 63 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信