유니티 플레이팹 게스트 로그인 Sign In with Guest 간단 사용법

반응형

유니티 플레이팹 게스트 로그인 Sign In with Guest 간단 사용법

using PlayFab;
using PlayFab.ClientModels;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class PlayfabManager : MonoBehaviour
{
    // 플레이어 정보 저장 변수
    private string customId = "";
    private string playfabId = "";
    private string entityId = "";
    private string entityType = "";

    // 게스트 로그인 버튼
    public void OnClickGuestLogin() 
    {
        // PlayerPrefs에서 저장된 ID 불러오기
        customId = PlayerPrefs.GetString("GuestID", "");
        
        // 저장된 ID가 없으면 새로 만들고, 있으면 그걸로 로그인
        if (string.IsNullOrEmpty(customId))
            CreateGuestId();
        else
            LoginGuestId();
    }

    // 새 게스트 ID 생성하기
    private void CreateGuestId() 
    {
        // 랜덤 ID 생성
        customId = GetRandomPassword(16);
        
        // 새 계정으로 로그인
        PlayFabClientAPI.LoginWithCustomID(new LoginWithCustomIDRequest()
        {
            CustomId = customId,
            CreateAccount = true
        }, 
        result => {
            // 성공하면 ID 저장하고 결과 처리
            PlayerPrefs.SetString("GuestID", customId);
            PlayerPrefs.Save();
            OnLoginSuccess(result);
        }, 
        error => {
            Debug.LogError("게스트 계정 생성 실패: " + error.ErrorMessage);
        });
    }

    // 랜덤 ID 생성 함수 (이전과 동일)
    private string GetRandomPassword(int _totLen) 
    {
        string input = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        var chars = Enumerable.Range(0, _totLen)
            .Select(x => input[UnityEngine.Random.Range(0, input.Length)]);
        return new string(chars.ToArray());
    }

    // 기존 게스트 ID로 로그인
    private void LoginGuestId() 
    {
        Debug.Log("저장된 게스트 ID로 로그인 시도: " + customId);
        
        PlayFabClientAPI.LoginWithCustomID(new LoginWithCustomIDRequest()
        {
            CustomId = customId,
            CreateAccount = false
        }, 
        result => {
            OnLoginSuccess(result);
        }, 
        error => {
            Debug.LogError("게스트 로그인 실패, 새 계정 생성 시도: " + error.ErrorMessage);
            // 로그인 실패시 새 계정 생성 시도
            CreateGuestId();
        });
    }

    // 로그인 성공 처리
    public void OnLoginSuccess(LoginResult result) 
    {
        Debug.Log("PlayFab 로그인 성공!");
        
        // 플레이어 정보 저장
        playfabId = result.PlayFabId;
        entityId = result.EntityToken.Entity.Id;
        entityType = result.EntityToken.Entity.Type;
        
        // 여기에 로그인 성공 후 처리 추가 (예: UI 업데이트 등)
    }
    
    // 로그아웃 함수 추가
    public void Logout()
    {
        PlayFabClientAPI.ForgetAllCredentials();
        Debug.Log("로그아웃 완료");
    }
}

 


참고할만한 글

 

유니티 플레이팹 구글 로그인 Playfab Sign In with Google Login 간단 구현

구글 로그인 SDK 설치 https://github.com/playgameservices/play-games-plugin-for-unity/tree/master/current-build GitHub - playgameservices/play-games-plugin-for-unity: Google Play Games plugin for Unity Google Play Games plugin for Unity. Contribute t

parksh3641.tistory.com

 

유니티 플레이팹 애플 로그인 Playfab Sign ln with Apple Login 간단 구현

애플 SDK https://github.com/lupidan/apple-signin-unity/releases/tag/v1.4.2 Release Sign in with Apple Unity Plugin v1.4.2 · lupidan/apple-signin-unity Changed Handles empty NSPersonNameComponents sent by Apple when not requesting a name, to be nil nativ

parksh3641.tistory.com

 

 

 

반응형