Exemplo n.º 1
0
        /// <summary>
        /// Clears all cloud variables currently stored in the cloud.
        /// </summary>
        private static void DeleteCloudData()
        {
#if !UNITY_EDITOR && UNITY_ANDROID
#if CLOUDONCE_AMAZON
            if (AGSClient.IsServiceReady())
            {
                using (var dataMap = AGSWhispersyncClient.GetGameData())
                {
                    using (var developerString = dataMap.getDeveloperString(DevStringKey))
                    {
                        developerString.setValue(string.Empty);
                        if (AGSPlayerClient.IsSignedIn())
                        {
                            AGSWhispersyncClient.Synchronize();
                        }
                        else
                        {
                            AGSWhispersyncClient.Flush();
                        }
                    }
                }
            }
#elif CLOUDONCE_GOOGLE
            if (GooglePlayGamesCloudProvider.Instance.IsGpgsInitialized && PlayGamesPlatform.Instance.IsAuthenticated())
            {
                PlayGamesPlatform.Instance.SavedGame.OpenWithAutomaticConflictResolution(
                    "GameData",
                    DataSource.ReadCacheOrNetwork,
                    ConflictResolutionStrategy.UseLongestPlaytime,
                    (status, metadata) =>
                {
                    if (status == SavedGameRequestStatus.Success)
                    {
                        PlayGamesPlatform.Instance.SavedGame.Delete(metadata);
                    }
                });
            }
#endif
#elif !UNITY_EDITOR && UNITY_IOS
            iCloudBridge.DeleteString(DevStringKey);
#endif
            PlayerPrefs.DeleteKey(DevStringKey);
            PlayerPrefs.Save();
        }
    /// <summary>
    /// Draws the menu. Note that this must be called from an OnGUI function.
    /// </summary>
    public override void DrawMenu()
    {
        // if cloud data has been received, show how long ago that was.
        if (lastOnNewCloudData.HasValue)
        {
            double timeElapsed = (System.DateTime.Now - lastOnNewCloudData.Value).TotalSeconds;
            AmazonGUIHelpers.CenteredLabel(string.Format(cloudDataLastReceivedLabel, timeElapsed));
        }

        if (lastOnDataUploadedToCloud.HasValue)
        {
            double timeElapsed = (System.DateTime.Now - lastOnDataUploadedToCloud.Value).TotalSeconds;
            AmazonGUIHelpers.CenteredLabel(string.Format(uploadedToCloudLabel, timeElapsed));
        }

        if (lastOnThrottled.HasValue)
        {
            double timeElapsed = (System.DateTime.Now - lastOnThrottled.Value).TotalSeconds;
            AmazonGUIHelpers.CenteredLabel(string.Format(lastThrottledLabel, timeElapsed));
        }

        if (lastOnDiskWriteComplete.HasValue)
        {
            double timeElapsed = (System.DateTime.Now - lastOnDiskWriteComplete.Value).TotalSeconds;
            AmazonGUIHelpers.CenteredLabel(string.Format(diskWriteCompleteLabel, timeElapsed));
        }

        if (lastOnFirstSynchronize.HasValue)
        {
            double timeElapsed = (System.DateTime.Now - lastOnFirstSynchronize.Value).TotalSeconds;
            AmazonGUIHelpers.CenteredLabel(string.Format(firstSynchronizeLabel, timeElapsed));
        }

        if (lastOnAlreadySynchronized.HasValue)
        {
            double timeElapsed = (System.DateTime.Now - lastOnAlreadySynchronized.Value).TotalSeconds;
            AmazonGUIHelpers.CenteredLabel(string.Format(alreadySychronizedLabel, timeElapsed));
        }

        if (lastOnSyncFailed.HasValue)
        {
            double timeElapsed = (System.DateTime.Now - lastOnSyncFailed.Value).TotalSeconds;
            AmazonGUIHelpers.CenteredLabel(string.Format(syncFailedLabel, timeElapsed, failReason));
        }


        else
        {
            // display a message that cloud data has not been received yet.
            AmazonGUIHelpers.CenteredLabel(noCloudDataReceivedLabel);
        }

        // This button allows the user to synchronize GameCircle data.
        if (GUILayout.Button(syncDataButtonLabel))
        {
            AGSWhispersyncClient.Synchronize();
        }

        // a small space to spread things out.
        GUILayout.Label(GUIContent.none);

        // This button allows the user to flush GameCircle data.
        if (GUILayout.Button(flushButtonLabel))
        {
            AGSWhispersyncClient.Flush();
        }

        // a small space to spread things out.
        GUILayout.Label(GUIContent.none);

        // try and initialize the Whispersync data map.
        InitializeDataMapIfAvailable();

        if (null == dataMap)
        {
            // if it couldn't be retrieved, bail out with an error.
            AmazonGUIHelpers.CenteredLabel(whispersyncUnavailableLabel);
            return;
        }

        // draws the available syncable numbers
        DrawSyncableNumbers();

        // draws the available accumulating numbers
        DrawAccumulatingNumbers();

        // draws the available syncable number lists
        DrawSyncableNumberLists();

        // draws the available hash sets.
        DrawHashSets();

        // draws the developer string
        DrawDeveloperString();
    }
        /// <summary>
        /// Saves all cloud variables, to both disk and cloud.
        /// If <see cref="Cloud.CloudSaveEnabled"/> is <c>false</c>, it will only save to disk.
        /// Skips saving if no variables have been changed.
        /// </summary>
        public void Save()
        {
            if (saveInitialized)
            {
                return;
            }

            saveInitialized = true;

            DataManager.SaveToDisk();
            if (!AmazonCloudProvider.Instance.CloudSaveInitialized || !Cloud.CloudSaveEnabled)
            {
#if CO_DEBUG
                Debug.LogWarning(!AmazonCloudProvider.Instance.CloudSaveInitialized
                    ? "Cloud Save has not been initialized, skipping upload and only saving to disk."
                    : "Cloud Save is currently disabled, skipping upload and only saving to disk.");
#endif
                saveInitialized = false;
                cloudOnceEvents.RaiseOnCloudSaveComplete(false);
                return;
            }

            if (DataManager.IsLocalDataDirty)
            {
                if (AGSClient.IsServiceReady())
                {
#if CO_DEBUG
                    Debug.Log("Saving cloud data");
#endif
                    using (var dataMap = AGSWhispersyncClient.GetGameData())
                    {
                        using (var developerString = dataMap.getDeveloperString(DataManager.DevStringKey))
                        {
                            developerString.setValue(DataManager.SerializeLocalData().ToBase64String());
                            if (AGSPlayerClient.IsSignedIn())
                            {
                                AGSWhispersyncClient.Synchronize();
                            }
                            else
                            {
                                AGSWhispersyncClient.Flush();
                            }

                            saveInitialized = false;
                            DataManager.IsLocalDataDirty = false;
                            cloudOnceEvents.RaiseOnCloudSaveComplete(true);
                        }
                    }
                }
                else
                {
                    saveInitialized = false;
                    Debug.LogWarning("Attempted to save cloud data, but the AGS service is not ready.");
                    cloudOnceEvents.RaiseOnCloudSaveComplete(false);
                }
            }
            else
            {
#if CO_DEBUG
                Debug.Log("Save called, but no data has changed since last save.");
#endif
                saveInitialized = false;
                cloudOnceEvents.RaiseOnCloudSaveComplete(false);
            }
        }