Unity Scripting API の RemoteSettings クラスを使用して、コードで設定を処理できます。RemoteSettings.Updated
イベントのハンドラー関数を設定します。RemoteSettings
クラスは、新しいセッションが開始されるたびに設定されているすべてのハンドラーを呼び出します。Unity Analytics Dashboard でダウンロードする RemoteSettings
のオブジェクトの Key-Value (KVS) を作成します。
設定を取得するには ネットワークトランザクション が必要です。そのため、 RemoteSettings
オブジェクトは Updated
イベントを非同期的にディスパッチします。ハンドラー関数は、すべてのプラットフォームで同じ順序で呼び出したり、または、同じプラットフォームであっても起動するたびに同じ順序で呼び出しをするわけではありません。実際、ネットワーク接続が利用できず、キャッシュされた設定が見つからない場合、RemoteSettings
オブジェクトは Updated
イベントのディスパッチをまったく行いません。常に、妥当なデフォルト値で環境設定変数を初期化し、Updated
ハンドラーが異なる時間に、または、異なる順序で呼び出されることができるようにしてください。
コード例
以下の例は、ゲームの難易度を調整する多くのプロパティーを定義するクラスを示しています。
using UnityEngine;
public class RemoteTuningVariables : MonoBehaviour {
public float DefaultSpawnRateFactor = 1.0f;
public float DefaultEnemySpeedFactor = 1.0f;
public float DefaultEnemyStrengthFactor = 1.0f;
public static float SpawnRateFactor{ get; private set; }
public static float EnemySpeedFactor{ get; private set; }
public static float EnemyStrengthFactor{ get; private set; }
void Start () {
SpawnRateFactor = DefaultSpawnRateFactor;
EnemySpeedFactor = DefaultEnemySpeedFactor;
EnemyStrengthFactor = DefaultEnemyStrengthFactor;
RemoteSettings.Updated +=
new RemoteSettings.UpdatedEventHandler(HandleRemoteUpdate);
}
private void HandleRemoteUpdate(){
SpawnRateFactor
= RemoteSettings.GetFloat ("SpawnRateFactor", DefaultSpawnRateFactor);
EnemySpeedFactor
= RemoteSettings.GetFloat ("EnemySpeedFactor", DefaultEnemySpeedFactor);
EnemyStrengthFactor
= RemoteSettings.GetFloat ("EnemyStrengthFactor", DefaultEnemyStrengthFactor);
}
}
このクラスは、RemoteSettings.GetFloat()
メソッドを呼び出すときにデフォルト値を設定することに注意してください。RemoteSettings
オブジェクトが指定されたキーを見つけられない場合 (例えば、キー名のスペルが間違っている場合など)、メソッドはデフォルト値を変数に割り当てます。そうでない場合、GetFloat()
と GetInt()
メソッドは数値に 0 を割り当て、GetString()
は文字列に空の文字列を割り当て、GetBool()
は bool 変数に false を割り当てます。
また、このクラスは、Start()
メソッドのプロパティーにも同じデフォルト値を割り当てます。 Unity が Analytics Service にアクセスできず、以前にキャッシュした設定がローカルで利用できない場合、RemoteSettings
オブジェクトは Updated
イベントをディスパッチしません。 Start()
メソッドでデフォルトを割り当てると、プロパティーに常に適切な値が設定されます。
2017–05–30 編集レビュー を行ってパブリッシュされたページ
2017–05–30 現在、サービスは Unity 5.5 以降と互換性があります。ただし、バージョンの互換性は変更されることもあります。
2017.1 の新機能
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.