保存/加载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; 应该工作正常吗?

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

 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; }