メインコンテンツまでスキップ

すべてを統合する - クラウドセーブ - (Unity モジュール)

Last updated on June 23, 2025

Connect the UI to Save to and Load from Cloud Save

In this section, you will learn how to connect the options menu to save and load game sound settings using AccelByte Gaming Services (AGS) Cloud Save.

  1. A Cloud Save record is stored as a pair of string keys and JSON object values. The JSON object contains key-value pairs. As mentioned in the previous section, Byte Wars uses keys and item names defined in the CloudSaveEssentialsModels class. The record that we will save looks like the example below:

    {
    "GameOptions-Sound": {
    "musicvolume": value,
    "sfxvolume": value
    }
    }
  2. Now that you understand how the record is structured, let's use the wrapper functions you created in the previous section to load the game options. Open the CloudSaveEssentialsWrapper_Starter class and add the following code:

    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);
    }
    });
    }
  3. Next, add the following code to save the game options:

    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.");
    }
    });
    }
  4. Now, bind the functions so the game saves and loads records when the Options menu is accessed. Also, bind the load function to the lobby connection event so the record loads when the user logs in.

    void Start()
    {
    cloudSave = AccelByteSDK.GetClientRegistry().GetApi().GetCloudSave();
    lobby = AccelByteSDK.GetClientRegistry().GetApi().GetLobby();

    lobby.Connected += LoadGameOptions;
    OptionsMenu.OnOptionsMenuActivated += (musicVolume, sfxVolume) => LoadGameOptions();
    OptionsMenu.OnOptionsMenuDeactivated += SaveGameOptions;
    }

Resources