すべてを統合する - クラウドセーブ - (Unity モジュール)
Last updated on February 4, 2026
注釈:本資料はAI技術を用いて翻訳されています。
UI をクラウドセーブへの保存と読み込みに接続する
このセクションでは、AccelByte Gaming Services (AGS) クラウドセーブを使用してゲームのサウンド設定を保存および読み込むために、オプションメニューを接続する方法を学びます。
-
クラウドセーブのレコードは、文字列キーと JSON オブジェクト値のペアとして保存されます。JSON オブジェクトにはキーと値のペアが含まれます。前のセクションで述べたように、Byte Wars は
CloudSaveEssentialsModelsクラスで定義されたキーとアイテム名を使用します。保存するレコードは以下の例のようになります。{
"GameOptions-Sound": {
"musicvolume": value,
"sfxvolume": value
}
} -
レコードの構造を理解したので、前のセクションで作成したラッパー関数を使用してゲームオプションを読み込みましょう。
CloudSaveEssentialsWrapper_Starterクラスを開き、以下のコードを追加します。public void LoadGameOptions()
{
GetUserRecord(CloudSaveEssentialsModels.GameOptionsRecordKey, (Result<UserRecord> result) =>
{
if (result.IsError)
{
BytewarsLogger.LogWarning($"Failed to load game options from Cloud Save. Error {result.Error.Code}: {result.Error.Message}");
return;
}
BytewarsLogger.Log("Successfully loaded game options from Cloud Save.");
// Apply record data to local game option settings.
Dictionary<string, object> recordData = result.Value.value;
if (recordData != null)
{
float musicVolume = Convert.ToSingle(recordData[CloudSaveEssentialsModels.MusicVolumeItemName]);
float sfxVolume = Convert.ToSingle(recordData[CloudSaveEssentialsModels.SfxVolumeItemName]);
AudioManager.Instance.SetMusicVolume(musicVolume);
AudioManager.Instance.SetSfxVolume(sfxVolume);
}
});
} -
次に、ゲームオプションを保存するために以下のコードを追加します。
private void SaveGameOptions(float musicVolume, float sfxVolume)
{
// Collect the local game option values to save.
Dictionary<string, object> gameOptions = new Dictionary<string, object>()
{
{
CloudSaveEssentialsModels.MusicVolumeItemName,
AudioManager.Instance.GetCurrentVolume(AudioManager.AudioType.MusicAudio)
},
{
CloudSaveEssentialsModels.SfxVolumeItemName,
AudioManager.Instance.GetCurrentVolume(AudioManager.AudioType.SfxAudio)
}
};
SaveUserRecord(CloudSaveEssentialsModels.GameOptionsRecordKey, gameOptions, (Result result) =>
{
if (result.IsError)
{
BytewarsLogger.LogWarning($"Failed to save game options to Cloud Save. Error {result.Error.Code}: {result.Error.Message}");
}
else
{
BytewarsLogger.Log("Successfully saved game options to Cloud Save.");
}
});
} -
次に、オプションメニューがアクセスされたときにゲームがレコードを保存および読み込むように関数をバインドします。また、ユーザーがログインしたときにレコードが読み込まれるように、ロビー接続イベントに読み込み関数をバインドします。
void Start()
{
cloudSave = AccelByteSDK.GetClientRegistry().GetApi().GetCloudSave();
lobby = AccelByteSDK.GetClientRegistry().GetApi().GetLobby();
lobby.Connected += LoadGameOptions;
OptionsMenu.OnOptionsMenuActivated += (musicVolume, sfxVolume) => LoadGameOptions();
OptionsMenu.OnOptionsMenuDeactivated += SaveGameOptions;
}
リソース
-
このチュートリアルで使用されているファイルは、Unity Byte Wars GitHub リポジトリで入手できます。