게임 플랫폼 응용프로그래밍

06/30 파이어베이스(Firebase) 사용 연습

박준희 2021. 6. 30. 12:34

파이어베이스

실시간 데이터베이스

클라우드 저장

애널리틱스

 

https://minquu.tistory.com/202

 

0630_ 서버(FireBase)

파이어 베이스를 한다. 2가지 할 것이다. 1.실시간 데이터 베이스 2.구글 애널리틱스 ---- 먼저 앱 등록을 해야한다. https://firebase.google.com/?gclid=CjwKCAjwieuGBhAsEiwA1Ly_nbYl-Mw1oHrXWl7cFWEJFVQFEBwdM..

minquu.tistory.com

 

app.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using UnityEngine.SocialPlatforms;
using UnityEngine.SceneManagement;

public class App : MonoBehaviour
{
    public enum ePlayMode
    {
        TEST, BUILD
    }

    public Text versionText;
    public Text txtid;
    public Text txtUserName;
    public Text txtState;
    public Text txtAuthenicate;
    public Image thumb;
    public Button btnStart;
    public ePlayMode playMode;
    void Start()
    {
        versionText.text = Application.version;

        if (this.playMode == ePlayMode.TEST)
            this.btnStart.gameObject.SetActive(true);
        else
            this.btnStart.gameObject.SetActive(false);

        //this.btnStart.gameObject.SetActive(false);
        this.btnStart.onClick.AddListener(() =>
        {
            SceneManager.LoadScene("GameScene");
            SceneManager.LoadSceneAsync("GameScene").completed += (oper) =>
            {
                var gameMain = GameObject.FindObjectOfType<GameMain>();
                gameMain.Init(Social.localUser.id);
            };
        });

        Debug.Log("================================> Init GPGS");

        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
            .EnableSavedGames()
            .RequestIdToken()
            .Build();

        PlayGamesPlatform.InitializeInstance(config);
        // recommended for debugging:
        PlayGamesPlatform.DebugLogEnabled = true;
        // Activate the Google Play Games platform
        PlayGamesPlatform.Activate();

        Debug.Log("================================> Authenticate");

        // authenticate user:
        PlayGamesPlatform.Instance.Authenticate(SignInInteractivity.CanPromptAlways, (result) =>
        {
            this.txtAuthenicate.text = result.ToString();
            if (result == SignInStatus.Success)
            {
                var localUer = (PlayGamesLocalUser)Social.localUser;
                var googleIdToken = localUer.GetIdToken();
                Debug.LogFormat("googleIdToken : {0}", googleIdToken);

                Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
                Firebase.Auth.Credential credential =
                Firebase.Auth.GoogleAuthProvider.GetCredential(googleIdToken, null);
                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);
                });
            }

            // handle results
            Debug.Log("================================>" + result);
            Debug.Log("================================>" + Social.localUser);
            Debug.Log("================================>" + Social.localUser.authenticated);

            this.txtid.text = Social.localUser.id;
            this.txtUserName.text = Social.localUser.userName;
            this.txtState.text = Social.localUser.state.ToString();
            

            StartCoroutine(this.WaitForLoadThumb(() =>
            {
                Debug.Log(Social.localUser.image);
                
                this.thumb.sprite = Sprite.Create(Social.localUser.image, new Rect(0, 0, Social.localUser.image.width, Social.localUser.image.height), Vector2.zero);
                this.thumb.SetNativeSize();
            }));

        });
    }

    private IEnumerator WaitForLoadThumb(System.Action callback)
    {
        while(true)
        {
            if(Social.localUser.image != null)
            {
                break;
            }
            yield return null;
        }
        callback();
    }
}

 

adb connect 127.0.0.1:62001

adb logcat -s Unity