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

クラウドセーブに SDK を使用する - クラウドセーブ - (Unity モジュール)

Last updated on June 5, 2024

Unwrap the wrapper

In this tutorial, you will learn how to store and query your game settings using the AccelByte Gaming Services (AGS) Game SDK. The game options will be stored as a Player Record in the AGS Cloud Save service.

Byte Wars uses a wrapper class named CloudSaveEssentialsWrapper that acts as the wrapper to cache and handle login-related functionalities when using the AGS Game SDK.

The CloudSaveEssentialsWrapper class uses the ApiClient and CloudSave class provided by the AGS Game SDK. They interact with AGS to provide the ability to create, query, update, and delete records in the Cloud Save service.

What's in the Starter Pack

The class CloudSaveEssentialsWrapper_Starter has been provided for you as the starter code. You can find the file in the Resources section.

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

There are some functionalities already implemented for you:

  • Namespace declarations for using the CloudSave class in the AGS Game SDK.

    using AccelByte.Api;
    using AccelByte.Core;
    using AccelByte.Models;
  • A local variable that holds the reference to the CloudSave class in the AGS Game SDK.

    private CloudSave cloudSave;

Integrate Cloud Save using the SDK

In this section, you will implement using the AGS Game SDK to integrate the Cloud Save service, first on the client side, then on the server side.

Client side

  1. Open Byte Wars in Unity.

  2. Open CloudSaveEssentialsWrapper_Starter. In Start(), initialize the cloudSave variable to get the client ApiClient CloudSave object.

    void Start()
    {
    cloudSave = MultiRegistry.GetApiClient().GetCloudSave();
    }
  3. Implement the function for saving player records. Since the process is asynchronous, create a callback function that will inform the user regarding the result. Add the AGS Game SDK Result and ResultCallback as parameters so the result will be accessible from other scripts.

    private void OnSaveUserRecordCompleted(Result result, ResultCallback customCallback = null)
    {
    if (!result.IsError)
    {
    Debug.Log("Save player record from client successful.");
    }
    else
    {
    Debug.Log($"Save player record from client failed. Message: {result.Error.Message}");
    }

    customCallback?.Invoke(result);
    }
  4. Create the wrapper function that will call the SaveUserRecord() function in the CloudSave class. The function will take a recordKey string as the unique identifier for the record, a recordRequest dictionary that maps the record unique identifier to the update request object, a ResultCallback that holds the callback function that will be called when the operation completed, and a boolean to set the record as public.

    public void SaveUserRecord(string recordKey, Dictionary<string, object> recordRequest, ResultCallback resultCallback)
    {
    cloudSave.SaveUserRecord(
    recordKey,
    recordRequest,
    result => OnSaveUserRecordCompleted(result, resultCallback),
    true
    );
    }
    注記

    The SaveUserRecord() function will automatically create the player record if it doesn't exist yet. If the player record exists, this function will update the record by overwriting with the value in the update request.

  5. Implement the function for querying player records. Since the process is asynchronous, create a callback function that will inform the user regarding the result. Add the AGS Game SDK Result and ResultCallback as parameters so the result will be accessible from other scripts.

    private void OnGetUserRecordCompleted(Result<UserRecord> result, ResultCallback<UserRecord> customCallback = null)
    {
    if (!result.IsError)
    {
    Debug.Log("Get player record from vlient successful.");
    }
    else
    {
    Debug.Log($"Get player record from client failed. Message: {result.Error.Message}");
    }

    customCallback?.Invoke(result);
    }
  6. Create the wrapper function that will call the GetUserRecord() function in the CloudSave class. The function will take a recordKey string as the unique identifier to the record and the ResultCallback that holds the callback function that will be called when the operation completes.

    public void GetUserRecord(string recordKey, ResultCallback<UserRecord> resultCallback)
    {
    cloudSave.GetUserRecord(
    recordKey,
    result => OnGetUserRecordCompleted(result, resultCallback)
    );
    }

Resources