Skip to content

Commit 9101f21

Browse files
committed
Changes made as part of "Instrument Your Game with Remote Config"
1 parent 212463c commit 9101f21

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

Assets/Hamster/Scripts/MainGame.cs

+45-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System.Collections.Generic;
1919
using System.Threading.Tasks;
2020
using Firebase.Extensions;
21+
using Firebase.RemoteConfig;
2122

2223
namespace Hamster
2324
{
@@ -178,7 +179,7 @@ private void InitializeFirebaseAndStartGame()
178179
if (dependencyStatus == Firebase.DependencyStatus.Available) {
179180
// Create and hold a reference to your FirebaseApp,
180181
app = Firebase.FirebaseApp.DefaultInstance;
181-
InitializeCommonDataAndStartGame();
182+
SetRemoteConfigDefaults();
182183
} else {
183184
UnityEngine.Debug.LogError(
184185
$"Could not resolve all Firebase dependencies: {dependencyStatus}\n" +
@@ -191,21 +192,61 @@ private void InitializeFirebaseAndStartGame()
191192
// before starting the game.
192193
private void SetRemoteConfigDefaults()
193194
{
194-
throw new System.NotImplementedException();
195+
var defaults = new System.Collections.Generic.Dictionary < string, object > ();
196+
defaults.Add(
197+
Hamster.MapObjects.AccelerationTile.AccelerationTileForceKey,
198+
Hamster.MapObjects.AccelerationTile.AccelerationTileForceDefault);
199+
defaults.Add(
200+
Hamster.States.MainMenu.SubtitleOverrideKey,
201+
Hamster.States.MainMenu.SubtitleOverrideDefault);
202+
var remoteConfig = FirebaseRemoteConfig.DefaultInstance;
203+
remoteConfig.SetDefaultsAsync(defaults).ContinueWithOnMainThread(
204+
previousTask =>
205+
{
206+
FetchRemoteConfig(InitializeCommonDataAndStartGame);
207+
}
208+
);
195209
}
196210

197211
// (Re)fetches Remote Config values and pass down the onFetchAndActivateSuccessful callback.
198212
// Called during the initialization flow but can also be called indepedently.
199213
public void FetchRemoteConfig(System.Action onFetchAndActivateSuccessful)
200214
{
201-
throw new System.NotImplementedException();
215+
if(app==null)
216+
{
217+
Debug.LogError($"Do not use Firebase until it is properly initialized by calling {nameof(InitializeFirebaseAndStartGame)}.");
218+
return;
219+
}
220+
221+
Debug.Log("Fetching data...");
222+
var remoteConfig = FirebaseRemoteConfig.DefaultInstance;
223+
remoteConfig.FetchAsync(System.TimeSpan.Zero).ContinueWithOnMainThread(
224+
previousTask=>
225+
{
226+
if (!previousTask.IsCompleted)
227+
{
228+
Debug.LogError($"{nameof(remoteConfig.FetchAsync)} incomplete: Status '{previousTask.Status}'");
229+
return;
230+
}
231+
ActivateRetrievedRemoteConfigValues(onFetchAndActivateSuccessful);
232+
});
202233
}
203234

204235
// The final method in the initialization flow that will activate fetched values
205236
// and on Success will call onFetchAndActivateSuccessful.
206237
private void ActivateRetrievedRemoteConfigValues(System.Action onFetchAndActivateSuccessful)
207238
{
208-
throw new System.NotImplementedException();
239+
var remoteConfig = FirebaseRemoteConfig.DefaultInstance;
240+
var info = remoteConfig.Info;
241+
if(info.LastFetchStatus == LastFetchStatus.Success)
242+
{
243+
remoteConfig.ActivateAsync().ContinueWithOnMainThread(
244+
previousTask =>
245+
{
246+
Debug.Log($"Remote data loaded and ready (last fetch time {info.FetchTime}).");
247+
onFetchAndActivateSuccessful();
248+
});
249+
}
209250
}
210251
}
211252
}

Assets/Hamster/Scripts/MapObjects/AccelerationTile.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
using UnityEngine;
16+
using Firebase.RemoteConfig;
1617

1718
namespace Hamster.MapObjects {
1819

@@ -32,7 +33,8 @@ public class AccelerationTile : MapObject {
3233
public float Acceleration { get; private set; }
3334

3435
private void Start() {
35-
Acceleration = AccelerationTileForceDefault;
36+
var remoteConfig = FirebaseRemoteConfig.DefaultInstance;
37+
Acceleration = (float)remoteConfig.GetValue(AccelerationTileForceKey).DoubleValue;
3638
}
3739

3840
public void FixedUpdate() {

Assets/Hamster/Scripts/States/BaseLevelSelect.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ protected void SetButtonColors(int selection) {
117117
}
118118

119119
public override void Resume(StateExitValue results) {
120-
ShowUI();
120+
CommonData.mainGame.FetchRemoteConfig(ShowUI);
121121
}
122122

123123
public override void Suspend() {

Assets/Hamster/Scripts/States/MainMenu.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using UnityEngine;
1616
using System.Collections.Generic;
17+
using Firebase.RemoteConfig;
1718

1819
namespace Hamster.States {
1920
class MainMenu : BaseState {
@@ -53,15 +54,17 @@ public override void Initialize() {
5354

5455
public override void Resume(StateExitValue results) {
5556
CommonData.mainGame.SelectAndPlayMusic(CommonData.prefabs.menuMusic, true);
56-
InitializeUI();
57+
CommonData.mainGame.FetchRemoteConfig(InitializeUI);
5758
}
5859

5960
private void InitializeUI() {
6061
if (menuComponent == null) {
6162
menuComponent = SpawnUI<Menus.MainMenuGUI>(StringConstants.PrefabMainMenu);
6263
}
6364

64-
var subtitleOverride = JsonUtility.FromJson<Menus.MainMenuGUI.SubtitleOverride>(SubtitleOverrideDefault);
65+
var remoteConfig = FirebaseRemoteConfig.DefaultInstance;
66+
var subtitleOverride = JsonUtility.FromJson<Menus.MainMenuGUI.SubtitleOverride>(
67+
remoteConfig.GetValue(SubtitleOverrideKey).StringValue);
6568
// Only sets values if all fields of the override are non-default.
6669
if(subtitleOverride != null &&subtitleOverride.IsValidOverride())
6770
{

0 commit comments

Comments
 (0)