简体   繁体   English

Unity中的Google Play服务是否将数据存储在Unity内的某处?

[英]Does Google Play Services in Unity, store data somewhere inside Unity?

I need help with Google Play Games plugin in unity. 我需要有关Google Play游戏插件的帮助。

My game have some scenes (1 - menu, 5 - game scenes). 我的游戏有一些场景(1-菜单,5-游戏场景)。 I downloaded plugin and write code, that initializing play games and sign user in. After this, I'm using firebase initialization and login with Play Games. 我下载了插件并编写了代码,用于初始化Play游戏并登录用户。此后,我正在使用Firebase初始化并使用Play Games登录。

Everything works fine, when I'm starting my game (I checked logs with logcat and it shows my login and message that sign in was successful both for firebase and play games services). 当我开始游戏时,一切正常(我使用logcat检查了日志,并显示了登录名和消息,表明登录Firebase和Play游戏服务均成功)。 My script that controls firebase and google play service contains DontDestroyOnLoad and it Destroys duplicates. 我控制firebase和Google Play服务的脚本包含DontDestroyOnLoad并销毁重复项。

When I'm returning from game scene to menu again, logcat saying that that was error and it can't login. 当我再次从游戏场景返回菜单时,logcat表示那是错误且无法登录。 I can press log out (on button) and log in again, and it will be good. 我可以按登出(按钮)并再次登录,这会很好。

So, can someone answer me question, should my PlayServices walk to other scenes with DontDestroyOnLoad, or it stores data somewhere in Unity libraries (SocialPlatform)? 那么,有人可以回答我的问题吗,我的PlayServices应该使用DontDestroyOnLoad转到其他场景,还是将数据存储在Unity库(SocialPlatform)中? I can't submit achievements on game scene and I can't doing other things with this... 我无法在游戏场景中提交成就,也无法以此做其他事情...

Thanks 谢谢

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using Firebase;
using Firebase.Auth;
using Firebase.Analytics;
using UnityEngine.SocialPlatforms;
using UnityEngine.SceneManagement;
using TMPro;


public class GooglePlayServices : MonoBehaviour
{
    private GPG_CloudSaveSystem cloudSaveSystem = new GPG_CloudSaveSystem();
    private string authCode;
    private bool firebaseInitialized;
    DependencyStatus dependencyStatus = DependencyStatus.UnavailableOther;
    public static GooglePlayServices instance;
    private bool isFirebaseLogged, isPlayGamesLogged;
    FirebaseAuth auth;
    FirebaseUser user;
    private string userNametest, userNameId;

