Skip to content

Commit e366f6f

Browse files
committed
- add logic
1 parent aee36b2 commit e366f6f

7 files changed

+400
-0
lines changed

Runtime.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/FirebaseRemoteConfigData.cs

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using System;
2+
3+
#if VIRTUESKY_FIREBASE_REMOTECONFIG
4+
using Firebase.RemoteConfig;
5+
#endif
6+
7+
using UnityEngine;
8+
using VirtueSky.Inspector;
9+
10+
11+
namespace VirtueSky.RemoteConfigs
12+
{
13+
[Serializable]
14+
public class FirebaseRemoteConfigData
15+
{
16+
public string key;
17+
public TypeRemoteConfigData typeRemoteConfigData;
18+
19+
[ShowIf(nameof(typeRemoteConfigData), TypeRemoteConfigData.StringData)]
20+
public string defaultValueString;
21+
22+
[ShowIf(nameof(typeRemoteConfigData), TypeRemoteConfigData.StringData)] [SerializeField, ReadOnly]
23+
private string resultValueString;
24+
25+
[ShowIf(nameof(typeRemoteConfigData), TypeRemoteConfigData.BooleanData)]
26+
public bool defaultValueBool;
27+
28+
[ShowIf(nameof(typeRemoteConfigData), TypeRemoteConfigData.BooleanData)] [SerializeField, ReadOnly]
29+
private bool resultValueBool;
30+
31+
[ShowIf(nameof(typeRemoteConfigData), TypeRemoteConfigData.IntData)]
32+
public int defaultValueInt;
33+
34+
[ShowIf(nameof(typeRemoteConfigData), TypeRemoteConfigData.IntData)] [SerializeField, ReadOnly]
35+
private int resultValueInt;
36+
37+
38+
public void SetupDataDefault()
39+
{
40+
switch (typeRemoteConfigData)
41+
{
42+
case TypeRemoteConfigData.StringData:
43+
#if VIRTUESKY_DATA
44+
VirtueSky.DataStorage.GameData.Set(key, defaultValueString);
45+
#else
46+
PlayerPrefs.SetString(key, defaultValueString);
47+
#endif
48+
break;
49+
case TypeRemoteConfigData.BooleanData:
50+
#if VIRTUESKY_DATA
51+
VirtueSky.DataStorage.GameData.Set(key, defaultValueBool);
52+
#else
53+
SetBool(key, defaultValueBool);
54+
#endif
55+
break;
56+
57+
case TypeRemoteConfigData.IntData:
58+
#if VIRTUESKY_DATA
59+
VirtueSky.DataStorage.GameData.Set(key, defaultValueInt);
60+
#else
61+
PlayerPrefs.SetInt(key, defaultValueInt);
62+
#endif
63+
break;
64+
}
65+
}
66+
#if VIRTUESKY_FIREBASE_REMOTECONFIG
67+
public void SetupData(ConfigValue result)
68+
{
69+
switch (typeRemoteConfigData)
70+
{
71+
case TypeRemoteConfigData.StringData:
72+
if (result.Source == ValueSource.RemoteValue)
73+
{
74+
#if VIRTUESKY_DATA
75+
VirtueSky.DataStorage.GameData.Set(key, result.StringValue);
76+
#else
77+
PlayerPrefs.SetString(key, result.StringValue);
78+
#endif
79+
}
80+
#if VIRTUESKY_DATA
81+
resultValueString = VirtueSky.DataStorage.GameData.Get<string>(key);
82+
#else
83+
resultValueString = PlayerPrefs.GetString(key);
84+
#endif
85+
Debug.Log($"<color=Green>{key}: {resultValueString}</color>");
86+
break;
87+
case TypeRemoteConfigData.BooleanData:
88+
if (result.Source == ValueSource.RemoteValue)
89+
{
90+
#if VIRTUESKY_DATA
91+
VirtueSky.DataStorage.GameData.Set(key, result.BooleanValue);
92+
#else
93+
SetBool(key, result.BooleanValue);
94+
#endif
95+
}
96+
#if VIRTUESKY_DATA
97+
resultValueBool = VirtueSky.DataStorage.GameData.Get<bool>(key);
98+
#else
99+
resultValueBool = GetBool(key);
100+
#endif
101+
Debug.Log($"<color=Green>{key}: {resultValueBool}</color>");
102+
break;
103+
case TypeRemoteConfigData.IntData:
104+
if (result.Source == ValueSource.RemoteValue)
105+
{
106+
#if VIRTUESKY_DATA
107+
VirtueSky.DataStorage.GameData.Set(key, int.Parse(result.StringValue));
108+
#else
109+
PlayerPrefs.SetInt(key, int.Parse(result.StringValue));
110+
#endif
111+
}
112+
#if VIRTUESKY_DATA
113+
resultValueInt = VirtueSky.DataStorage.GameData.Get<int>(key);
114+
#else
115+
resultValueInt = PlayerPrefs.GetInt(key);
116+
#endif
117+
Debug.Log($"<color=Green>{key}: {resultValueInt}</color>");
118+
break;
119+
}
120+
}
121+
#endif
122+
123+
private bool GetBool(string key, bool defaultValue = false) =>
124+
PlayerPrefs.GetInt(key, defaultValue ? 1 : 0) > 0;
125+
126+
private void SetBool(string key, bool value) => PlayerPrefs.SetInt(key, value ? 1 : 0);
127+
}
128+
129+
public enum TypeRemoteConfigData
130+
{
131+
StringData,
132+
BooleanData,
133+
IntData
134+
}
135+
}

