如何将多个PlayerPrefs保存到云保存

时间:2019-03-10 14:24:46

标签: c# unity3d google-cloud-platform google-play-services

我正在使用Google Play服务云存储。我找到了一个有关如何保存的教程,但有一个类似高分的问题。我想保存多个playerpref,例如,我有3个能力,并且每个能力都保存在一个playerpref中,所以我想将它们保存到云中,我有黄金可以购买这些能力,因此您可以使用IAP购买黄金我也想在云中保存黄金,但是我不知道如何保存多个Playerprefs。您可以帮我吗。

我的playerprefs是这样的

PlayerPrefs.SetInt(“ Revive”,revive);

PlayerPrefs.SetInt(“ SlowTime”,slowTime);

PlayerPrefs.SetInt(“ Immortal”,不朽);

购买IAP后,我将更改并保存黄金

PlayerPrefs.SetInt(“ Gold”,PlayerPrefs.GetInt(“ Gold”)+ 250);

这是Cloud Save脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GooglePlayGames;
using UnityEngine.SocialPlatforms;
using GooglePlayGames.BasicApi;
using GooglePlayGames.BasicApi.SavedGame;
using System.Text;
using System;

public class PlayGamesController : MonoBehaviour

{
public static PlayGamesController Instance { set; get; }

const string Save_Name = "Save";
bool isSaving;
bool isCloudDataLoaded = false;

private void Start()
{
    if (Instance == null)
    {
        DontDestroyOnLoad(gameObject);
        Instance = this;
    }
    else
    {
        Destroy(gameObject);
    }

    if (!PlayerPrefs.HasKey(Save_Name))
        PlayerPrefs.SetString(Save_Name, "0");

    if (!PlayerPrefs.HasKey("IsFirstTime"))
        PlayerPrefs.SetInt("IsFirstTime", 1);

    LoadLocal();

    PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().EnableSavedGames().Build();

    PlayGamesPlatform.InitializeInstance(config);

    PlayGamesPlatform.Activate();

    SignIn();
}

private void SignIn()
{
    Social.localUser.Authenticate((success) => {
        LoadData();
    });
}

public void AddScoreToLeaderboard(string leaderboardId, long score)
{
    Social.ReportScore(score, leaderboardId, (bool success) => { });
}

public void OnAchievementClick()
{
    if (Social.localUser.authenticated)
    {
        Social.ShowAchievementsUI();
    }
    if (!Social.localUser.authenticated)
    {
        SignIn();
    }
}

public void OnLeaderboardClick()
{
    if (Social.localUser.authenticated)
    {
        Social.ShowLeaderboardUI();
    }
    if (!Social.localUser.authenticated)
    {
        SignIn();
    }
}

#region Saved Games

string GameDataToString()
{
    return CloudVariables.highScore.ToString();
}

void StringToGameData(string cloudData, string localData)
{
    if(PlayerPrefs.GetInt("IsFirstTime") == 1)
    {
        PlayerPrefs.SetInt("IsFirstTime", 0);
        if (int.Parse(cloudData) > int.Parse(localData))
        {
            PlayerPrefs.SetString(Save_Name, cloudData);
        }
    }
    else
    {
        if(int.Parse(localData) > int.Parse(cloudData))
        {
            CloudVariables.highScore = int.Parse(localData);
            AddScoreToLeaderboard(GPGSIds.leaderboard_high_score, CloudVariables.highScore);
            isCloudDataLoaded = true;
            SaveData();
            return;
        }
    }
    CloudVariables.highScore = int.Parse(cloudData);
    isCloudDataLoaded = true;
}

void StringToGameData(string localData)
{
    CloudVariables.highScore = int.Parse(localData);
}

public void LoadData()
{
    if (Social.localUser.authenticated)
    {
        isSaving = false;
        ((PlayGamesPlatform)Social.Active).SavedGame.OpenWithManualConflictResolution(Save_Name, DataSource.ReadCacheOrNetwork, true, ResolveConflict, OnSavedGameOpened);
    }
    else
    {
        LoadLocal();
    }
}

private void LoadLocal()
{
    StringToGameData(PlayerPrefs.GetString(Save_Name));
}

public void SaveData()
{
    if (!isCloudDataLoaded)
    {
        SaveLocal();
        return;
    }
    if (Social.localUser.authenticated)
    {
        isSaving = true;
        ((PlayGamesPlatform)Social.Active).SavedGame.OpenWithManualConflictResolution(Save_Name, DataSource.ReadCacheOrNetwork, true, ResolveConflict, OnSavedGameOpened);
    }
    else
    {
        SaveLocal();
    }
}

private void SaveLocal()
{
    PlayerPrefs.SetString(Save_Name, GameDataToString());
}

private void ResolveConflict(IConflictResolver resolver, ISavedGameMetadata original, byte[] originalData, ISavedGameMetadata unmerged, byte[] unmergedData)
{
    if (originalData == null)
        resolver.ChooseMetadata(unmerged);
    else if (unmergedData == null)
        resolver.ChooseMetadata(original);
    else
    {
        string originalStr = Encoding.ASCII.GetString(originalData);
        string unmergedStr = Encoding.ASCII.GetString(unmergedData);

        int originalNum = int.Parse(originalStr);
        int unmergedNum = int.Parse(unmergedStr);

        if(originalNum > unmergedNum)
        {
            resolver.ChooseMetadata(original);
            return;
        }
        else if (unmergedNum > originalNum)
        {
            resolver.ChooseMetadata(unmerged);
            return;
        }
        resolver.ChooseMetadata(original);
    }
}

private void OnSavedGameOpened(SavedGameRequestStatus status, ISavedGameMetadata game)
{
    if(status == SavedGameRequestStatus.Success)
    {
        if (!isSaving)
            LoadGame(game);
        else
            SaveGame(game);
    }
    else
    {
        if (!isSaving)
            LoadLocal();
        else
            SaveLocal();
    }
}

private void LoadGame(ISavedGameMetadata game)
{
    ((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(game, OnSavedGameDataRead);
}

private void SaveGame(ISavedGameMetadata game)
{
    string stringToSave = GameDataToString();
    PlayerPrefs.SetString(Save_Name, stringToSave);

    byte[] dataToSave = Encoding.ASCII.GetBytes(stringToSave);

    SavedGameMetadataUpdate update = new SavedGameMetadataUpdate.Builder().Build();

    ((PlayGamesPlatform)Social.Active).SavedGame.CommitUpdate(game, update, dataToSave, OnSavedGameDataWritten);
}

private void OnSavedGameDataRead(SavedGameRequestStatus status, byte[] savedData)
{
    if(status == SavedGameRequestStatus.Success)
    {
        string cloudDataString;
        if (savedData.Length == 0)
            cloudDataString = "0";
        else
            cloudDataString = Encoding.ASCII.GetString(savedData);

        string localDataString = PlayerPrefs.GetString(Save_Name);

        StringToGameData(cloudDataString, localDataString);
    }
}

private void OnSavedGameDataWritten(SavedGameRequestStatus status, ISavedGameMetadata game)
{

}
#endregion /Saved Games
}

0 个答案:

没有答案