从 google 中删除保存的游戏以编程方式玩游戏
Delete saved game from google play games programmatically
我们想用 google 玩游戏 backup/restore 我们 android 在 Unity 中玩游戏。
我们正在使用 play-games-plugin-for-unity
为此目的。
使用 github 中介绍的 ShowSelectUI() 方法,用户可以手动删除已保存的游戏。
现在的问题是我们想在 UI 中提供一个按钮,用户可以通过点击该按钮来删除已保存的游戏,有没有办法以编程方式进行删除操作?
提前致谢。
如果您知道要删除的已保存游戏实例的名称,您可以调用 Open,然后在获得已保存数据的元数据后,调用 Delete:
void DeleteSavedGame(string filename) {
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
savedGameClient.OpenWithAutomaticConflictResolution(filename, DataSource.ReadCacheOrNetwork,
ConflictResolutionStrategy.UseLongestPlaytime, OnDeleteSavedGame);
}
public void OnDeleteSavedGame(SavedGameRequestStatus status, ISavedGameMetadata game) {
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
if (status == SavedGameRequestStatus.Success) {
// delete the game.
savedGameClient.Delete(game);
} else {
// handle error
}
}
我最终根据上面的@claytonWilkinson 实现了这个。然后我添加了 DeleteGoogleCloudSave() 来点击一个按钮:
public void DeleteGoogleCloudSave(){
#if UNITY_ANDROID && !UNITY_EDITOR
Debug.Log("Opening save...");
if (isGooglePlayGamesConnected()){
((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution("Filename", GooglePlayGames.BasicApi.DataSource.ReadCacheOrNetwork,ConflictResolutionStrategy.UseLongestPlaytime, DeleteSavedGameOpened);
}
#endif
}
void DeleteSavedGameOpened(SavedGameRequestStatus status, ISavedGameMetadata meta)
{
#if UNITY_ANDROID && !UNITY_EDITOR
Debug.Log("Running SaveGameOpened...");
if(status == SavedGameRequestStatus.Success)
{
DeleteSavedGame();
}
#endif
}
void DeleteSavedGame() {
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
savedGameClient.OpenWithAutomaticConflictResolution("Filename", DataSource.ReadCacheOrNetwork,ConflictResolutionStrategy.UseLongestPlaytime, OnDeleteSavedGame);
}
public void OnDeleteSavedGame(SavedGameRequestStatus status, ISavedGameMetadata game) {
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
if (status == SavedGameRequestStatus.Success) {
// delete the game.
savedGameClient.Delete(game);
Debug.Log("Google Cloud Save Game has been deleted...");
} else {
// handle error
Debug.LogError("Google Cloud Save Game has NOT been deleted...");
}
}
我们想用 google 玩游戏 backup/restore 我们 android 在 Unity 中玩游戏。 我们正在使用 play-games-plugin-for-unity 为此目的。 使用 github 中介绍的 ShowSelectUI() 方法,用户可以手动删除已保存的游戏。 现在的问题是我们想在 UI 中提供一个按钮,用户可以通过点击该按钮来删除已保存的游戏,有没有办法以编程方式进行删除操作? 提前致谢。
如果您知道要删除的已保存游戏实例的名称,您可以调用 Open,然后在获得已保存数据的元数据后,调用 Delete:
void DeleteSavedGame(string filename) {
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
savedGameClient.OpenWithAutomaticConflictResolution(filename, DataSource.ReadCacheOrNetwork,
ConflictResolutionStrategy.UseLongestPlaytime, OnDeleteSavedGame);
}
public void OnDeleteSavedGame(SavedGameRequestStatus status, ISavedGameMetadata game) {
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
if (status == SavedGameRequestStatus.Success) {
// delete the game.
savedGameClient.Delete(game);
} else {
// handle error
}
}
我最终根据上面的@claytonWilkinson 实现了这个。然后我添加了 DeleteGoogleCloudSave() 来点击一个按钮:
public void DeleteGoogleCloudSave(){
#if UNITY_ANDROID && !UNITY_EDITOR
Debug.Log("Opening save...");
if (isGooglePlayGamesConnected()){
((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution("Filename", GooglePlayGames.BasicApi.DataSource.ReadCacheOrNetwork,ConflictResolutionStrategy.UseLongestPlaytime, DeleteSavedGameOpened);
}
#endif
}
void DeleteSavedGameOpened(SavedGameRequestStatus status, ISavedGameMetadata meta)
{
#if UNITY_ANDROID && !UNITY_EDITOR
Debug.Log("Running SaveGameOpened...");
if(status == SavedGameRequestStatus.Success)
{
DeleteSavedGame();
}
#endif
}
void DeleteSavedGame() {
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
savedGameClient.OpenWithAutomaticConflictResolution("Filename", DataSource.ReadCacheOrNetwork,ConflictResolutionStrategy.UseLongestPlaytime, OnDeleteSavedGame);
}
public void OnDeleteSavedGame(SavedGameRequestStatus status, ISavedGameMetadata game) {
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
if (status == SavedGameRequestStatus.Success) {
// delete the game.
savedGameClient.Delete(game);
Debug.Log("Google Cloud Save Game has been deleted...");
} else {
// handle error
Debug.LogError("Google Cloud Save Game has NOT been deleted...");
}
}