Advertisement
Void_Ptr_YT

Untitled

Sep 27th, 2023
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.66 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.  
  7. public class MyFireBaseRemoteConfig : MonoBehaviour {
  8.     Firebase.DependencyStatus dependencyStatus = Firebase.DependencyStatus.UnavailableOther;
  9.     // Use this for initialization
  10.     void Start() {
  11.         Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
  12.             dependencyStatus = task.Result;
  13.             if (dependencyStatus == Firebase.DependencyStatus.Available) {
  14.                 InitializeFirebase();
  15.             } else {
  16.                 Debug.LogError(
  17.                     "Could not resolve all Firebase dependencies: " + dependencyStatus);
  18.             }
  19.         });
  20.     }
  21.  
  22.     void InitializeFirebase() {
  23.         System.Collections.Generic.Dictionary<string, object> defaults =
  24.             new System.Collections.Generic.Dictionary<string, object>();
  25.  
  26.         // These are the values that are used if we haven't fetched data from the
  27.         // server
  28.         // yet, or if we ask for values that the server doesn't have:
  29.         defaults.Add("config_test_string", "default local string");
  30.         defaults.Add("config_test_int", 1);
  31.         defaults.Add("config_test_float", 1.0);
  32.         defaults.Add("config_test_bool", false);
  33.  
  34.         Firebase.RemoteConfig.FirebaseRemoteConfig.SetDefaults(defaults);
  35.         Debug.Log("Remote config ready!");
  36.     }
  37.     public void FetchFireBase() {
  38.         FetchDataAsync();
  39.     }
  40.     public void ShowData() {
  41.         //   DebugLog("config_test_string: " +
  42.         //        Firebase.RemoteConfig.FirebaseRemoteConfig.GetValue("config_test_string").StringValue);
  43.         Debug.Log("maxCountToShowAdmob: " +
  44.             Firebase.RemoteConfig.FirebaseRemoteConfig.GetValue("maxCountToShowAdmob").LongValue);
  45.         //   DebugLog("config_test_float: " +
  46.         //            Firebase.RemoteConfig.FirebaseRemoteConfig.GetValue("config_test_float").DoubleValue);
  47.         //   DebugLog("config_test_bool: " +
  48.         //            Firebase.RemoteConfig.FirebaseRemoteConfig.GetValue("config_test_bool").BooleanValue);
  49.     }
  50.  
  51.     // Start a fetch request.
  52.     public Task FetchDataAsync() {
  53.         Debug.Log("Fetching data...");
  54.         // FetchAsync only fetches new data if the current data is older than the provided
  55.         // timespan.  Otherwise it assumes the data is "recent enough", and does nothing.
  56.         // By default the timespan is 12 hours, and for production apps, this is a good
  57.         // number.  For this example though, it's set to a timespan of zero, so that
  58.         // changes in the console will always show up immediately.
  59.         System.Threading.Tasks.Task fetchTask = Firebase.RemoteConfig.FirebaseRemoteConfig.FetchAsync(
  60.             TimeSpan.Zero);
  61.         return fetchTask.ContinueWith(FetchComplete);
  62.     }
  63.  
  64.     void FetchComplete(Task fetchTask) {
  65.         if (fetchTask.IsCanceled) {
  66.             Debug.Log("Fetch canceled.");
  67.         } else if (fetchTask.IsFaulted) {
  68.             Debug.Log("Fetch encountered an error.");
  69.         } else if (fetchTask.IsCompleted) {
  70.             Debug.Log("Fetch completed successfully!");
  71.         }
  72.  
  73.         var info = Firebase.RemoteConfig.FirebaseRemoteConfig.Info;
  74.         switch (info.LastFetchStatus) {
  75.             case Firebase.RemoteConfig.LastFetchStatus.Success:
  76.                 Firebase.RemoteConfig.FirebaseRemoteConfig.ActivateFetched();
  77.                 Debug.Log(String.Format("Remote data loaded and ready (last fetch time {0}).",
  78.                     info.FetchTime));
  79.                 break;
  80.             case Firebase.RemoteConfig.LastFetchStatus.Failure:
  81.                 switch (info.LastFetchFailureReason) {
  82.                     case Firebase.RemoteConfig.FetchFailureReason.Error:
  83.                         Debug.Log("Fetch failed for unknown reason");
  84.                         break;
  85.                     case Firebase.RemoteConfig.FetchFailureReason.Throttled:
  86.                         Debug.Log("Fetch throttled until " + info.ThrottledEndTime);
  87.                         break;
  88.                 }
  89.                 break;
  90.             case Firebase.RemoteConfig.LastFetchStatus.Pending:
  91.                 Debug.Log("Latest Fetch call still pending.");
  92.                 break;
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement