Unity ゲームの保存済みゲーム

このトピックでは、Unity ゲームで Play ゲームサービス向けの保存済みゲームを使用する方法について説明します。

準備

  • プロジェクトと Unity 用の Google Play Games プラグインを設定します。詳細については、スタートガイドをご覧ください。

  • 保存済みゲームを有効にしました。詳細については、保存済みゲームガイドをご覧ください。

保存済みゲームの UI を表示する

以下のように呼び出して、保存済みゲームエントリを選択または作成するための標準 UI を表示します。

    void ShowSelectUI() {
        uint maxNumToDisplay = 5;
        bool allowCreateNew = false;
        bool allowDelete = true;

        ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
        savedGameClient.ShowSelectSavedGameUI("Select saved game",
            maxNumToDisplay,
            allowCreateNew,
            allowDelete,
            OnSavedGameSelected);
    }


    public void OnSavedGameSelected (SelectUIStatus status, ISavedGameMetadata game) {
        if (status == SelectUIStatus.SavedGameSelected) {
            // handle selected game save
        } else {
            // handle cancel or error
        }
    }

保存済みゲームを開く

保存済みゲームに対してデータを読み書きするには、保存済みゲームを開く必要があります。保存されたゲーム ステータスは、デバイス上でローカルにキャッシュ保存されてクラウドに保存されるため、保存されたデータの状態に競合が発生する可能性があります。競合は、デバイスが状態をクラウドに保存しようとしたときに、現在クラウド上にあるデータが別のデバイスによって書き込まれた場合に発生します。この競合は、保存されたゲームデータを開く際に解決する必要があります。

競合の解決を処理する 2 つのオープン メソッドがあります。最初の OpenWithAutomaticConflictResolution は標準の解決戦略タイプを受け入れ、競合を自動的に解決します。もう一方のメソッドである OpenWithManualConflictResolution は、コールバック メソッドを受け入れて、競合を手動で解決できるようにします。

これらのメソッドの詳細については、GooglePlayGames/BasicApi/SavedGame/ISavedGameClient.cs をご覧ください。

    void OpenSavedGame(string filename) {
        ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
        savedGameClient.OpenWithAutomaticConflictResolution(filename, DataSource.ReadCacheOrNetwork,
            ConflictResolutionStrategy.UseLongestPlaytime, OnSavedGameOpened);
    }

    public void OnSavedGameOpened(SavedGameRequestStatus status, ISavedGameMetadata game) {
        if (status == SavedGameRequestStatus.Success) {
            // handle reading or writing of saved game.
        } else {
            // handle error
        }
    }

保存済みゲームを作成する

保存済みゲームのファイルを開いたら、ゲームの状態を保存するための書き込みが可能です。これを行うには CommitUpdate を呼び出します。CommitUpdate には 4 つのパラメータがあります。

  1. Open 呼び出しのいずれかに渡されたコールバックに渡された保存済みゲーム メタデータ
  2. メタデータに対して実施する更新
  3. データの実際のバイト配列
  4. commit が完了したときに呼び出すコールバック
    void SaveGame (ISavedGameMetadata game, byte[] savedData, TimeSpan totalPlaytime) {
        ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;

        SavedGameMetadataUpdate.Builder builder = new SavedGameMetadataUpdate.Builder();
        builder = builder
            .WithUpdatedPlayedTime(totalPlaytime)
            .WithUpdatedDescription("Saved game at " + DateTime.Now());
        if (savedImage != null) {
            // This assumes that savedImage is an instance of Texture2D
            // and that you have already called a function equivalent to
            // getScreenshot() to set savedImage
            // NOTE: see sample definition of getScreenshot() method below
            byte[] pngData = savedImage.EncodeToPNG();
            builder = builder.WithUpdatedPngCoverImage(pngData);
        }
        SavedGameMetadataUpdate updatedMetadata = builder.Build();
        savedGameClient.CommitUpdate(game, updatedMetadata, savedData, OnSavedGameWritten);
    }

    public void OnSavedGameWritten (SavedGameRequestStatus status, ISavedGameMetadata game) {
        if (status == SavedGameRequestStatus.Success) {
            // handle reading or writing of saved game.
        } else {
            // handle error
        }
    }

    public Texture2D getScreenshot() {
        // Create a 2D texture that is 1024x700 pixels from which the PNG will be
        // extracted
        Texture2D screenShot = new Texture2D(1024, 700);

        // Takes the screenshot from top left hand corner of screen and maps to top
        // left hand corner of screenShot texture
        screenShot.ReadPixels(
            new Rect(0, 0, Screen.width, (Screen.width/1024)*700), 0, 0);
        return screenShot;
    }

保存済みゲームを読み込む

保存済みゲームのファイルを開くと、読み取りを行い、ゲームの状態を読み込むことができます。これを行うには ReadBinaryData を呼び出します。

    void LoadGameData (ISavedGameMetadata game) {
        ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
        savedGameClient.ReadBinaryData(game, OnSavedGameDataRead);
    }

    public void OnSavedGameDataRead (SavedGameRequestStatus status, byte[] data) {
        if (status == SavedGameRequestStatus.Success) {
            // handle processing the byte array data
        } else {
            // handle error
        }
    }

保存済みのゲームを削除する

保存したゲームファイルは、開いた後で削除できます。これを行うには Delete を呼び出します。

    void DeleteGameData (string filename) {
        // Open the file to get the metadata.
        ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
        savedGameClient.OpenWithAutomaticConflictResolution(filename, DataSource.ReadCacheOrNetwork,
            ConflictResolutionStrategy.UseLongestPlaytime, DeleteSavedGame);
    }

    public void DeleteSavedGame(SavedGameRequestStatus status, ISavedGameMetadata game) {
        if (status == SavedGameRequestStatus.Success) {
            ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
            savedGameClient.Delete(game);
        } else {
            // handle error
        }
    }