I want to get enter image description here this parameter. I'm trying to do this with this code. But it's working only on pc.
Here all my code but I need only get parameters from Remote Config and initialize firebase analytics.
I can't figure out where is problem because there is few guides about it for unity. I am trying do this already 3 days please help.
using UnityEngine;
using Firebase.RemoteConfig;
using System;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using System.Threading.Tasks;
using Firebase.Crashlytics;
using Firebase.Analytics;
using Photon.Pun;
using TMPro;
using UnityEngine.UI;
public class GameOpening : MonoBehaviour
{
[SerializeField] private Text _versionText;
[SerializeField] private RemoteConfigSavings _remoteConfigSavings;
private Firebase.DependencyStatus dependencyStatus = Firebase.DependencyStatus.UnavailableOther;
private int _version = -1;
private int _receivedVersion
{
get => _version;
set
{
_version = value;
UpdateConfig();
}
}
private void Start()
{
AwakeFireBase();
Invoke("ToNextScene", 1);
}
private void ToNextScene()
{
if(_version!=-1) PhotonNetwork.LoadLevel(1);
else Invoke("ToNextScene", 1);
}
private void AwakeFireBase()
{
if(Debug.isDebugBuild)
{
var configSettings = new ConfigSettings();
configSettings.MinimumFetchInternalInMilliseconds = 0;
FirebaseRemoteConfig.DefaultInstance.SetConfigSettingsAsync(configSettings);
}
Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
{
FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
dependencyStatus = task.Result;
if (dependencyStatus == Firebase.DependencyStatus.Available)
{
//InitializeFirebase();
GetGameVersion();
}
else
{
Debug.LogError("Could not resolve all Firebase dependencies: " dependencyStatus);
}
});
}
private void InitializeFirebase()
{
var defaults = new System.Collections.Generic.Dictionary<string, object>
{
{"someThing", "asdf"},
{"some2", 12323}
};
FirebaseRemoteConfig.DefaultInstance.SetDefaultsAsync(defaults).ContinueWith(task=> { GetGameVersion(); });
Debug.Log("Remote config ready!");
}
private void GetGameVersion()
{
var remoteConfig = FirebaseRemoteConfig.DefaultInstance;
remoteConfig.FetchAndActivateAsync().ContinueWith(task =>
{
IDictionary<string, ConfigValue> values = remoteConfig.AllValues;
values.TryGetValue("VERSION", out ConfigValue objValue);
int version = Convert.ToInt32(objValue.StringValue);
_receivedVersion = version;
_versionText.text = version.ToString();
});
}
private void UpdateConfig()
{
_remoteConfigSavings.Version = _version;
_remoteConfigSavings.SaveObject();
}
}
CodePudding user response:
Before we start, we need to clean up our code.
I deleted everything except the part related to FirebaseRemoteConfig.
Also, I removed the CheckAndFixDependenciesAsync()
and SetDefaultsAsync()
parts.
After the code cleanup presented above, all that is left is the GetGameVersion()
method. Now, let's find the problem.
Problem
You should fully understand the description of FetchAndActivateAsync()
in the FirebaseRemoteConfig API page.
FetchAndActivateAsync()
If the time elapsed since the last fetch from the Firebase Remote Config backend is more than the default minimum fetch interval, configs are fetched from the backend.
Although not listed on this page, By default the minimum fetch interval is 12 hours.
We change that part to FetchAsync(TimeSpan.Zero)
because we want to check whether the value is updated or not in real time while developing.
using UnityEngine;
using System;
using Firebase.Extensions;
public class GameOpening : MonoBehaviour
{
private void Start()
{
GetGameVersion();
}
private static void GetGameVersion()
{
var remoteConfig = Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance;
var fetchTask = remoteConfig.FetchAsync(TimeSpan.Zero)
.ContinueWithOnMainThread(async task =>
{
await Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.ActivateAsync();
var values = remoteConfig.AllValues;
values.TryGetValue("VERSION", out var configValue);
Debug.Log(configValue.StringValue);
});
}
}
See why you used ActivateAsync()
. There is a reason why we used async await.
Note: This does not actually apply the data or make it accessible, it merely retrieves it and caches it. To accept and access the newly retrieved values, you must call
ActivateAsync()
. Note that this function is asynchronous, and will normally take an unspecified amount of time before completion.
All issues have been resolved. I also checked it on Android and it works fine. (Based on Samsung Galaxy 10 5G) (using LogCat)
Hope your problem is solved :)