ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Playfab와 Unity 연동 (6) - 닉네임 수정 기능 구현
    CS/졸업 프로젝트(Duk to Me) 2023. 12. 9. 22:39
    반응형

     

        이전 단계까지 진행한 Playfab 설정들은 닉네임 수정 기능이 구현되어 있지 않았다. 오늘은 Unity Project의 Settings Scene에서 필요한 닉네임 수정 기능을 구현해보자.

     


     

    ① Settings Scene에 적용될 'Setting'라는 C# Script파일을 생성해주어 다음과 같이 내용을 작성해준다. 이 Script Code 내용들이 Settings Scene에 사용자 Nickname 정보를 Playfab DB에서 nick 변수로 불러와서 화면에 출력하고 'Update' Button이 Click 되었을 때 displayNameInput이라는 명으로 지정된 Inputfield안에 있는 내용대로 Nickname 정보를 Update 시켜주도록 지정해주고 있다. 

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    using PlayFab;
    using PlayFab.ClientModels;
    using UnityEngine.UI;
    using TMPro;
    
    public class Setting : MonoBehaviour
    {
        public TMP_Text nick;
        public TMP_InputField displayNameInput; // Input field for the new display name
    
        // Start is called before the first frame update
        void Start()
        {
            // Fetch and display user's current display name
            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;
            nick.text = displayName;
        }
    
        private void OnGetAccountInfoFailure(PlayFabError error)
        {
            Debug.LogError("Failed to fetch PlayFab account info: " + error.ErrorMessage);
        }
    
        // This method is called when the update button is clicked
        public void UpdateDisplayName()
        {
            string newDisplayName = displayNameInput.text; // Get the new display name from the input field
    
            // Update the display name in PlayFab
            UpdateUserTitleDisplayNameRequest request = new UpdateUserTitleDisplayNameRequest
            {
                DisplayName = newDisplayName
            };
    
            PlayFabClientAPI.UpdateUserTitleDisplayName(request, OnUpdateDisplayNameSuccess, OnUpdateDisplayNameFailure);
        }
    
        private void OnUpdateDisplayNameSuccess(UpdateUserTitleDisplayNameResult result)
        {
            Debug.Log("Display name updated successfully!");
            // Optionally, you can update the displayed name on the UI to show the new name
            nick.text = result.DisplayName;
        }
    
        private void OnUpdateDisplayNameFailure(PlayFabError error)
        {
            Debug.LogError("Failed to update display name: " + error.ErrorMessage);
        }
    }

     

    ② 이제 Settings Scene에 Script를 적용시켜 보자. 해당 Scene을 열어 Main Camera에 'Setting' Script를 Drag&Drop하여 변수 빈칸에 Nickname을 출력하는 Placeholder와 새로운 Nickname을 입력하는 Inputfield Object를 Drag&Drop으로 적용시켜 준다.

     

    ③ 이제 'update' Button에 Script 함수를 적용시켜 보자. 'update' Object를 눌러 Inspector를 열고 On Click() 에 'Setting' Script가 적용되어 있는 Main Camera를 Drag&Drop하여 거기서 'update' Button이 눌렸을 때 동작 함수를 정의한 'UpdateDisplayName()' 함수를 찾아 지정해준다.

     

    이로써 Settings Scene의 Nickname 수정 기능 구현을 마친다.

     


     

    닉네임 수정 기능 구현 결과

     

     

    반응형
Designed by Tistory.