简体   繁体   中英

Saving/loading Unity3d

I'm trying to save the game everytime the player reaches a portal and then when the game is stopped and when player wants to play again he can press the continue button on the startmenu to start at the level he last unlocked.

here is the tutorial: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

here is one of the scripts on the portal that should save the game:

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

gives no errors.

here is the script for the continue button:

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

no errors

and here is the script based on the tutorial:

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

Edited!: these are the current scripts, still failing to load the last unlocked level, keeps loading level 1 and if i remove the if statement from continuescript leaving this:

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.

i started using GameControl.control.x because in the tutorial this worked to load, save or add to variables. the only difference is that everything there is done with guibuttons. control is static so GameControl.control.levelcount +=1; should work right?

When you load your data you deserialize from the file and the overwrite the newly deserialized data with old variables.

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

Simply change the order so you move the values to your variables instead.

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

I know this is a bit old post, but I think you need to change this part:

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

    DontDestroyOnLoad(gameObject);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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