public void LoadSelectedScene(string playerName) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + "/Saves" + playerName + ".dat", FileMode.Open); CubeListData data = (CubeListData)bf.Deserialize(file); file.Close(); int index = SceneUtility.GetBuildIndexByScenePath("Assets/_scenes/" + data.sceneName + ".unity"); flagScene.GetComponent <DataLoad> ().playerName = data.playerName; DontDestroyOnLoad(flagScene); SceneManager.LoadScene(index, LoadSceneMode.Single); // need to load the scene before finding it Scene sceneToLoad = SceneManager.GetSceneByBuildIndex(index); Debug.Log(flagScene.name); SceneManager.MoveGameObjectToScene(flagScene, sceneToLoad); }
public void LoadData() { if (File.Exists(Application.persistentDataPath + "/Saves/" + flagScene.GetComponent <DataLoad>().playerName + ".dat")) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + "/Saves" + "/Data.dat", FileMode.Open); CubeListData data = (CubeListData)bf.Deserialize(file); file.Close(); //SceneManager.LoadScene (data.sceneName, LoadSceneMode.Single); cubes = GameObject.FindGameObjectsWithTag("Placed Cube"); foreach (GameObject cube in cubes) { Destroy(cube); } foreach (CubeData cube in data.cubes) { m_wallController.createCube(new Vector3(cube.posx, cube.posy, cube.posz), new Vector4(cube.r, cube.g, cube.b, cube.a)); } } }
public void SaveData() { if (!Directory.Exists(Application.persistentDataPath + "/Saves")) { Directory.CreateDirectory(Application.persistentDataPath + "/Saves"); } BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Create(Application.persistentDataPath + "/Saves/" + flagScene.GetComponent <DataLoad>().playerName + ".dat"); CubeListData data = new CubeListData(); cubes = GameObject.FindGameObjectsWithTag("Placed Cube"); data.cubes = new CubeData[cubes.Length]; i = 0; foreach (GameObject cube in cubes) { CubeData cubeData = new CubeData(); cubeData.posx = cube.transform.position.x; cubeData.posy = cube.transform.position.y; cubeData.posz = cube.transform.position.z; cubeData.r = cube.GetComponent <MeshRenderer> ().material.color.r; cubeData.g = cube.GetComponent <MeshRenderer> ().material.color.g; cubeData.b = cube.GetComponent <MeshRenderer> ().material.color.b; cubeData.a = cube.GetComponent <MeshRenderer> ().material.color.a; Debug.Log("CubeData : " + cubeData.posx); data.cubes[i] = cubeData; i++; } scene = SceneManager.GetActiveScene(); data.sceneName = scene.name; data.playerName = flagScene.GetComponent <DataLoad> ().playerName; Debug.Log("Data : " + data); bf.Serialize(file, data); file.Close(); }
public void Load() // DOESNT WORK !!! //Scene sceneMain = SceneManager.GetSceneByName ("main"); //Debug.Log ("SCENE : " + sceneMain.name); //SceneManager.MoveGameObjectToScene (flagScene, sceneMain); //SceneManager.LoadScene ("main"); // load every scene.unity in _scenes folder --> need a scrollbar //LoadData(); { int i = 0; Debug.Log(Application.persistentDataPath + "/Saves"); if (Directory.Exists(Application.persistentDataPath + "/Saves")) { files = Directory.GetFiles(Application.persistentDataPath + "/Saves"); scrollView.SetActive(true); foreach (string f in files) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(f, FileMode.Open); CubeListData data = (CubeListData)bf.Deserialize(file); file.Close(); Instantiate(itemButton); itemButton.GetComponentInChildren <Text> ().text = data.playerName; Debug.Log(i++); // afficher list button for each save //scrollView. // when click on one, load scene // que si save choisie //Debug.Log(data.playerName); // list file name text on button to click to load the good scene } } }