Reputation: 1
I need help with Google Play Games plugin in unity.
My game have some scenes (1 - menu, 5 - game scenes). 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.
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). My script that controls firebase and google play service contains DontDestroyOnLoad and it Destroys duplicates.
When I'm returning from game scene to menu again, logcat saying that that was error and it can't login. 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)? 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...");
}
}
}
Upvotes: 0
Views: 476
Reputation: 1
One guy on github issues answered me, that there is no need to use DontDestroyOnLoad, just use once at main scene and that's all
Upvotes: 0