メインコンテンツまでスキップ

すべてを統合する - デバイス ID でログインする - (Unity モジュール)

Last updated on November 1, 2024

Connect the UI to let player log in with device ID

In this tutorial, you will learn how to connect the login menu user interface (UI) with the login methods you implemented previously. You are going to get the AuthEssentialsWrapper_Starter class reference, set up the callback function OnLoginCompleted in LoginHandler_Starter, and start the login process from the existing LoginHandler.Login() method you created.

  1. Open Byte Wars in Unity. From Project window, open /Assets/Resources/Modules/AuthEssentials/Scripts/UI/LoginHandler_Starter.cs.

  2. Create a new callback function for OnLoginCompleted in the LoginHandler_Starter file. Based on the ResultCallback, set to change the screen to the MainMenuCanvas using the in-game utility MenuManager on login success and set to change the view to LoginFailed on login failed.

    public void OnLoginCompleted(Result<TokenData, OAuthError> result)
    {
    if (!result.IsError)
    {
    onLoginCompleted.Invoke(result.Value);
    MenuManager.Instance.ChangeToMenu(AssetEnum.MainMenuCanvas);
    BytewarsLogger.Log($"[LoginHandler.OnLoginCompleted] success: {result.Value.ToJsonString()}");
    }
    else
    {
    failedMessageText.text = $"Login Failed: {result.Error.error}";
    CurrentView = LoginView.LoginFailed;
    StartCoroutine(SetSelectedGameObject(retryLoginButton.gameObject));
    }
    }
  3. To access the AuthEssentialsWrapper_Starter class, you can use TutorialModuleManager to get the reference easily, then store it to a local variable in the script's Start().

    private AuthEssentialsWrapper_Starter authWrapper;
    private void Start()
    {
    authWrapper = TutorialModuleManager.Instance.GetModuleClass<AuthEssentialsWrapper_Starter>();
    ...
    }
  4. To enable retrying logins with the same method, prepare a local variable to store the last login method.

    private LoginType lastLoginMethod;
  5. Update the LoginHandler_Starter.Login() function by adding the LoginType as a parameter. Inside the function, store the login method and call the AuthEssentialsWrapper_Starter.LoginWithDeviceId() right after changing the view to LoginLoading you created previously.

    private void Login(LoginType loginMethod)
    {
    CurrentView = LoginView.LoginLoading;
    lastLoginMethod = loginMethod;
    OnRetryLoginClicked = OnRetryLoginButtonClicked;
    authWrapper.LoginWithDeviceId(OnLoginCompleted);
    }
  6. Since you updated the Login() function, you also need to update the Login() function calls inside OnLoginWithDeviceIdButtonClicked() and OnRetryLoginButtonClicked() that are listening to the onClick event of loginWithDeviceIdButton and quitGameButton, respectively, that you created earlier.

    private void OnLoginWithDeviceIdButtonClicked()
    {
    Login(LoginType.DeviceId);
    }
    private void OnRetryLoginButtonClicked()
    {
    Login(lastLoginMethod);
    }