본문 바로가기
programming | development/unity

Unity에서 Firebase쓰기

by foooo828 2022. 1. 6.

유니티 셋업

https://firebase.google.com/docs/unity/setup

 

Unity 프로젝트에 Firebase 추가  |  Firebase Documentation

의견 보내기 Unity 프로젝트에 Firebase 추가 plat_ios plat_android plat_unity Firebase Unity SDK를 활용하여 Unity 게임을 업그레이드 해보세요. Firebase를 Unity 프로젝트에 연결하는 것이 얼마나 간편한지 보여드

firebase.google.com

 

로그 끄기

 

 

External Dependancy Manager > Version Handeler > Settings

Verbose Log 체크 해제

 

 

 

 

 

 

 

 

구글플레이 연동

https://firebase.google.com/docs/auth/unity/play-games

 

Unity에서 Google Play 게임 서비스를 사용하여 인증  |  Firebase Documentation

의견 보내기 Unity에서 Google Play 게임 서비스를 사용하여 인증 Google Play 게임 서비스를 사용하여 Firebase 및 Unity로 개발된 Android 게임에 플레이어가 로그인하도록 할 수 있습니다. Firebase를 통한 Goog

firebase.google.com

void Start()
{
    PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
        .RequestIdToken()
        .EnableSavedGames()
        .RequestServerAuthCode(false /* Don't force refresh */) // 이거 추가
        .Build();

    PlayGamesPlatform.InitializeInstance(config);
    PlayGamesPlatform.Activate();
}

게임 클라이언트 구성

Social.localUser.Authenticate(sucess =>
        {
            if (sucess)
            {
                FirebaseAuth(PlayGamesPlatform.Instance.GetServerAuthCode());           
            }
        });

플레이게임 로그인 성공시 플레이어 인증코드(authcode)로 아래 함수 호출

public void FirebaseAuth(string authcode)
{
    Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
    Firebase.Auth.Credential credential = 
        Firebase.Auth.PlayGamesAuthProvider.GetCredential(authcode);
    auth.SignInWithCredentialAsync(credential).ContinueWith(task =>
    {
        if (task.IsCanceled)
        {
            Debug.LogError("SignInWithCredentialAsync was canceled.");
            return;
        }

        if (task.IsFaulted)
        {
            Debug.LogError("SignInWithCredentialAsync encountered an error: " + task.Exception);
            return;
        }

        Firebase.Auth.FirebaseUser newUser = task.Result;
        Debug.LogFormat("User signed in successfully: {0} ({1})",
            newUser.DisplayName, newUser.UserId);
    });
}

 

 

데이터베이스

https://firebase.google.com/docs/database/unity/start?hl=ko 

 

Unity용 Firebase 실시간 데이터베이스 시작하기  |  Firebase Documentation

의견 보내기 Unity용 Firebase 실시간 데이터베이스 시작하기 Firebase 실시간 데이터베이스는 NoSQL 클라우드 데이터베이스를 사용하여 데이터를 저장하고 동기화합니다. 모든 클라이언트에서 실시간

firebase.google.com

 

 

 

데이터 검색

>public void GetVersionCode()
{
    reference = FirebaseDatabase.DefaultInstance.RootReference;
    reference.GetValueAsync().ContinueWith(task =>
    {
        if (task.IsCompleted)
        {
            DataSnapshot snapShot = task.Result;          
            UpdatePopupFunction(snapShotDic["key이름"]);             
        }
    });
}

처음에 비동기 메서드에대한 이해가 없어서 이런식으로 했다가 안됨. (Task 정리 : https://8trian8.tistory.com/49)

또 저 Task 안에서 SetActive를 쓰려고 하면 SetActive can only be called from the main thread 이렇게 경고뜸.

일단 ContinueWith() 이 아니라 ContinueWithOnMainThread() 를 써서 해결

 

https://firebase.google.com/docs/reference/unity/class/firebase/extensions/task-extension

 

 

참고 : https://dldbdud314.github.io/unity/unity-firebase/

 

댓글