保存/加载Unity3d

时间:2014-09-09 13:49:39

标签: c# unity3d

我试图在每次玩家到达门户时保存游戏,然后当游戏停止并且玩家想要再次玩游戏时,他可以按下startmenu上的继续按钮以从他上次解锁的级别开始

这是教程: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

这是门户网站上应该保存游戏的一个脚本:

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

public class Saving : MonoBehaviour
{

    void OnTriggerEnter( Collider other)
    {
        if(other.gameObject.tag == "Player")
        {
            GameControl.control.levelcount += 1;
            GameControl.control.Save();
        }
    }
}

没有错误。

这是继续按钮的脚本:

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

public class ContinueCS : MonoBehaviour {

    void OnMouseUp(){
        GameControl.control.Load();
        int levelcount =  GameControl.control.levelcount;
        if(levelcount == 0)
        {
            Application.LoadLevel("Level1");
        }
        else
        Application.LoadLevel(levelcount);
    }
}

没有错误

这是基于教程的脚本:

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

     public class GameControl : MonoBehaviour {
    public static GameControl control;

    public float score;
    public int levelcount;

    void Awake () {
        if(control == null)
        {
            DontDestroyOnLoad(gameObject);
            control = this;
        }
        else if(control != this)
        {
            Destroy(gameObject);
        }
    }

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

        PlayerData data = new PlayerData();
        data.score = score;
        data.levelcount = levelcount;

        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", FileMode.Open);
            PlayerData data = (PlayerData)bf.Deserialize(file);
            file.Close();

            score = data.score;
            levelcount = data.levelcount;

        }
    }
}
[Serializable]
class PlayerData
{
    public float score;
    public int levelcount;
}

已编辑!:这些是当前脚本,仍然无法加载上一个解锁级别,继续加载级别1,如果我从continuecript中删除if语句,则离开此:

void OnMouseUp(){
        GameControl.control.Load();
        int levelcount =  GameControl.control.levelcount;
        Application.LoadLevel(levelcount);


it simply does nothing so i'm guessing the levelcount just isn't adding up.

我开始使用GameControl.control.x,因为在本教程中,这可以加载,保存或添加到变量。唯一的区别是那里的一切都是用guibutton完成的。控制是静态的,所以GameControl.control.levelcount + = 1;应该工作正常吗?

2 个答案:

答案 0 :(得分:2)

加载数据时,从文件反序列化并用旧变量覆盖新反序列化的数据。

if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
    PlayerData data = (PlayerData)bf.Deserialize(file);
    file.Close();

    //These lines overwrites the loaded data
    data.score = score;
    data.levelcount = levelcount;
}

只需更改顺序,即可将值移动到变量中。

if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
    PlayerData data = (PlayerData)bf.Deserialize(file);
    file.Close();

    score = data.score;
    levelcount= data.levelcount;
}

答案 1 :(得分:0)

我知道这是一个过时的帖子,但是我认为您需要更改此部分:

void Awake () {
    if(control == null)
    {
        control = this;
    }
    else if(control != this)
    {
        Destroy(gameObject);
    }

    DontDestroyOnLoad(gameObject);
}