開始使用 Firebase 遠端設定


您可以使用 Firebase Remote Config 定義應用程式中的參數。 更新雲端值,讓您可以修改外觀和 未發布應用程式更新。

Remote Config 程式庫可用來儲存應用程式內的預設參數值。 從 Remote Config 後端擷取更新後的參數值 將擷取到的值提供給應用程式使用時。如要瞭解詳情 請參閱「遠端設定載入策略」。

本指南將引導您 協助您開始使用並提供一些程式碼範例 就能複製或下載 firebase/quickstart-unity GitHub 存放區。

步驟 1:將 Remote Config 新增至應用程式

使用前 Remote Config, 請完成下列操作:

  • 註冊 Unity 專案,並將其設定為使用 Firebase。

    • 如果您的 Unity 專案已在使用 Firebase, 已完成註冊和設定程序。

    • 如果您沒有 Unity 專案,可以 範例應用程式

  • Firebase Unity SDK (特別是 FirebaseRemoteConfig.unitypackage) 新增至 Unity 專案

,瞭解如何調查及移除這項存取權。

請注意,將 Firebase 新增至 Unity 專案時,必須一併執行以下兩者的工作: Firebase 控制台,然後在開啟的 Unity 專案中 (例如,您可以從控制台下載 Firebase 設定檔,然後 放入您的 Unity 專案中)。

步驟 2:設定應用程式內預設參數值

您可以在 Remote Config 中設定應用程式內預設參數值 物件,因此應用程式在連線至 Remote Config 後端,這樣一來,在沒有預設值的情況下,就無法使用預設值 您在後端中設定的連線

方法是建立字串字典,並填入鍵/值組合。 代表您要新增的預設值如已設定 Remote Config 後端參數值之後,您可以下載 ,並利用該組合來建立字串字典。 若需更多資訊,請參閲 下載 Remote Config範本預設值

(非字串屬性會是 則會在呼叫 SetDefaultsAsync() 時轉換為屬性類型)。

System.Collections.Generic.Dictionary<string, object> defaults =
  new System.Collections.Generic.Dictionary<string, object>();

// These are the values that are used if we haven't fetched data from the
// server
// yet, or if we ask for values that the server doesn't have:
defaults.Add("config_test_string", "default local string");
defaults.Add("config_test_int", 1);
defaults.Add("config_test_float", 1.0);
defaults.Add("config_test_bool", false);

Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.SetDefaultsAsync(defaults)
  .ContinueWithOnMainThread(task => {

步驟 3:取得要用於應用程式的參數值

您現在可以從 Remote Config 物件取得參數值。如果您為 Remote Config 後端中的值、擷取值,然後加以啟用 這些價值適用於您的應用程式。否則,您會收到應用程式內參數 值是使用 SetDefaultsAsync()

如要取得這些值,請使用 GetValue(), 提供參數鍵做為引數這會傳回 ConfigValue, 其屬性有很多屬性,可將值轉換為各種基礎型別。

步驟 4:設定參數值

  1. Firebase 控制台中開啟專案。
  2. 從選單中選取 Remote Config 以查看「Remote Config」資訊主頁。
  3. 使用與剛才定義的參數相同的名稱定義參數 您可以為每個參數設定預設值 (也就是 最終覆寫應用程式內預設值) 和條件式值。 詳情請參閱: Remote Config 參數和條件

步驟 5:擷取並啟用值 (視需要)

如要從 Remote Config 後端擷取參數值,請呼叫 FetchAsync() 方法。系統將擷取您在後端設定的任何值 快取在 Remote Config 物件中。

// Start a fetch request.
// FetchAsync only fetches new data if the current data is older than the provided
// timespan.  Otherwise it assumes the data is "recent enough", and does nothing.
// By default the timespan is 12 hours, and for production apps, this is a good
// number. For this example though, it's set to a timespan of zero, so that
// changes in the console will always show up immediately.
public Task FetchDataAsync() {
  DebugLog("Fetching data...");
  System.Threading.Tasks.Task fetchTask =
  Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.FetchAsync(
      TimeSpan.Zero);
  return fetchTask.ContinueWithOnMainThread(FetchComplete);
}

在上述程式碼中,FetchComplete 是一種方法,其簽章與 其中一個 超載 (共 ContinueWithOnMainThread() 個)。

在下方的程式碼範例中,FetchComplete 方法會傳遞上一個工作 (fetchTask),可讓 FetchComplete 判斷是否已完成。 程式碼使用 Info.LastFetchStatus ,判斷流程「也」是否達成目標。如果是, 接著,系統會使用 ActivateAsync() 啟用 Remote Config 參數值。

private void FetchComplete(Task fetchTask) {
  if (!fetchTask.IsCompleted) {
    Debug.LogError("Retrieval hasn't finished.");
    return;
  }

  var remoteConfig = FirebaseRemoteConfig.DefaultInstance;
  var info = remoteConfig.Info;
  if(info.LastFetchStatus != LastFetchStatus.Success) {
    Debug.LogError($"{nameof(FetchComplete)} was unsuccessful\n{nameof(info.LastFetchStatus)}: {info.LastFetchStatus}");
    return;
  }

  // Fetch successful. Parameter values must be activated to use.
  remoteConfig.ActivateAsync()
    .ContinueWithOnMainThread(
      task => {
        Debug.Log($"Remote data loaded and ready for use. Last fetch time {info.FetchTime}.");
    });
}

使用以下方式擷取的值: FetchAsync() 擷取完成後就會在本機快取,但要到 ActivateAsync() 事件。這樣就能確保系統不會套用新的值 或其他可能導致問題或異常情況的計算時間 行為

步驟 6:即時監聽更新

擷取參數值後,您可以使用即時 Remote Config 執行以下操作: 監聽來自 Remote Config 後端的更新。即時 有可用的更新時,Remote Config 會發出已連線裝置的訊號, 發布新的 Remote Config 後自動擷取變更 版本。

Firebase Unity SDK v11.0.0 以上版本支援即時更新,適用於 Android 和 Apple 平台。

  1. 在應用程式中新增 OnConfigUpdateListener,即可開始監聽更新 並自動擷取任何全新或更新的參數值接著 ConfigUpdateListenerEventHandler 來處理更新事件。下列 範例會監聽更新,並使用新擷取的值, 更新歡迎訊息
// Invoke the listener.
void Start()
{
  Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.OnConfigUpdateListener
    += ConfigUpdateListenerEventHandler;
}

// Handle real-time Remote Config events.
void ConfigUpdateListenerEventHandler(
   object sender, Firebase.RemoteConfig.ConfigUpdateEventArgs args) {
  if (args.Error != Firebase.RemoteConfig.RemoteConfigError.None) {
    Debug.Log(String.Format("Error occurred while listening: {0}", args.Error));
    return;
  }

  Debug.Log("Updated keys: " + string.Join(", ", args.UpdatedKeys));
  // Activate all fetched values and then display a welcome message.
  remoteConfig.ActivateAsync().ContinueWithOnMainThread(
    task => {
        DisplayWelcomeMessage();
    });
}

// Stop the listener.
void OnDestroy() {
    Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.OnConfigUpdateListener
      -= ConfigUpdateListenerEventHandler;
}

下次發布新版 Remote Config 時, 所執行的應用程式並監聽變更,將呼叫完成處理常式。

後續步驟

如果尚未體驗,請前往Remote Config 用途,以及 重要概念和進階策略說明文件,包括: