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

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

Last updated on October 23, 2024

What's in the Starter Pack

In this section, you will learn how to update statistics (stats) values with gameplay data and display them in the Stats Profile Menu.

The CloudSaveHelper_Starter class that subscribes to events related to changes in the Options Menu has been provided for you.

  • CloudSaveHelper_Starter file: /Assets/Resources/Modules/CloudSaveEssentials/Scripts/CloudSaveHelper_Starter.cs

The helper class has some functionalities already implemented:

  • The AccelByte Gaming Serivces (AGS) Game SDK libraries declarations to allow the use of the Statistic and ServerStatistic classes.

    using AccelByte.Core;
    using AccelByte.Models;

  • Some event definitions to notify when the player logs in successfully, opens the Options Menu, and closes the Options Menu.

    void Start()
    {
    LoginHandler.onLoginCompleted += tokenData => {};
    OptionsMenu.onOptionsMenuActivated += (musicVolume, sfxVolume) => {};
    OptionsMenu.onOptionsMenuDeactivated += (musicVolume, sfxVolume) => {};
    }

Connect the UI to save data to cloud save

  1. Open the CloudSaveHelper_Starter.cs.

  2. Define constant variables to hold the unique identifier and record the item names of the audio volume settings player records.

    private const string GameOptionsRecordKey = "GameOptions-Sound";
    private const string MusicVolumeItemName = "musicvolume";
    private const string SfxVolumeItemName = "sfxvolume";
  3. Add a local variable to hold the reference of CloudSaveEssentialsWrapper_Starter and a dictionary of strings and objects that maps the player record item name of each desired audio volume setting.

    private CloudSaveEssentialsWrapper_Starter _cloudSaveWrapper;
    private Dictionary<string, object> volumeSettings;
  4. In Start(), initialize the _cloudSaveWrapper by using the utility class called TutorialModuleManager to get the module class. Also, initialize the volumeSettings dictionary with the corresponding values of music and sound effects (SFX) volumes that are stored locally with the AudioManager.

    void Start()
    {
    _cloudSaveWrapper = TutorialModuleManager.Instance.GetModuleClass<CloudSaveEssentialsWrapper_Starter>();

    volumeSettings = new Dictionary<string, object>()
    {
    {MusicVolumeItemName, AudioManager.Instance.GetCurrentVolume(AudioManager.AudioType.MusicAudio)},
    {SfxVolumeItemName, AudioManager.Instance.GetCurrentVolume(AudioManager.AudioType.SfxAudio)}
    };
    }
  5. Create a callback function to inform when the save audio volume settings function completes.

    private void OnSaveGameOptionsCompleted(Result result)
    {
    if (!result.IsError)
    {
    Debug.Log("Audio volume settings saved to cloud save!");
    }
    }
  6. Create a function to call the the SaveUserRecord() function in CloudSaveEssentialsWrapper_Starter. Use the GameOptionsRecordKey constant as the unique identifier of which player record you are saving. The volumeSettings dictionary will use the latest value from the AudioManager and save it to their corresponding player record item.

    private void SaveGameOptions()
    {
    _cloudSaveWrapper.SaveUserRecord(GameOptionsRecordKey, volumeSettings, OnSaveGameOptionsCompleted);
    }
  7. To ensure the data is updated, create a function that will be called upon the Options Menu closing to get the latest values of the audio volume settings and call the SaveGameOptions() function to update the player record data.

    private void UpdateGameOptions(float musicVolume, float sfxVolume)
    {
    volumeSettings[MusicVolumeItemName] = musicVolume;
    volumeSettings[SfxVolumeItemName] = sfxVolume;

    SaveGameOptions();
    }
  8. In the Start() function, modify the listener of the onOptionsMenuDeactivated event to call the UpdateGameOptions() function so that you will always save the latest value to AGS Cloud Save when the player is done changing their audio volume settings in the Options Menu.

    private void Start()
    {
    ...

    OptionsMenu.onOptionsMenuDeactivated += (musicVolume, sfxVolume) => UpdateGameOptions(musicVolume, sfxVolume);
    }

Connect the UI to load data from cloud save

  1. Open CloudSaveHelper_Starter.cs.

  2. Prepare the callback function to handle getting the player record results. In Byte Wars, the music and SFX volume settings are also saved locally using PlayerPrefs in addition to the player records in AGS Cloud Save. You will use the AudioManager class to set and get the audio volume settings. If no player record exists, then you will save the current local settings.

    private void OnGetGameOptionsCompleted(Result<UserRecord> result)
    {
    if (!result.IsError)
    {
    Dictionary<string, object> recordData = result.Value.value;
    if (recordData != null)
    {
    volumeSettings[MusicVolumeItemName] = recordData[MusicVolumeItemName];
    AudioManager.Instance.SetMusicVolume(Convert.ToSingle(recordData[MusicVolumeItemName]));

    volumeSettings[SfxVolumeItemName] = recordData[SfxVolumeItemName];
    AudioManager.Instance.SetSfxVolume(Convert.ToSingle(recordData[SfxVolumeItemName]));
    }
    }
    else
    {
    SaveGameOptions();
    }
    }
  3. Create a new function to call the GetUserRecord() function in CloudSaveEssentialsWrapper_Starter and query the audio volume settings player record.

    private void GetGameOptions()
    {
    _cloudSaveWrapper.GetUserRecord(GameOptionsRecordKey, OnGetGameOptionsCompleted);
    }
  4. Modify the Start() function to listen to the onLoginCompleted event from the LoginHandler to know when the player successfully logs in and onOptionsMenuActivated event to know when the Options Menu opens. Bind those events to call GetGameOptions() when the events trigger to set the audio volume settings according to the saved player record stored in AGS Cloud Save.

    private void Start()
    {
    ...
    LoginHandler.onLoginCompleted += tokenData => GetGameOptions();
    OptionsMenu.onOptionsMenuActivated += (musicVolume, sfxVolume) => GetGameOptions();
    }

Resources