-
PlayFab과 Unity 연동 (3) - 닉네임 Data 보내고 가져오기CS/졸업 프로젝트(Duk to Me) 2023. 9. 18. 20:12반응형
지난 단계까지 설정한 것들을 UI 디자인만 Project에 맞게 바꾼 화면 지난 단계까지는 Tutorial Youtube Video를 따라서 기본 기능들을 설정하였다. 이제는 현재 적용할려는 Unity Project의 필요에 맞게 수정하여 사용해보려고 한다.
① PlayFab에서는 기본적으로 계정별로 Profile에 Display Name을 등록할 수 있다. 그래서 Project에서 필요한 닉네임을 Field를 이 Display Name으로 대체하기로 하였다. Tutorial Video와는 다르게 필요에 의해서 'RegisterUser()' Method에 Display Name을 등록할 수 있도록 'LoginPagePlayfab' Script를 고치고, 원래의 로그인 성공시 다음 Scene으로 넘어가도록 정의한 'OnLoginSucceess()' Method를 Project 서비스 흐름대로 다음 Scene인 Character Selection Scene으로 넘어가도록 수정하기
using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; using PlayFab; using PlayFab.ClientModels; using System; using UnityEngine.SceneManagement; using UnityEngine.UI; public class LoginPagePlayfab : MonoBehaviour { [Header("Login")] [SerializeField] private TMP_InputField EmailLoginInput; [SerializeField] private TMP_InputField PasswordLoginput; [SerializeField] private GameObject LoginPage; [Header("Register")] [SerializeField] private TMP_InputField UsernameRegisterInput; [SerializeField] private TMP_InputField EmailRegisterInput; [SerializeField] private TMP_InputField PasswordRegisterInput; [SerializeField] private GameObject RegisterPage; [Header("Recovery")] [SerializeField] private TMP_InputField EmailRecoveryInput; [SerializeField] private GameObject RecoveryPage; private void Start() { } private void Update() { } #region Button Functions public void RegisterUser() { var request = new RegisterPlayFabUserRequest { DisplayName = UsernameRegisterInput.text, Email = EmailRegisterInput.text, Password = PasswordRegisterInput.text, RequireBothUsernameAndEmail = false }; PlayFabClientAPI.RegisterPlayFabUser(request, OnRegisterSuccess, OnError); } public void Login() { var request = new LoginWithEmailAddressRequest { Email = EmailLoginInput.text, Password = PasswordLoginput.text, }; PlayFabClientAPI.LoginWithEmailAddress(request, OnLoginSuccess, OnError); } // private void OnLoginSuccess(LoginResult result) // { // SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); // } public TMPro.TextMeshProUGUI t_logname; private void OnLoginSuccess(LoginResult result) { PlayFabClientAPI.GetPlayerProfile(new GetPlayerProfileRequest(), profileResult => { string displayName = profileResult.PlayerProfile.DisplayName; //t_logname.text = "displayName"; Debug.Log("Logged in as: " + displayName); // You can use the display name for further actions if needed. SceneManager.LoadScene("Selection"); // Change this line to load your "Selection" scene. }, OnError); } public void RecoverUser() { var request = new SendAccountRecoveryEmailRequest { Email = EmailRecoveryInput.text, TitleId = "BEF62", }; PlayFabClientAPI.SendAccountRecoveryEmail(request, OnRecoverySuccess, OnErrorRecovery); } private void OnErrorRecovery(PlayFabError result) { } private void OnRecoverySuccess(SendAccountRecoveryEmailResult obj) { OpenLoginPage(); } private void OnError(PlayFabError Error) { Debug.Log(Error.GenerateErrorReport()); } private void OnRegisterSuccess(RegisterPlayFabUserResult Result) { OpenLoginPage(); } public void OpenLoginPage() { LoginPage.SetActive(true); RegisterPage.SetActive(false); RecoveryPage.SetActive(false); } public void OpenRegisterPage() { LoginPage.SetActive(false); RegisterPage.SetActive(true); RecoveryPage.SetActive(false); } public void OpenRecoveryPage() { LoginPage.SetActive(false); RegisterPage.SetActive(false); RecoveryPage.SetActive(true); } #endregion }
② 위에서 등록한 Display Name을 출력할 첫번째 Scene인 'Character Selection' Scene에 적용할 'nick_selection.cs' C# Script를 생성하여 아래와 같이 작성하고 Scene에 Script 적용하기
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using PlayFab; using PlayFab.ClientModels; using UnityEngine.UI; public class nick_selection : MonoBehaviour { public TMPro.TextMeshProUGUI t_name; public void Start() { // Fetch and display PlayFab data when transitioning scenes FetchDataFromPlayFab(); } private void FetchDataFromPlayFab() { // Request to get the player's account info GetAccountInfoRequest request = new GetAccountInfoRequest(); // Send the request to PlayFab PlayFabClientAPI.GetAccountInfo(request, OnGetAccountInfoSuccess, OnGetAccountInfoFailure); } private void OnGetAccountInfoSuccess(GetAccountInfoResult result) { // Fetch and display user's display name string displayName = result.AccountInfo.TitleInfo.DisplayName; //Debug.Log("Fetched Display Name: " + displayName); t_name.text = displayName; // Fetch and display user's score FetchScoreFromPlayFab(); } private void OnGetAccountInfoFailure(PlayFabError error) { Debug.LogError("Failed to fetch PlayFab account info: " + error.ErrorMessage); } }
③ 위에서 등록한 Display Name을 출력할 두번째 Scene인 'My Page' Scene에 적용할 'nick_mypage.cs' C# Script를 생성하여 아래와 같이 작성하고 Scene에 Script 적용하기
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using PlayFab; using PlayFab.ClientModels; using UnityEngine.UI; public class nick_mypage : MonoBehaviour { public TMPro.TextMeshProUGUI t_name, t_score1; public void Start() { // Fetch and display PlayFab data when transitioning scenes FetchDataFromPlayFab(); } private void FetchDataFromPlayFab() { // Request to get the player's account info GetAccountInfoRequest request = new GetAccountInfoRequest(); // Send the request to PlayFab PlayFabClientAPI.GetAccountInfo(request, OnGetAccountInfoSuccess, OnGetAccountInfoFailure); } private void OnGetAccountInfoSuccess(GetAccountInfoResult result) { // Fetch and display user's display name string displayName = result.AccountInfo.TitleInfo.DisplayName; //Debug.Log("Fetched Display Name: " + displayName); t_name.text = displayName; // Fetch and display user's score FetchScoreFromPlayFab(); } private void OnGetAccountInfoFailure(PlayFabError error) { Debug.LogError("Failed to fetch PlayFab account info: " + error.ErrorMessage); } }
닉네임 출력결과
Character Selection Scene My Page Scene 반응형'CS > 졸업 프로젝트(Duk to Me)' 카테고리의 다른 글
ChatGPT 설명및 Unity와의 연동 (1) 2023.11.06 PlayFab과 Unity 연동 (4) - 게임 점수 Data 보내고 가져오기 (0) 2023.09.18 PlayFab과 Unity 연동 (2) - 로그인, 회원가입, 비밀번호 찾기 기능 구현 (0) 2023.09.18 PlayFab 설명및 Unity와의 연동 (1) - UI 만들기, 기초설정, PlayFab 계정만들기 (0) 2023.09.17 Unity에서 여러 Scene으로부터 점수를 합산하여 내는 점수체계 C# Script 코드 쓰기 (0) 2023.08.16