Google play 游戏服务 : 保存游戏,创建新快照

Posted

技术标签:

【中文标题】Google play 游戏服务 : 保存游戏,创建新快照【英文标题】:Google play games services : Saved games, creating a new snapshot 【发布时间】:2019-11-19 05:05:21 【问题描述】:

我正在关注如何在我的游戏中设置保存的游戏的 google 开发者指南,当我到达您创建快照以存储数据的部分时,该指南没有指定要做什么。

我还检查了此处提供的示例代码 https://github.com/gguuss/android-basic-samples/tree/master/BasicSamples/CollectAllTheStars2

但是使用的一些方法被标记为已弃用,并且从 5 年前开始就没有更新代码

private String mCurrentSaveName = "snapshotTemp";

/**
 * This callback will be triggered after you call startActivityForResult from the
 * showSavedGamesUI method.
 */
@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent intent) 
  if (intent != null) 
    if (intent.hasExtra(SnapshotsClient.EXTRA_SNAPSHOT_METADATA)) 
      // Load a snapshot.
      SnapshotMetadata snapshotMetadata =
          intent.getParcelableExtra(SnapshotsClient.EXTRA_SNAPSHOT_METADATA);
      mCurrentSaveName = snapshotMetadata.getUniqueName();

      // Load the game data from the Snapshot
      // ...
     else if (intent.hasExtra(SnapshotsClient.EXTRA_SNAPSHOT_NEW)) 
      // Create a new snapshot named with a unique string
      String unique = new BigInteger(281, new Random()).toString(13);
      mCurrentSaveName = "snapshotTemp-" + unique;

      // Create the new snapshot
      // ...
    
  

谁能帮助我?以及在//create the new snapshot comment附近做什么?

【问题讨论】:

【参考方案1】:

经过大量研究,我发现了如何操作,在您执行此操作之前,您的播放器必须先登录

//the data you want to save must be in bytes
byte[] bytearray;
//name of the snapshot
private String currentSave="snapshot"+1235;

private void executeSaving()

    // get the snapshoClient
    SnapshotsClient snapshotsClient= Games.getSnapshotsClient(this,account);

    // to save a game
    snapshotsClient.open(currentSave,true).addOnCompleteListener(task -> 

        Snapshot snapshot =task.getResult().getData();

        if (snapshot != null)
            //call of the method writeSnapshot params : the snapshot and the data we 
            //want to save with a description
            writeSnapshot(snapshot,bytearray,"first description").addOnCompleteListener(task1 -> 

                if(task1.isSuccessful())
                    Toast.makeText(getApplicationContext(),"Succesful",Toast.LENGTH_SHORT).show();
                else 
                    Log.e("ERR",""+task1.getException());
                
            );
        
    );


private Task<SnapshotMetadata> writeSnapshot(Snapshot snapshot ,byte[] bytearray,String desc)

    //write your data in the snapshot
    snapshot.getSnapshotContents().writeBytes(bytearray);

    SnapshotMetadataChange snapshotMetadata = new SnapshotMetadataChange.Builder().setDescription(desc).build();

    SnapshotsClient snapshotsClient=Games.getSnapshotsClient(this,account);

    //commit and close to send the new changes to google play games services
    return snapshotsClient.commitAndClose(snapshot,snapshotMetadata);

【讨论】:

以上是关于Google play 游戏服务 : 保存游戏,创建新快照的主要内容,如果未能解决你的问题,请参考以下文章

如何静默下载 Google Play 游戏服务保存的游戏快照数据?

获取对快照的引用。 Google Play 游戏服务保存的游戏

是否可以在 google play 游戏服务上保存关卡?

用户退出 Google Play 服务后保存游戏的最佳做法?

Google Play 游戏,“已保存的游戏”已弃用?

Google Play 游戏服务多人游戏和后端