如何制作在Unity3d中保存变量数据的脚本

时间:2018-10-03 19:14:07

标签: unity3d compiler-errors save

我正在尝试通过以下视频制作脚本来保存得分变量数据:https://www.youtube.com/watch?time_continue=264&v=J6FfcJpbPXE,但是在“类PlayerData”和其他位置使用“ PlayerData”和“打开”时,我总是遇到错误“还有另一种保存1个变量的方法吗?请帮助我两个星期以来一直在寻找解决方案,而且一切都太糟糕了。

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

public class scoreCount : MonoBehaviour
{

    public Text scoreText;
    public int score;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

        scoreText.text = "Points: " + score;

        if (Input.GetMouseButtonDown(0))
        {
            score++;
        }
    }

    public void Save()
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create(Application.persistentDataPath + "/palyerInfo.dat", FileMode.Open);

        PalyerData data = new PalyerData();

        data.score = score;

        bf.Serialize(file, data);
        file.Close();
    }

    public void Load()
    {
        if (File.Exists(Application.persistentDataPath + "/playerInfo.dat"));
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat");
            PalyerData data = (PlayeData)bf.Deserialize(file);
            file.Close();

            score = data.score;
        }
    }
}

[Serializable]
internal class PlayeData
{
    public int score;

}

0 个答案:

没有答案