Firebase/수업내용

Firebase Analytics 수업 내용 (21.07.08)

tjdgus9955 2021. 7. 8. 12:17

기본 이벤트 인수

 

App.cs

using System.Collections;
using UnityEngine;
using System.Collections.Generic;
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 txtAuthenicate;
    public Text test;
    public Text txtid;
    public Text txtUserName;
    public Text txtState;
    public Image thumb;
    public Button btnStart;
    public ePlayMode playMode;
    public Button btnTutorial;
    public Text txtUserId;
    public UIPopupTutorial popupTutorial;

    // Start is called before the first frame update
    void Start()
    {

        this.btnTutorial.onClick.AddListener(() =>
        {
            
        });
        this.test.text = Application.version;

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


        this.btnStart.onClick.AddListener(() =>
        {
            //씬전환
            //SceneManager.LoadScene("GameScenes");
            SceneManager.LoadSceneAsync("GameScenes").completed += (oper) =>
            {
                var gameMain = GameObject.FindObjectOfType<GameMain>();
                gameMain.Init(Social.localUser.userName);
            };
        });

        Debug.Log("-----------------> lnit GPGS");

        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
            .RequestIdToken()
            .Build();
        PlayGamesPlatform.InitializeInstance(config);
        PlayGamesPlatform.DebugLogEnabled = true;
        PlayGamesPlatform.Activate();
        Debug.Log("----------------->Authenticate");

        // authenticate user:
        PlayGamesPlatform.Instance.Authenticate(SignInInteractivity.CanPromptAlways, (result) => {
            // handle results

            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);

                    this.txtUserId.text = newUser.UserId;


                    this.popupTutorial.Init();
                    this.popupTutorial.Open();
                });
            }

            Debug.Log("----------------->" + result);
            Debug.Log("----------------->" + Social.localUser);
            Debug.Log("----------------->" + Social.localUser.authenticated);

            if (Social.localUser.authenticated)
            {


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

                Debug.Log("----------------->" + Social.localUser.image);

                StartCoroutine(this.WaitForLoadThumb(() => {
                    Debug.Log(Social.localUser.image);
                    Debug.LogFormat("{0}, {1}", Social.localUser.image.width, Social.localUser.image.height);
                    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();
                }));

                this.btnStart.gameObject.SetActive(true);
            }

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

UIPopUpTutorial.cs

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIPopupTutorial : MonoBehaviour
{
    public Button btnClose;
    public Button btnNext;
    public Text txtPageNum;
    private int currentPage = 1;
    private int totalPage = 5;

    public void Init()
    {
        this.btnNext.onClick.AddListener(() => {
            if (this.currentPage < this.totalPage)
            {
                this.currentPage++;
                var str = string.Format("{0}/{1}", this.currentPage, this.totalPage);
                Debug.Log(str);
                this.txtPageNum.text = str;
            }
        });

        this.btnClose.onClick.AddListener(() => {
            this.Close();
        });
    }

    public void Open()
    {
        this.gameObject.SetActive(true);
    }

    public void Close()
    {
        this.gameObject.SetActive(false);
        Firebase.Analytics.FirebaseAnalytics.LogEvent("end_tutorial", "page", this.currentPage);
    }
}