- Home /
Question by
Shinroku · Feb 23, 2018 at 03:22 PM ·
yieldsave dataintbinaryformatterpublic static
Need help yielding during AsyncOperations with my function being called as an int
here is my code :
pragma strict
mport UnityEngine; import System; import System.Collections.Generic; import System.Runtime.Serialization.Formatters.Binary; import System.IO;
public static class SaveLoadManager { private var scene : Scene;
public static function SavePlayer(playerinfo : PlayerInfo)
{
var bf : BinaryFormatter = new BinaryFormatter();
var stream : FileStream = new FileStream(Application.persistentDataPath + "/game.sav", FileMode.Create);
var data : PlayerData = new PlayerData(playerinfo);
var scene = SceneManager.GetActiveScene ();
data.sceneName = scene.name;
bf.Serialize(stream, data);
stream.Close();
}
public static function LoadPlayer() : int[]
{
if(File.Exists(Application.persistentDataPath + "/game.sav"))
{
var bf : BinaryFormatter = new BinaryFormatter();
var stream : FileStream = new FileStream(Application.persistentDataPath + "/game.sav", FileMode.Open);
var data : PlayerData = bf.Deserialize(stream) as PlayerData;
stream.Close();
SceneManager.LoadScene (data.sceneName, LoadSceneMode.Single);
var async : AsyncOperation = SceneManager.LoadSceneAsync (data.sceneName);
while (!async.isDone)
{
yield;
Debug.Log ("Loading");
}
return data.stats;
}
}
}
public class PlayerData { public var stats : int []; public var sceneName : String;
public function PlayerData(playerinfo : PlayerInfo)
{
stats = new int[5];
stats[0] = playerinfo.curHealth;
stats[1] = playerinfo.curSpecial;
stats[2] = playerinfo.playerPositionX;
stats[3] = playerinfo.playerPositionY;
stats[4] = playerinfo.playerPositionZ;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612153243im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
is it bad practice to have multiple serialized binary files for one save game? 1 Answer
Variable names are still visible when saving with binaryFormatter 1 Answer
Is this code handling fall back on previous saves upon failure the right way? 2 Answers
How to save list of Sprites? 1 Answer
Save multiple scriptableObjects in just one binary file 2 Answers