您可以使用 Firebase Remote Config 在您的應用程序中定義參數並在雲端更新它們的值,從而允許您修改應用程序的外觀和行為而無需分發應用程序更新。
Remote Config 庫用於存儲應用內默認參數值、從 Remote Config 後端獲取更新的參數值,以及控制獲取的值何時可用於您的應用。要了解更多信息,請參閱遠程配置加載策略。
本指南將引導您完成入門步驟並提供一些示例代碼,所有這些代碼都可以從firebase/quickstart-unity GitHub 存儲庫中克隆或下載。
第 1 步:將遠程配置添加到您的應用
在使用Remote Config之前,您需要:
註冊您的 Unity 項目並將其配置為使用 Firebase。
如果您的 Unity 項目已經使用 Firebase,那麼它已經針對 Firebase 進行了註冊和配置。
如果您沒有 Unity 項目,可以下載示例應用程序。
將Firebase Unity SDK (特別是
FirebaseRemoteConfig.unitypackage
)添加到您的 Unity 項目。
請注意,將 Firebase 添加到您的 Unity 項目涉及Firebase 控制台和您打開的 Unity 項目中的任務(例如,您從控制台下載 Firebase 配置文件,然後將它們移動到您的 Unity 項目中)。
第 2 步:設置應用內默認參數值
您可以在 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 步:在 Firebase 控制台中連接您的應用
在Firebase 控制台中,將您的應用程序添加到您的 Firebase 項目。
第 5 步:設置參數值
- 在Firebase 控制台中,打開您的項目。
- 從菜單中選擇遠程配置以查看遠程配置儀表板。
- 使用與您在應用程序中定義的參數相同的名稱定義參數。對於每個參數,您可以設置默認值(最終將覆蓋應用內默認值)和條件值。要了解更多信息,請參閱遠程配置參數和條件。
第 6 步:獲取並激活值(根據需要)
要從遠程配置後端獲取參數值,請調用 [ FetchAsync()
][fetch] 方法。您在後端設置的任何值都將被提取並緩存在遠程配置對像中。
// 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()
激活遠程配置參數值。
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()
][fetch] 獲取的值在獲取完成時緩存在本地,但在調用ActivateAsync()
之前不可用。這使您能夠確保新值不會在計算中應用,或者在其他可能導致問題或奇怪行為的時候應用。
第 7 步:實時收聽更新
獲取參數值後,您可以使用實時 Remote Config 來監聽來自 Remote Config 後端的更新。實時遠程配置在更新可用時向連接的設備發出信號,並在您發布新的遠程配置版本後自動獲取更改。
Android 和 Apple 平台的 Firebase Unity SDK v11.0.0+ 及更高版本支持實時更新。
- 在您的應用程序中,添加一個
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; }
下次您發布新版本的遠程配置時,運行您的應用程序並偵聽更改的設備將調用完成處理程序。
下一步
If you haven't already, explore the Remote Config use cases , and take a look at some of the key concepts and advanced strategies documentation, including: