Skip to content

Instantly share code, notes, and snippets.

@yasuyuki-kamata
Created May 10, 2020 18:05
Show Gist options
  • Save yasuyuki-kamata/6acacf3d22b6cdea6cda4b41a78267b2 to your computer and use it in GitHub Desktop.
Save yasuyuki-kamata/6acacf3d22b6cdea6cda4b41a78267b2 to your computer and use it in GitHub Desktop.
Unity Remote Config を使って値を取得・適用するサンプルコード
using UnityEngine;
using Unity.RemoteConfig;
public class RemoteConfigExample : MonoBehaviour {
/// <summary>
/// Userアトリビュート用の構造体
/// </summary>
public struct userAttributes {
// カスタムUserアトリビュート用の変数を宣言する
public bool expansionFlag;
}
/// <summary>
/// Appアトリビュート用の構造体
/// </summary>
public struct appAttributes {
// カスタムAppアトリビュート用の変数を宣言する
public int level;
public int score;
public string appVersion;
}
// トラッキング用にユニークなassignment IDを宣言する
public string assignmentId;
// Remote Configを使って設定する値の変数を宣言する
public int enemyVolume;
public float enemyHealth;
public float enemyDamage;
// ConfigManager.FetchConfigsはアトリビュートの構造体
// (空の構造体またはカスタムアトリビュート)を引数にいれて
// 呼び出す必要があります。
// Awake時にサービスから最新の
// Key-Valueペアを取得して適用する
void Awake () {
// 値の取得が成功したときに設定を適用するためのリスナーを登録
ConfigManager.FetchCompleted += ApplyRemoteSettings;
// ユニークなカスタムユーザーIDを設定する
ConfigManager.SetCustomUserID("ここにカスタムユーザーID");
// EnvironmentID(環境名)を設定する
ConfigManager.SetEnvironmentID("ここにEnvironment ID");
// リモートのサービスから設定値を取得する
ConfigManager.FetchConfigs<userAttributes, appAttributes>(
new userAttributes(),
new appAttributes()
);
}
/// <summary>
/// 取得した設定を適用する
/// </summary>
/// <param name="configResponse"></param>
void ApplyRemoteSettings (ConfigResponse configResponse) {
// レスポンス元に応じて設定を更新する
switch (configResponse.requestOrigin) {
case ConfigOrigin.Default:
Debug.Log (
"設定を読み込めなかったためデフォルトの値を使用");
break;
case ConfigOrigin.Cached:
Debug.Log (
"設定を読み込めなかったため前回キャッシュされた値を使用");
break;
case ConfigOrigin.Remote:
Debug.Log (
"新しい設定を読み込んだのでそれぞれの値を更新");
enemyVolume = ConfigManager.appConfig
.GetInt("enemyVolume");
enemyHealth = ConfigManager.appConfig
.GetInt("enemyHealth");
enemyDamage = ConfigManager.appConfig
.GetFloat("enemyDamage");
assignmentId = ConfigManager.appConfig
.assignmentID;
break;
}
}
void OnDestroy()
{
ConfigManager.FetchCompleted -= ApplyRemoteSettings;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment