Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.15 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. using UnityEngine;
  6. using GooglePlayGames;
  7. using GooglePlayGames.BasicApi;
  8. using Firebase;
  9. using Firebase.Auth;
  10. using Firebase.Analytics;
  11. using UnityEngine.SocialPlatforms;
  12. using UnityEngine.SceneManagement;
  13. using TMPro;
  14.  
  15.  
  16. public class GooglePlayServices : MonoBehaviour
  17. {
  18.     private GPG_CloudSaveSystem cloudSaveSystem = new GPG_CloudSaveSystem();
  19.     private string authCode;
  20.     private bool firebaseInitialized;
  21.     DependencyStatus dependencyStatus = DependencyStatus.UnavailableOther;
  22.     public static GooglePlayServices instance;
  23.     private bool isFirebaseLogged, isPlayGamesLogged;
  24.     FirebaseAuth auth;
  25.     FirebaseUser user;
  26.     private string userNametest, userNameId;
  27.  
  28.     void Awake()
  29.     {
  30.         if (instance == null)
  31.         {
  32.             instance = this;
  33.             DontDestroyOnLoad(this.gameObject);
  34.         }
  35.         else
  36.         {
  37.             DestroyImmediate(gameObject);
  38.         }
  39.        
  40.         auth = FirebaseAuth.DefaultInstance;
  41.         PlayGamesClientConfiguration config = new
  42.             PlayGamesClientConfiguration.Builder()
  43.             .EnableSavedGames()
  44.             .RequestServerAuthCode(false)
  45.             .Build();
  46.  
  47.         // Enable debugging output (recommended)
  48.         PlayGamesPlatform.DebugLogEnabled = true;
  49.  
  50.         // Initialize and activate the platform
  51.         PlayGamesPlatform.InitializeInstance(config);
  52.         PlayGamesPlatform.Activate();
  53.         if (!isPlayGamesLogged)
  54.         {
  55.             PlayGamesPlatform.Instance.Authenticate(SignInCallback, false);
  56.         }
  57.     }
  58.  
  59.     private void Start()
  60.     {
  61.         Debug.Log("User name: " + userNametest + " | User ID: " + userNameId);
  62.     }
  63.  
  64.     void LoginFirebase()
  65.     {
  66.         if (Social.localUser.authenticated)
  67.         {
  68.             authCode = PlayGamesPlatform.Instance.GetServerAuthCode();
  69.         }
  70.         else
  71.         {
  72.             PlayGamesPlatform.Instance.Authenticate(SignInCallback, false);
  73.             authCode = PlayGamesPlatform.Instance.GetServerAuthCode();
  74.         }
  75.  
  76.         //FirebaseAuth auth = FirebaseAuth.DefaultInstance;
  77.  
  78.         FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
  79.         // Set the user's sign up method.
  80.         FirebaseAnalytics.SetUserProperty(
  81.           FirebaseAnalytics.UserPropertySignUpMethod,
  82.           "PlayGames");
  83.  
  84.         Credential credential = PlayGamesAuthProvider.GetCredential(authCode);
  85.         auth.SignInWithCredentialAsync(credential).ContinueWith(task =>
  86.         {
  87.             if (task.IsCanceled)
  88.             {
  89.                 Debug.LogError("SignInWithCredentialAsync was canceled.");
  90.                 return;
  91.             }
  92.             if (task.IsFaulted)
  93.             {
  94.                 Debug.LogError("SignInWithCredentialAsync encountered an error: " + task.Exception);
  95.                 return;
  96.             }
  97.  
  98.             FirebaseUser newUser = task.Result;
  99.             userNametest = newUser.DisplayName;
  100.             userNameId = newUser.UserId;
  101.             Debug.LogFormat("User signed in successfully: {0} ({1})",
  102.                 newUser.DisplayName, newUser.UserId);
  103.             // Set the user ID.
  104.             FirebaseAnalytics.SetUserId(newUser.UserId);
  105.             isFirebaseLogged = true;
  106.         });
  107.  
  108.  
  109.         // Set default session duration values.
  110.         FirebaseAnalytics.SetMinimumSessionDuration(new TimeSpan(0, 0, 10));
  111.         FirebaseAnalytics.SetSessionTimeoutDuration(new TimeSpan(0, 30, 0));
  112.         firebaseInitialized = true;
  113.     }
  114.  
  115.     public void EventLevelFinished(int levelNumber)
  116.     {
  117.         string eventLog = "Level_finished_" + (levelNumber + 1).ToString();
  118.         switch (levelNumber)
  119.         {
  120.             case 2:
  121.                 {
  122.                     FirebaseAnalytics.LogEvent(eventLog);
  123.                     break;
  124.                 }
  125.             case 9:
  126.                 {
  127.                     FirebaseAnalytics.LogEvent(eventLog);
  128.                     break;
  129.                 }
  130.             case 19:
  131.                 {
  132.                     FirebaseAnalytics.LogEvent(eventLog);
  133.                     break;
  134.                 }
  135.             case 29:
  136.                 {
  137.                     FirebaseAnalytics.LogEvent(eventLog);
  138.                     break;
  139.                 }
  140.             case 39:
  141.                 {
  142.                     FirebaseAnalytics.LogEvent(eventLog);
  143.                     break;
  144.                 }
  145.             case 49:
  146.                 {
  147.                     FirebaseAnalytics.LogEvent(eventLog);
  148.                     break;
  149.                 }
  150.             case 59:
  151.                 {
  152.                     FirebaseAnalytics.LogEvent(eventLog);
  153.                     break;
  154.                 }
  155.         }
  156.     }
  157.  
  158.     public void EventLogCustomEvent(string log)
  159.     {
  160.         FirebaseAnalytics.LogEvent(log);
  161.     }
  162.  
  163.     public string GetFirebaseUser()
  164.     {
  165.         return user.DisplayName + " | " + user.UserId;
  166.     }
  167.  
  168.     public void SaveToCloud()
  169.     {
  170.         JsonToCloud.instance.BuildSaveString();
  171.         cloudSaveSystem.saveString = Prefs.CloudSaveString;
  172.         cloudSaveSystem.SaveToCloud();
  173.     }
  174.  
  175.     public void LoadFromCloud()
  176.     {
  177.         cloudSaveSystem.LoadFromCloud();
  178.         Prefs.CloudSaveString = cloudSaveSystem.saveString;
  179.         JsonToCloud.instance.StringToData();
  180.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
  181.     }
  182.  
  183.     public void ShowSavedGames()
  184.     {
  185.         cloudSaveSystem.showUI();
  186.     }
  187.  
  188.     public void AnalyticsLogin()
  189.     {
  190.         FirebaseAnalytics.LogEvent(FirebaseAnalytics.EventLogin);
  191.     }
  192.  
  193.     public void AnalyticsProgress()
  194.     {
  195.         FirebaseAnalytics.LogEvent("progress", "percent", 0.4f);
  196.     }
  197.  
  198.     void InitializeFirebase()
  199.     {
  200.         FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
  201.         // Set the user's sign up method.
  202.         FirebaseAnalytics.SetUserProperty(
  203.           FirebaseAnalytics.UserPropertySignUpMethod,
  204.           "PlayGames");
  205.         // Set the user ID.
  206.         FirebaseAnalytics.SetUserId("uber_user_510");
  207.         // Set default session duration values.
  208.         FirebaseAnalytics.SetMinimumSessionDuration(new TimeSpan(0, 0, 10));
  209.         FirebaseAnalytics.SetSessionTimeoutDuration(new TimeSpan(0, 30, 0));
  210.         firebaseInitialized = true;
  211.     }
  212.  
  213.     public void ShowAchievements()
  214.     {
  215.         if (PlayGamesPlatform.Instance.localUser.authenticated)
  216.         {
  217.             PlayGamesPlatform.Instance.ShowAchievementsUI();
  218.         }
  219.         else
  220.         {
  221.             Debug.Log("Cannot show Achievements, not logged in");
  222.         }
  223.     }
  224.  
  225.     public void SignIn()
  226.     {
  227.         if (!PlayGamesPlatform.Instance.localUser.authenticated)
  228.         {
  229.             // Sign in with Play Game Services, showing the consent dialog
  230.             // by setting the second parameter to isSilent=false.
  231.             PlayGamesPlatform.Instance.Authenticate(SignInCallback, false);
  232.         }
  233.         else
  234.         {
  235.             // Sign out of play games
  236.             PlayGamesPlatform.Instance.SignOut();
  237.         }
  238.         AnalyticsLogin();
  239.     }
  240.  
  241.     public void SignInCallback(bool success)
  242.     {
  243.         if (success)
  244.         {
  245.             isPlayGamesLogged = true;
  246.             Debug.Log("(Lollygagger) Signed in!");
  247.             authCode = PlayGamesPlatform.Instance.GetServerAuthCode();
  248.             ////////////////////////////////
  249.             FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
  250.             {
  251.                 dependencyStatus = task.Result;
  252.                 if (dependencyStatus == DependencyStatus.Available)
  253.                 {
  254.                     if (!isFirebaseLogged)
  255.                     {
  256.                         LoginFirebase();
  257.                     }
  258.                 }
  259.                 else
  260.                 {
  261.                     Debug.LogError(
  262.                       "Could not resolve all Firebase dependencies: " + dependencyStatus);
  263.                 }
  264.             });
  265.             /////////////////////////////////////////
  266.         }
  267.         else
  268.         {
  269.             Debug.Log("(Lollygagger) Sign-in failed...");
  270.         }
  271.     }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement