本主题介绍了如何在 Unity 游戏中使用 Play 游戏服务的游戏存档。
开始之前
显示游戏存档 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
}
}
打开游戏存档
为了读取或写入游戏存档数据,需要先打开游戏存档。由于游戏存档状态在设备本地缓存并保存到云端,因此可能会遇到存档数据状态冲突的情况。当设备尝试将状态保存到云端,但云端当前数据是由不同设备写入时,就会发生冲突。在打开游戏存档数据时,需要解决这些冲突。
有两种打开方法用于处理冲突解决,第一种是 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
}
}