Keep in mind this is an Older version of .net 2.0 - 2.5 because i'm using Unity 5.6.2f1
[System.Serializable]
public class SaveManager {
[OptionalField]
public float version = 1.5f;
[OptionalField]
public string versionString = "1.5";
}
When using the above class to save data to a text file by using BinaryFormatter, these values will be set to defaults. version will be 0 versionString will be ""
Is there any way to have nonexisting optional fields have default values? I have been googling this issue for a while but I haven't found any useful information.
To clarify, this issue is reproduced by Serializing SaveManager class with no fields inside of it, saving the data to a txt file. Then update the SaveManager class to include the OptionalField float version, and then trying to read the saved data, and then deserialize it back into SaveManager.
[System.Serializable]
public class SaveManager {
[OnDeserializing()]
internal void OnDeserializingMethod(StreamingContext context) {
version = 1.5f;
resolutionSetting = 1;
notificationTimer = 5 * 60;
hideNameTagTimer = 0 * 60;
storedLevelName = "little_transparent";
fartVolume = 0f;
fartToggle = true;
nametagsCollideWithCharacters = true;
}
[OptionalField]
public float version = 1.5f;
Fixed by using OnDeserializing() Thanks for everyone that tried to help me!
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.