Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class LevelController : MonoBehaviour
- {
- private int curScene;
- public ObstacleController obstacleController;
- public TextAsset level1;
- public TextAsset level2;
- public int levelIndex = 0;
- public List<TextAsset> JsonList = new List<TextAsset>();
- void Start()
- {
- Debug.Log(Application.persistentDataPath + "/save.dat");
- LoadFile();
- SaveFile();
- }
- public void SaveFile()
- {
- string destination = Application.persistentDataPath + "/save.dat";
- FileStream file;
- if (File.Exists(destination)) file = File.OpenWrite(destination);
- else file = File.Create(destination);
- GameData data = new GameData(levelIndex);
- BinaryFormatter bf = new BinaryFormatter();
- bf.Serialize(file, data);
- file.Close();
- }
- public void LoadFile()
- {
- string destination = Application.persistentDataPath + "/save.dat";
- FileStream file;
- if (File.Exists(destination)) file = File.OpenRead(destination);
- else
- {
- Debug.LogError("File not found");
- return;
- }
- BinaryFormatter bf = new BinaryFormatter();
- GameData data = bf.Deserialize(file) as GameData;
- levelIndex = data.curLevel;
- file.Close();
- Debug.Log(data.curLevel);
- }
- public void CreateJsonList()
- {
- JsonList.Add(level1);
- JsonList.Add(level2);
- }
- public void NextLevel()
- {
- if(levelIndex+1 < JsonList.Count)
- {
- ++levelIndex;
- SaveFile();
- SceneManager.LoadScene(SceneManager.GetActiveScene().name);
- obstacleController.JSON = JsonList[levelIndex];
- }
- }
- public void RestartLevel()
- {
- SceneManager.LoadScene(SceneManager.GetActiveScene().name);
- obstacleController.JSON = JsonList[levelIndex];
- }
- public void MainMenu()
- {
- SceneManager.LoadScene(0);
- }
- }
Add Comment
Please, Sign In to add comment