Speichern/laden von Unity3d

Ich versuche, das Spiel zu speichern jedes mal wenn der Spieler erreicht ein portal und dann wenn das Spiel beendet ist, und wenn der Spieler will, um wieder zu spielen, er kann drücken Sie die weiter-Taste auf dem Startmenü zu starten, auf der Ebene, die er die letzten freigeschaltet.

hier ist das tutorial:
http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

hier ist eines der Skripte, die auf dem portal, sollte das Spiel speichern:

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

gibt keine Fehler.

hier ist das Skript für die Schaltfläche weiter:

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

keine Fehler

und hier ist das Skript basiert auf dem 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;
}

Bearbeitet!: dies sind die aktuellen Skripte, die noch nicht geladen das Letzte level freigeschaltet, hält den laden auf level 1 und wenn ich entfernen Sie die if-Anweisung aus continuescript verlassen:

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.

begann ich mit GameControl.Kontrolle.x, weil in dem tutorial, das hat funktioniert, zu laden, zu speichern oder fügen Sie Variablen zu. der einzige Unterschied ist, dass alles, was dort getan wird, mit guibuttons. Kontrolle ist statisch so GameControl.Kontrolle.levelcount +=1; sollte funktionieren, richtig?

InformationsquelleAutor Dane Gillis | 2014-09-09

Schreibe einen Kommentar