简体   繁体   English

无法保存游戏

[英]Unable to save game

I'm try a few days to learn how to keep the data in the game我尝试了几天来学习如何将数据保留在游戏中

I want to keep a number for learn, but I do not understand why I can not我想保留一个号码以供学习,但我不明白为什么我不能

[System.Serializable]
public class Save 
{
    public int Cuens;
    void Update () 
    {
        if (Input.GetKey (KeyCode.T)) {
            Cuens += 5;
            SaveGame ();
        }
        if (Input.GetKey (KeyCode.Y))
            LodeGame ();
    }
    void SaveGame()
    {
        BinaryFormatter bf = new BinaryFormatter ();
        FileStream file = File.Create (Application.persistentDataPath + "/gamesave.save");
        Game save = new Game (Cuens);
        bf.Serialize (file, save);
        file.Close ();
        Debug.Log (save);
    }
    void LodeGame()
    {
        BinaryFormatter bf = new BinaryFormatter ();
        FileStream file = File.Open (Application.persistentDataPath + "/gamesave.save", FileMode.Open);
        Game save = (Game)bf.Deserialize (file);
        file.Close ();
        Debug.Log (save);
    }
}

When you load the game state from file you create a new Game object but never do anything with it:当您从文件加载游戏状态时,您会创建一个新的 Game 对象,但从不对其进行任何操作:

 void LodeGame()
{
    BinaryFormatter bf = new BinaryFormatter ();
    FileStream file = File.Open (Application.persistentDataPath + "/gamesave.save", FileMode.Open);
    Game save = (Game)bf.Deserialize (file);
    // you never do something with this
    file.Close ();
    Debug.Log (save);
}

A simple approach could be to write back the loaded data into your current Game object:一种简单的方法是将加载的数据写回到当前的Game对象中:

 void LodeGame()
{
    BinaryFormatter bf = new BinaryFormatter ();
    FileStream file = File.Open (Application.persistentDataPath + "/gamesave.save", FileMode.Open);
    Game save = (Game)bf.Deserialize (file);
    file.Close ();
    this.Cuens = save.Cuens; // restore value.
    Debug.Log (save);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM