簡體   English   中英

當級別結束時,如何將級別的分數添加到該級別之外的貨幣?

[英]How can I add a score from a level to a currency outside of said level when the level ends?

我有一個名為“Stash”的 int 變量。 當我通關時,游戲將保存。 當它保存時,無論我的分數是那個水平,我都希望 Stash 上升。 目前,我的代碼沒有保存我的 stash 值,它只是將它設置為“0(stash 默認)+ Score(無論我的分數是那個級別)”。 我如何在此代碼中編寫以實現我的目標?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

[RequireComponent(typeof(GameData))]
public class SaveScript : MonoBehaviour
{
    private GameData gameData;
    private string savePath;

    void Start()
    {
        gameData = GetComponent<GameData>();
        savePath = Application.persistentDataPath + "/gamesave.save";
    }

    public void SaveData()
    {
        var save = new Save()
        {
            SavedStash = gameData.Stash + gameData.Score      //<------ (This code)
        };

        var binaryFormatter = new BinaryFormatter();
        using (var fileStream = File.Create(savePath))
        {
            binaryFormatter.Serialize(fileStream, save);
        }

        Debug.Log("Data Saved");
    }

    public void LoadData()
    {
        if (File.Exists(savePath))
        {
            Save save;

            var binaryFormatter = new BinaryFormatter();
            using (var fileStream = File.Open(savePath, FileMode.Open))
            {
                save = (Save)binaryFormatter.Deserialize(fileStream);
            }
            gameData.Stash = save.SavedStash;              //<---------- (and this code)
            gameData.ShowData();

            Debug.Log("Data Loaded");
        }
        else
        {
            Debug.LogWarning("Save file doesn't exist");
        }
    }
}

我的 GameData 文件中還有相關內容:

    public int Stash { get; set; }
    public int StashNew { get; set; }
    public int Score { get; set; }

StashNew 只是我必須讓整個事情工作的一個想法。

首先:永遠不要簡單地使用+ "/"作為系統文件路徑!

不同的目標設備可能具有具有不同路徑分隔符的不同文件系統。

而是使用Path.Combine ,它會根據運行的操作系統自動插入正確的路徑分隔符。

savePath = Path.combine(Application.persistentDataPath, "gamesave.save");

然后是主要問題:

屬性如

public int Stash { get; set; }
public int StashNew { get; set; }
public int Score { get; set; }

不會BinaryFormatter /序列化

因此當你做

binaryFormatter.Serialize(fileStream, save);

數字甚至沒有寫入輸出文件!

然后,當讀取相同的內容不起作用並且您的屬性只是保持int默認值0


只需刪除這些{ get; set; } { get; set; } { get; set; }為了你的財產轉化為可序列化字段,你應該罰款。

public int Stash;
public int StashNew;
public int Score;

僅用於演示我使用此代碼

public class MonoBehaviourForTests : MonoBehaviour
{
    [SerializeField] private GameData In;
    [SerializeField] private GameData Out;

    [ContextMenu("Test")]
    private void Test()
    {
        var formatter = new BinaryFormatter();

        using (var fs = File.Open(Path.Combine(Application.streamingAssetsPath, "Example.txt"), FileMode.OpenOrCreate, FileAccess.Write))
        {
            formatter.Serialize(fs, In);
        }

        using (var fs = File.Open(Path.Combine(Application.streamingAssetsPath, "Example.txt"), FileMode.Open, FileAccess.Read))
        {
            Out = (GameData)formatter.Deserialize(fs);
        }
    }

    [Serializable]
    private class GameData
    {
        public int Stash;
        public int StashNew;
        public int Score;
    }
}

正如您所看到的,這也有副作用,即現在這些字段實際上出現在 Inspector 中並且可以進行編輯。 我只將值寫入In實例,在點擊Test后,您可以看到它們通過文件加載到Out實例中。

在此處輸入圖片說明


但是總的來說,我根本不建議在這里使用BinaryFormatter ......它增加了很多你根本不需要的開銷垃圾。 只需選擇JSON或類似的簡單文件格式,如 XML 或 CSV,它們只存儲相關數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM