Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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...");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement