您如何云保存每个级别的数据。像糖果迷恋游戏一样的统一游戏

时间:2019-01-14 08:06:20

标签: android unity3d cloud

我正在使用Unity开发适用于Android的益智游戏。因此,我想保存在Google Play Cloud中每个级别获得的得分,时间和星级。但是,如果我尝试将其保存为1个以上的级别,则可以将其保存为一个级别。它会覆盖先前的数据。那我该如何克服这个问题呢?

public class CloudSave : MonoBehaviour {
[SerializeField]
private Text message;
private int totalCoin = 20;
public int levelNumber;
public int starEarnedPerLevel;
#region Cloud_Save

private string GetSaveString() {
    string data = "";
    data += PlayerPrefs.GetInt("HighScore").ToString();
    data += "|";
    data += totalCoin.ToString();
    data += "|";
    data += levelNumber.ToString();
    data += "|";
    data += starEarnedPerLevel.ToString();
    return data;
}

private void LoadSaveString(string save) {
    string[] data = save.Split('|');
    PlayerPrefs.SetInt("HighScore", int.Parse(data[0]));
    totalCoin = int.Parse(data[1]);
    levelNumber = int.Parse(data[2]);
    starEarnedPerLevel = int.Parse(data[3]);
    Debug.Log("LoadSaveString Function");
}   

private bool isSaving = false;

public void OpenSave(bool saving) {
    Debug.Log("Open Save");
    if (Social.localUser.authenticated) {
        isSaving = saving;
        ((PlayGamesPlatform)Social.Active).SavedGame
            .OpenWithAutomaticConflictResolution(
            "MyCloudFile", 
            DataSource.ReadCacheOrNetwork, 
            ConflictResolutionStrategy.UseLongestPlaytime, SavedGameOpen);
    }
}

private void SavedGameOpen(SavedGameRequestStatus reqStatus, ISavedGameMetadata metadata) {
    Debug.Log("SavedGameOpen");

    if (reqStatus == SavedGameRequestStatus.Success) {
        if (isSaving)// Writting
        { 
            byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(GetSaveString());
            SavedGameMetadataUpdate update = new SavedGameMetadataUpdate.Builder().WithUpdatedDescription("Saved At :" + System.DateTime.Now.ToString()).Build();
            ((PlayGamesPlatform)Social.Active).SavedGame.CommitUpdate(metadata, update, data, SaveUpdate);

        }
        else // Reading or Loading
        {
            ((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(metadata,SaveRead);
        }

    }


}
//success Save
private void SaveUpdate(SavedGameRequestStatus reqStatus, ISavedGameMetadata metadata) {
    Debug.Log(reqStatus);
    if (reqStatus == SavedGameRequestStatus.Success)
    {
        message.text = ("Data Saved successfully ");
    }
    else {
        message.text = ("Data Saved failed " + reqStatus.ToString());
    }
}
//Load
private void SaveRead(SavedGameRequestStatus reqStatus, byte[] data) {
    if (reqStatus == SavedGameRequestStatus.Success) {
        string savedData = System.Text.ASCIIEncoding.ASCII.GetString(data);
        message.text = ("Data read successfully " + savedData);
        LoadSaveString(savedData);
    }
    else
    {
        message.text = ("Data read Failed!" + reqStatus.ToString());
    }
}

#endregion

}

1 个答案:

答案 0 :(得分:0)

使用JSON保留游戏数据,分割字符串很难维护。

{
    "currentLevel":"3",
    "totalCoin":"20",
    "levelData":[
    {
        "levelNum":1,
        "starEarn":3,
        "highScore":123456
    },
    {
        "levelNum":2,
        "starEarn":1,
        "highScore":1234
    }
    ]
}

//GameData.cs
[System.Serializable]
public class GameData {

    public int currentLevel;
    public int totalCoin;
    private Dictionary<int, LevelData> levelDataList;
    public GameData(){
        //
        // constructor
        //
    }
    public string GetSaveString(){
        string res = JsonUtility.ToJson (this);
        res = res.Substring (0, res.Length - 1);
        res = res + ",\"levelData\":[";
        foreach (LevelData levelData in levelDataList.Values) {
                res = res + JsonUtility.ToJson (levelData);
                res = res + ",";
            }
        if(levelDataList.Count > 0)
                res = res.Substring (0, res.Length - 1);
                res = res + "]}";
        return res;
    }
}

//LevelData.cs
    [System.Serializable]
    public class LevelData {

    public int levelNum;
    public int starEarn;
    public int highScore;
    public LevelData(){
        //
        // constructor
        //
    }
}