/// <summary> /// After all Android cloud saves are fetched this method opens each save file. /// Google Play requires that each cloud save is "opened" before actually loading or saving it. /// </summary> /// <param name="status">Saved game request status</param> /// <param name="savesList">All fetched saves</param> private void OnPlayServicesSavesFetched(SavedGameRequestStatus status, List <ISavedGameMetadata> savesList) { var cloudLoadStatus = new CloudLoadStatus(savesList, OnAllSavesLoaded); if (status == SavedGameRequestStatus.Success) { foreach (var save in savesList) { var fileName = save.Filename; OpenAndroidCloudSave(save.Filename, (requestStatus, metadata) => OnPlayServicesLoadResponse(requestStatus, metadata, cloudLoadStatus), error => OnPlayServicesError(error, fileName, cloudLoadStatus)); } } else { Logger.LogError($"Failed to fetch all cloud saved games {status}"); } }
/// <summary> /// Check if cloud save was successfully loaded. If true then cloud save file is deserialized. /// CloudLoadStatus is also updated to inform that a file is loaded. /// </summary> /// <param name="status"></param> /// <param name="data"></param> /// <param name="cloudLoadStatus"></param> /// <param name="meta"></param> private void OnPlayServicesLoaded(SavedGameRequestStatus status, byte[] data, CloudLoadStatus cloudLoadStatus, ISavedGameMetadata meta = null) { switch (status) { case SavedGameRequestStatus.Success: Logger.Log("Loaded from cloud."); if (meta != null) { string json = DeserializeByte(data); cloudLoadStatus.Loaded(meta.Filename, meta.Filename, json); } break; default: //error Logger.LogError($"Failed to load. {status}"); throw new Exception($"Failed to load. {status}"); } }
/// <summary> /// Opening a cloud save was successful. It can now be loaded. /// </summary> /// <param name="status"></param> /// <param name="meta"></param> /// <param name="cloudLoadStatus"></param> private void OnPlayServicesLoadResponse(SavedGameRequestStatus status, ISavedGameMetadata meta, CloudLoadStatus cloudLoadStatus) { #if UNITY_ANDROID if (status == SavedGameRequestStatus.Success) { var platform = (PlayGamesPlatform)Social.Active; platform.SavedGame.ReadBinaryData(meta, (dataReadStatus, binaryData) => OnPlayServicesLoaded(dataReadStatus, binaryData, cloudLoadStatus, meta)); } else { OnPlayServicesLoaded(status, null, cloudLoadStatus); } #endif }
/// <summary> /// Failed opening an Android cloud save. /// </summary> /// <param name="err"></param> /// <param name="fileName"></param> /// <param name="cloudLoadStatus"></param> private void OnPlayServicesError(PlayServiceError err, string fileName, CloudLoadStatus cloudLoadStatus) { Logger.Log(err); cloudLoadStatus.Loaded(fileName); throw new Exception($"Error opening Android cloud saves. {err}"); }