简体   繁体   中英

Detect when Binary Formatter finishes serializing

I have a "Save And Quit" Button in my Game. I Made it and it saves the game as intended. Everything works fine, HOWEVER if i then want to close the game, I need to know when the serialization finished because if i close the game to early its not saved :(. I await some good answers :). Thanks in advance, Here's the code (that works!):

BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/Gmaster.fun";

FileStream stream = new FileStream(path, FileMode.Create);

PlayerData data = new PlayerData();

Formatter.Serialize(stream, data);
stream.Close();

Create an Event and invoke it from the class:

public event System.Action OnSerializeDone;

somewhere else:

thatClass.OnSerializeDone += MyClass_SerializeDone;

at the end of your code:

OnSerializeDone?.Invoke();

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