Saved Games - StansAssets/com.stansassets.ultimate-mobile GitHub Wiki
Implement saved games using the cross-platform Ultimate Mobile API based on Google Play Snapshots & iOS Game Saves API.
Before you begin you might want to check out the configuration guides for platforms you want to have:
Fetch Saved Games List
Use the FetchSavedGames method to retrieve available saved games list and display inside your application UI, allowing a player to pick a game save he needs. See the code snippet below:
using SA.CrossPlatform.GameServices;
...
var client = UM_GameService.SavedGamesClient;
client.FetchSavedGames((result) => {
if(result.IsSucceeded) {
foreach(var snapshot in result.Snapshots) {
Debug.Log($"snapshot name: {snapshot.Name}");
Debug.Log($"snapshot deviceName: {snapshot.DeviceName}");
}
} else {
Debug.LogError($"FetchSavedGames failed: {result.Error.FullMessage}");
}
});
Load Game Data
Once a user picked game save he wanted to load from the available saves list, you may download save binary data using the LoadGameData method.
using SA.CrossPlatform.GameServices;
...
public void LoadGame(UM_iSavedGameMetadata game) {
var client = UM_GameService.SavedGamesClient;
client.LoadGameData(game, (result) => {
if (result.IsSucceeded) {
byte[] data = result.Data;
Debug.Log($"Data size (bytes): {data.Data.Length}");
var meta = result.Meta;
Debug.Log($"Description {meta.Description}");
Debug.Log($"PlayedTime {meta.PlayedTime}");
Debug.Log($"ProgressValue {meta.ProgressValue}");
//Restore your game progress here
} else {
Debug.LogError($"Failed to load saved game data: {result.Error.FullMessage}");
}
});
}
Saving a Game
You may also create a new game save using the SaveGame method.
using SA.CrossPlatform.GameServices;
...
public void SaveGame(string name, byte[] data) {
var client = UM_GameService.SavedGamesClient;
client.SaveGame(name, data, result => {
if (result.IsSucceeded) {
//FetchSavedGames();
}
else {
Debug.LogError($"Failed to save game: {result.Error.FullMessage}");
}
});
}
Delete Game Save
If you wish to provide an ability to delete user-created game saves, please use the Delete method.
using SA.CrossPlatform.GameServices;
...
private void DeleteGameSave(UM_iSavedGameMetadata game) {
var client = UM_GameService.SavedGamesClient;
client.Delete(game, result => {
if (result.IsSucceeded) {
FetchSavedGames();
}
else {
Debug.LogError($"Failed to delete saved game: {result.Error.FullMessage}");
}
});
}