CustomSaveData
Last updated
Last updated
The CustomSaveData
class is a data container used for serializing and deserializing save data for game objects. It provides mechanisms for saving the relevant fields of an object to a JSON format and loading the saved data back into the object. This class supports saving data like the scene name, position, and the type of object, along with a dictionary of saved fields that contain other properties of the object. It uses the Newtonsoft.Json
library for JSON serialization and deserialization.
The class also defines methods for saving and loading fields based on the [SaveField]
attribute and supports custom handling for non-serializable types like Item_SO
objects.
SceneName
(string
):
The name of the scene where the object is located. This is stored to filter out objects that do not belong to the current scene during loading.
Attributes: [JsonProperty]
(Serialized as part of JSON).
Position
(SerializableVector3
):
The position of the object in the scene. This will be serialized into the JSON file if it is provided. If not provided, the property is ignored during serialization.
Attributes: [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
(Only serialized if the position is not null
).
Type
(string
):
The name of the object's type. This is useful for associating the object with its prefab or type information when reloading. For more information, refer to & .
savedFields
(Dictionary<string, object>
):
A dictionary that stores the saved fields of the object. The keys are the field names, and the values are the field values to be serialized. This Dictionary is populated based on the variables with the attribute [SaveField]
within the class.
Expanding CustomSaveData is usually unnecessary, as it, along with the SaveField attribute, can store all serializable fields without additional coding.
However, implementing custom data processing can be beneficial if you need to save non-serializable data types.
In such cases,.