Advertisement
Guest User

Untitled

a guest
Aug 30th, 2019
1,345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. using UnityEngine;
  2. using Firebase;
  3. using Firebase.Analytics;
  4. using System.Threading.Tasks;
  5. using Firebase.Extensions;
  6. public class FirebaseManager : MonoBehaviour
  7. {
  8. protected bool firebaseInitialized = false;
  9. void Awake()
  10. {
  11. FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
  12. var dependencyStatus = task.Result;
  13. if (dependencyStatus == DependencyStatus.Available)
  14. {
  15. InitializeFirebase();
  16. }
  17. else
  18. {
  19. Debug.LogError(string.Format(
  20. "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
  21. // Firebase Unity SDK is not safe to use here.
  22. }
  23. });
  24. }
  25. // Handle initialization of the necessary firebase modules:
  26. void InitializeFirebase()
  27. {
  28. Debug.Log("Enabling data collection.");
  29. FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
  30.  
  31. Debug.Log("Set user properties.");
  32. // Set the user's sign up method.
  33. FirebaseAnalytics.SetUserProperty(FirebaseAnalytics.UserPropertySignUpMethod, "Google");
  34. //// Set the user ID.
  35. //FirebaseAnalytics.SetUserId("uber_user_510");
  36. //// Set default session duration values.
  37. //FirebaseAnalytics.SetMinimumSessionDuration(new TimeSpan(0, 0, 10));
  38. //FirebaseAnalytics.SetSessionTimeoutDuration(new TimeSpan(0, 30, 0));
  39. firebaseInitialized = true;
  40. }
  41. // Get the current app instance ID.
  42. public Task<string> DisplayAnalyticsInstanceId()
  43. {
  44. return FirebaseAnalytics.GetAnalyticsInstanceIdAsync().ContinueWithOnMainThread(task => {
  45. if (task.IsCanceled)
  46. {
  47. Debug.Log("App instance ID fetch was canceled.");
  48. }
  49. else if (task.IsFaulted)
  50. {
  51. Debug.Log(string.Format("Encounted an error fetching app instance ID {0}",
  52. task.Exception.ToString()));
  53. }
  54. else if (task.IsCompleted)
  55. {
  56. Debug.Log(string.Format("App instance ID: {0}", task.Result));
  57. }
  58. return task;
  59. }).Unwrap();
  60. }
  61. public static void TutorialPhaseEvent(bool began) //Вызываем из Grathic Component, когда показываем начальный тутор и когда закрываем его
  62. {
  63. if(began)
  64. FirebaseAnalytics.LogEvent(FirebaseAnalytics.EventTutorialBegin);
  65. else
  66. FirebaseAnalytics.LogEvent(FirebaseAnalytics.EventTutorialComplete);
  67. }
  68.  
  69. public static void CompletedLevelEvent(int levelNumber)
  70. {
  71. FirebaseAnalytics.LogEvent(FirebaseAnalytics.EventLevelUp, new Parameter(FirebaseAnalytics.ParameterLevel, levelNumber));
  72. }
  73.  
  74. public static void EarnedCurrencyEvent()
  75. {
  76. FirebaseAnalytics.LogEvent(FirebaseAnalytics.EventEarnVirtualCurrency, new Parameter[]
  77. { new Parameter(FirebaseAnalytics.ParameterValue, 1), new Parameter(FirebaseAnalytics.ParameterVirtualCurrencyName, "Coin") });
  78. }
  79.  
  80. public static void UsedFrostEvent(int frostCharges) //MY OWN EVENT
  81. {
  82. FirebaseAnalytics.LogEvent("Player pressed the Frost button", new Parameter("Frost charges left", frostCharges));
  83. }
  84.  
  85. public static void SpentCurrencyEvent(string itemName, int spentAmount)
  86. {
  87. FirebaseAnalytics.LogEvent(FirebaseAnalytics.EventSpendVirtualCurrency, new Parameter[]
  88. { new Parameter(FirebaseAnalytics.ParameterItemName, itemName), new Parameter(FirebaseAnalytics.ParameterValue, spentAmount)});
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement