facebook twitter hatena line email

「Unity/Firebase/RemoteConfig」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(firebase側値取得)
(FetchComplete内でUI操作ができない問題)
行88: 行88:
 
}
 
}
 
</pre>
 
</pre>
 +
 +
参考:https://stackoverflow.com/questions/4331262/task-continuation-on-ui-thread
  
 
==firebase値キャッシュしない設定==
 
==firebase値キャッシュしない設定==

2019年5月29日 (水) 17:02時点における版

Firebase設定

unity/Firebase/基本 [ショートカット]

RemoteConfigインストール

FirebaseRemoteConfig.unitypackageをAssets/Importからインストールする

デフォルトで12時間アプリ内にキャッシュする

Helloworld

公式 https://firebase.google.com/docs/remote-config/use-config-unity?hl=ja

System.Collections.Generic.Dictionary defaults =
  new System.Collections.Generic.Dictionary();
defaults.Add("propertyname_string", "default local string");
defaults.Add("propertyname_int", 1);
defaults.Add("propertyname_float", 1.0);
defaults.Add("propertyname_bool", false);

Firebase.RemoteConfig.FirebaseRemoteConfig.SetDefaults(defaults);

error CS0305: Using the generic type 'Dictionary<TKey, TValue>' requires 2 type argumentsエラー

Dictionary defaults 部分で、error CS0305: Using the generic type 'Dictionary<TKey, TValue>' requires 2 type argumentsエラーがでる。

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

ローカルで値取得

Dictionary<string, object> defaults = new Dictionary<string, object>();
defaults.Add("propertyname_string", "default local string");
Debug.Log("hoge=" + Firebase.RemoteConfig.FirebaseRemoteConfig.GetValue("propertyname_string").StringValue); // default local string


firebase側値取得

firebase1=hogehoge をfirebaseのRemoteConfigに登録しておく

using System.Threading.Tasks;
using System;
void Start()
{
        Task fetchTask = Firebase.RemoteConfig.FirebaseRemoteConfig.FetchAsync(new TimeSpan(0)); // cache時間0秒としている。
        fetchTask.ContinueWith(Firebase1FetchComplete);
}
void Firebase1FetchComplete(Task fetchTask)
{
        switch (Firebase.RemoteConfig.FirebaseRemoteConfig.Info.LastFetchStatus)
        {
            case Firebase.RemoteConfig.LastFetchStatus.Success:
                Firebase.RemoteConfig.FirebaseRemoteConfig.ActivateFetched();
                Debug.Log("firebase1=" + Firebase.RemoteConfig.FirebaseRemoteConfig.GetValue("firebase1").StringValue); // hogehoge
                break;
            case Firebase.RemoteConfig.LastFetchStatus.Failure:
                switch (Firebase.RemoteConfig.FirebaseRemoteConfig.Info.LastFetchFailureReason)
                {
                    case Firebase.RemoteConfig.FetchFailureReason.Error:
                        Debug.Log("Fetch failed for unknown reason");
                        break;
                    case Firebase.RemoteConfig.FetchFailureReason.Throttled:
                        Debug.Log("Fetch throttled until " +
                        Firebase.RemoteConfig.FirebaseRemoteConfig.Info.ThrottledEndTime);
                        break;
                }
                break;
            case Firebase.RemoteConfig.LastFetchStatus.Pending:
                Debug.Log("Latest Fetch call still pending.");
                break;
        }
}

参考: https://qiita.com/wapa5pow/items/04edd95d312ee27f1da8

FetchComplete内でUI操作ができない問題

TaskScheduler.FromCurrentSynchronizationContext()を第二引数に追加するとFetchCompleteがUIスレッドとなる。

Task fetchTask = Firebase.RemoteConfig.FirebaseRemoteConfig.FetchAsync(new TimeSpan(3600));
fetchTask.ContinueWith(FirebaseFetchComplete, TaskScheduler.FromCurrentSynchronizationContext());

void FirebaseFetchComplete(Task fetchTask)
{
        GameObject.Find("AdUnitIdText").GetComponent<Text>().text = "hogehoge";
}

参考:https://stackoverflow.com/questions/4331262/task-continuation-on-ui-thread

firebase値キャッシュしない設定

var settings = Firebase.RemoteConfig.FirebaseRemoteConfig.Settings;
settings.IsDeveloperMode = true;
Firebase.RemoteConfig.FirebaseRemoteConfig.Settings = settings;

keyリスト取得

IEnumerable<string> keys = Firebase.RemoteConfig.FirebaseRemoteConfig.GetKeysByPrefix("propertyname");
foreach (string key in keys) {
    Debug.Log("key=" + key);
}

firebase公式RemoteConfigメソッド

https://firebase.google.com/docs/reference/unity/struct/firebase/remote-config/config-value.html?hl=ja#struct_firebase_1_1_remote_config_1_1_config_value