Runtime/FirebaseRemoteConfigData.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Threading.Tasks;
5+
using UnityEditor;
6+
using UnityEngine;
7+
using VirtueSky.Inspector;
8+
9+
10+
#if VIRTUESKY_FIREBASE
11+
using Firebase;
12+
using Firebase.Extensions;
13+
#endif
14+
15+
#if VIRTUESKY_FIREBASE_REMOTECONFIG
16+
using Firebase.RemoteConfig;
17+
#endif
18+
19+
namespace VirtueSky.RemoteConfigs
20+
{
21+
public class FirebaseRemoteConfigManager : MonoBehaviour
22+
{
23+
[SerializeField] private bool dontDestroyOnLoad;
24+
#if VIRTUESKY_FIREBASE
25+
[Space, ReadOnly, SerializeField] private DependencyStatus dependencyStatus = DependencyStatus.UnavailableOther;
26+
#endif
27+
[SerializeField] private bool isSetupDefaultData = true;
28+
[Space(10), SerializeField] private List<FirebaseRemoteConfigData> listRemoteConfigData;
29+
30+
private bool _isFetchRemoteConfigCompleted = false;
31+
private static FirebaseRemoteConfigManager _instance;
32+
33+
private void Awake()
34+
{
35+
if (dontDestroyOnLoad)
36+
{
37+
DontDestroyOnLoad(this.gameObject);
38+
}
39+
40+
if (_instance == null)
41+
{
42+
_instance = this;
43+
}
44+
else
45+
{
46+
Destroy(gameObject);
47+
}
48+
}
49+
50+
#region API
51+
52+
public static bool IsFetchRemoteConfigCompleted => _instance._isFetchRemoteConfigCompleted;
53+
public static List<FirebaseRemoteConfigData> ListRemoteConfigData => _instance.listRemoteConfigData;
54+
55+
#endregion
56+
57+
#if VIRTUESKY_FIREBASE
58+
private void Start()
59+
{
60+
_isFetchRemoteConfigCompleted = false;
61+
if (isSetupDefaultData)
62+
{
63+
foreach (var remoteConfigData in listRemoteConfigData)
64+
{
65+
remoteConfigData.SetupDataDefault();
66+
}
67+
}
68+
69+
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task =>
70+
{
71+
dependencyStatus = task.Result;
72+
if (dependencyStatus == DependencyStatus.Available)
73+
{
74+
#if VIRTUESKY_FIREBASE_REMOTECONFIG
75+
FetchDataAsync();
76+
#endif
77+
}
78+
else
79+
{
80+
Debug.LogError("Could not resolve all Firebase dependencies: " +
81+
dependencyStatus);
82+
}
83+
});
84+
}
85+
#endif
86+
87+
#if VIRTUESKY_FIREBASE_REMOTECONFIG && VIRTUESKY_FIREBASE
88+
private Task FetchDataAsync()
89+
{
90+
Debug.Log("Fetching data...");
91+
Task fetchTask = FirebaseRemoteConfig.DefaultInstance
92+
.FetchAsync(TimeSpan.Zero);
93+
if (fetchTask.IsCanceled)
94+
{
95+
Debug.Log("Fetch canceled.");
96+
}
97+
else if (fetchTask.IsFaulted)
98+
{
99+
Debug.Log("Fetch encountered an error.");
100+
}
101+
else if (fetchTask.IsCompleted)
102+
{
103+
Debug.Log("Fetch completed successfully!");
104+
}
105+
106+
return fetchTask.ContinueWithOnMainThread(tast =>
107+
{
108+
var info = FirebaseRemoteConfig.DefaultInstance.Info;
109+
if (info.LastFetchStatus == LastFetchStatus.Success)
110+
{
111+
FirebaseRemoteConfig.DefaultInstance.ActivateAsync().ContinueWithOnMainThread(
112+
task =>
113+
{
114+
Debug.Log(String.Format(
115+
"Remote data loaded and ready (last fetch time {0}).",
116+
info.FetchTime));
117+
foreach (var remoteConfigData in listRemoteConfigData)
118+
{
119+
if (string.IsNullOrEmpty(remoteConfigData.key)) continue;
120+
remoteConfigData.SetupData(FirebaseRemoteConfig.DefaultInstance
121+
.GetValue(remoteConfigData.key.ToString()));
122+
}
123+
124+
_isFetchRemoteConfigCompleted = true;
125+
});
126+
127+
Debug.Log("<color=Green>Firebase Remote Config Fetching completed!</color>");
128+
}
129+
else
130+
{
131+
Debug.Log("Fetching data did not completed!");
132+
}
133+
});
134+
}
135+
#endif
136+
137+
#if UNITY_EDITOR
138+
private const string pathDefaultScript = "Assets/_Root/Scripts";
139+
[Button]
140+
private void GenerateRemoteData()
141+
{
142+
if (!Directory.Exists(pathDefaultScript))
143+
{
144+
Directory.CreateDirectory(pathDefaultScript);
145+
}
146+
147+
var productImplPath = $"{pathDefaultScript}/RemoteData.cs";
148+
var str = "namespace VirtueSky.RemoteConfigs\n{";
149+
str += "\n\tpublic struct RemoteData\n\t{";
150+
151+
var listRmcData = listRemoteConfigData;
152+
for (int i = 0; i < listRmcData.Count; i++)
153+
{
154+
var rmcKey = listRmcData[i].key;
155+
156+
str += $"\n\t\tpublic const string KEY_{rmcKey.ToUpper()} = \"{rmcKey}\";";
157+
158+
switch (listRmcData[i].typeRemoteConfigData)
159+
{
160+
case TypeRemoteConfigData.StringData:
161+
str +=
162+
$"\n\t\tpublic const string DEFAULT_{rmcKey.ToUpper()} = \"{listRmcData[i].defaultValueString}\";";
163+
str += "\n\t\t#if VIRTUESKY_DATA";
164+
str +=
165+
$"\n\t\tpublic static string {rmcKey.ToUpper()} => VirtueSky.DataStorage.GameData.Get(KEY_{rmcKey.ToUpper()}, DEFAULT_{rmcKey.ToUpper()});";
166+
str += "\n\t\t#else";
167+
str +=
168+
$"\n\t\tpublic static string {rmcKey.ToUpper()} => UnityEngine.PlayerPrefs.GetString(KEY_{rmcKey.ToUpper()}, DEFAULT_{rmcKey.ToUpper()});";
169+
str += "\n\t\t#endif";
170+
break;
171+
case TypeRemoteConfigData.BooleanData:
172+
str +=
173+
$"\n\t\tpublic const bool DEFAULT_{rmcKey.ToUpper()} = {GetBool(listRmcData[i].defaultValueBool)};";
174+
str += "\n\t\t#if VIRTUESKY_DATA";
175+
str +=
176+
$"\n\t\tpublic static bool {rmcKey.ToUpper()} => VirtueSky.DataStorage.GameData.Get(KEY_{rmcKey.ToUpper()}, DEFAULT_{rmcKey.ToUpper()});";
177+
str += "\n\t\t#else";
178+
str +=
179+
$"\n\t\tpublic static bool {rmcKey.ToUpper()} => UnityEngine.PlayerPrefs.GetInt(KEY_{rmcKey.ToUpper()}, DEFAULT_{rmcKey.ToUpper()} ? 1 : 0) > 0;";
180+
str += "\n\t\t#endif";
181+
break;
182+
case TypeRemoteConfigData.IntData:
183+
str +=
184+
$"\n\t\tpublic const int DEFAULT_{rmcKey.ToUpper()} = {listRmcData[i].defaultValueInt};";
185+
str += "\n\t\t#if VIRTUESKY_DATA";
186+
str +=
187+
$"\n\t\tpublic static int {rmcKey.ToUpper()} => VirtueSky.DataStorage.GameData.Get(KEY_{rmcKey.ToUpper()}, DEFAULT_{rmcKey.ToUpper()});";
188+
str += "\n\t\t#else";
189+
str +=
190+
$"\n\t\tpublic static int {rmcKey.ToUpper()} => UnityEngine.PlayerPrefs.GetInt(KEY_{rmcKey.ToUpper()}, DEFAULT_{rmcKey.ToUpper()});";
191+
str += "\n\t\t#endif";
192+
break;
193+
}
194+
}
195+
196+
str += "\n\t}";
197+
str += "\n}";
198+
199+
var writer = new StreamWriter(productImplPath, false);
200+
writer.Write(str);
201+
writer.Close();
202+
AssetDatabase.ImportAsset(productImplPath);
203+
204+
string GetBool(bool condition)
205+
{
206+
return condition ? "true" : "false";
207+
}
208+
}
209+
#endif
210+
}
211+
}

Runtime/FirebaseRemoteConfigManager.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)