    void Awake()
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(this.gameObject);
        }
        else
        {
            DestroyImmediate(gameObject);
        }

        auth = FirebaseAuth.DefaultInstance;
        PlayGamesClientConfiguration config = new
            PlayGamesClientConfiguration.Builder()
            .EnableSavedGames()
            .RequestServerAuthCode(false)
            .Build();

        // Enable debugging output (recommended)
        PlayGamesPlatform.DebugLogEnabled = true;

        // Initialize and activate the platform
        PlayGamesPlatform.InitializeInstance(config);
        PlayGamesPlatform.Activate();
        if (!isPlayGamesLogged)
        {
            PlayGamesPlatform.Instance.Authenticate(SignInCallback, false);
        }
    }

    private void Start()
    {
        Debug.Log("User name: " + userNametest + " | User ID: " + userNameId);
    }

    void LoginFirebase()
    {
        if (Social.localUser.authenticated)
        {
            authCode = PlayGamesPlatform.Instance.GetServerAuthCode();
        }
        else
        {
            PlayGamesPlatform.Instance.Authenticate(SignInCallback, false);
            authCode = PlayGamesPlatform.Instance.GetServerAuthCode();
        }

        //FirebaseAuth auth = FirebaseAuth.DefaultInstance;

        FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
        // Set the user's sign up method.
        FirebaseAnalytics.SetUserProperty(
          FirebaseAnalytics.UserPropertySignUpMethod,
          "PlayGames");

        Credential credential = 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;
            }

            FirebaseUser newUser = task.Result;
            userNametest = newUser.DisplayName;
            userNameId = newUser.UserId;
            Debug.LogFormat("User signed in successfully: {0} ({1})",
                newUser.DisplayName, newUser.UserId);
            // Set the user ID.
            FirebaseAnalytics.SetUserId(newUser.UserId);
            isFirebaseLogged = true;
        });


        // Set default session duration values.
        FirebaseAnalytics.SetMinimumSessionDuration(new TimeSpan(0, 0, 10));
        FirebaseAnalytics.SetSessionTimeoutDuration(new TimeSpan(0, 30, 0));
        firebaseInitialized = true;
    }

    public void EventLevelFinished(int levelNumber)
    {
        string eventLog = "Level_finished_" + (levelNumber + 1).ToString();
        switch (levelNumber)
        {
            case 2:
                {
                    FirebaseAnalytics.LogEvent(eventLog);
                    break;
                }
            case 9:
                {
                    FirebaseAnalytics.LogEvent(eventLog);
                    break;
                }
            case 19:
                {
                    FirebaseAnalytics.LogEvent(eventLog);
                    break;
                }
            case 29:
                {
                    FirebaseAnalytics.LogEvent(eventLog);
                    break;
                }
            case 39:
                {
                    FirebaseAnalytics.LogEvent(eventLog);
                    break;
                }
            case 49:
                {
                    FirebaseAnalytics.LogEvent(eventLog);
                    break;
                }
            case 59:
                {
                    FirebaseAnalytics.LogEvent(eventLog);
                    break;
                }
        }
    }

    public void EventLogCustomEvent(string log)
    {
        FirebaseAnalytics.LogEvent(log);
    }

    public string GetFirebaseUser()
    {
        return user.DisplayName + " | " + user.UserId;
    }

    public void SaveToCloud()
    {
        JsonToCloud.instance.BuildSaveString();
        cloudSaveSystem.saveString = Prefs.CloudSaveString;
        cloudSaveSystem.SaveToCloud();
    }

    public void LoadFromCloud()
    {
        cloudSaveSystem.LoadFromCloud();
        Prefs.CloudSaveString = cloudSaveSystem.saveString;
        JsonToCloud.instance.StringToData();
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void ShowSavedGames()
    {
        cloudSaveSystem.showUI();
    }

    public void AnalyticsLogin()
    {
        FirebaseAnalytics.LogEvent(FirebaseAnalytics.EventLogin);
    }

    public void AnalyticsProgress()
    {
        FirebaseAnalytics.LogEvent("progress", "percent", 0.4f);
    }

    void InitializeFirebase()
    {
        FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
        // Set the user's sign up method.
        FirebaseAnalytics.SetUserProperty(
          FirebaseAnalytics.UserPropertySignUpMethod,
          "PlayGames");
        // Set the user ID.
        FirebaseAnalytics.SetUserId("uber_user_510");
        // Set default session duration values.
        FirebaseAnalytics.SetMinimumSessionDuration(new TimeSpan(0, 0, 10));
        FirebaseAnalytics.SetSessionTimeoutDuration(new TimeSpan(0, 30, 0));
        firebaseInitialized = true;
    }

    public void ShowAchievements()
    {
        if (PlayGamesPlatform.Instance.localUser.authenticated)
        {
            PlayGamesPlatform.Instance.ShowAchievementsUI();
        }
        else
        {
            Debug.Log("Cannot show Achievements, not logged in");
        }
    }

    public void SignIn()
    {
        if (!PlayGamesPlatform.Instance.localUser.authenticated)
        {
            // Sign in with Play Game Services, showing the consent dialog
            // by setting the second parameter to isSilent=false.
            PlayGamesPlatform.Instance.Authenticate(SignInCallback, false);
        }
        else
        {
            // Sign out of play games
            PlayGamesPlatform.Instance.SignOut();
        }
        AnalyticsLogin();
    }

    public void SignInCallback(bool success)
    {
        if (success)
        {
            isPlayGamesLogged = true;
            Debug.Log("(Lollygagger) Signed in!");
            authCode = PlayGamesPlatform.Instance.GetServerAuthCode();
            ////////////////////////////////
            FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
            {
                dependencyStatus = task.Result;
                if (dependencyStatus == DependencyStatus.Available)
                {
                    if (!isFirebaseLogged)
                    {
                        LoginFirebase();
                    }
                }
                else
                {
                    Debug.LogError(
                      "Could not resolve all Firebase dependencies: " + dependencyStatus);
                }
            });
            /////////////////////////////////////////
        }
        else
        {
            Debug.Log("(Lollygagger) Sign-in failed...");
        }
    }
}

github问题上的一个人回答了我,不需要使用DontDestroyOnLoad,只需要在主场景中使用一次即可,仅此而已

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM