本主题介绍了如何在 Unity 游戏中使用 Play 游戏服务的游戏存档。
前期准备
显示游戏存档界面
您可以通过调用以下内容来显示用于选择或创建游戏存档条目的标准界面:
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 个 Open 方法解决冲突,一个是 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 有四个参数:
- 向传递到某个 Open 调用的回调传递的游戏存档元数据。
- 对元数据进行的更新。
- 数据的实际字节数组。
- 在提交完成时调用的回调。
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
}
}