- Home /
Why do I need to serialize a struct inside a ScriptableObject to save its data?
Let's say I create an instance of this ScriptableObject
public class MyObject : ScriptableObject
{
public int myInt;
public Foos foos;
[System.Serializable]
public struct Foos
{
public int value;
}
}
If I don't put [System.Serializable] for Foos, changing the values of foos.value will not be saved inside the ScriptableObject when I Play/Pause the game, but myInt will be saved.
Why do I need to Serialize the struct but not the int? Is it the good way to do it or is it bad for performance or anything else?
Thanks!
Answer by ataboo · Sep 08, 2021 at 03:16 AM
It sounds like ScriptableObjects are only interactive or change in the editor, but the end product is just the static serialized data that can be referenced by prefabs. So anything that isn't serialized is likely not included.
Here's the reference code for determining if a field should be serialized:
Integer is likely caught by the typeReference.isValueType
, Unity classes like Vector2 by IsReferenceTypeSerializable
. HasSerializeReferenceAttribute
checks for the Serializable attribute in cases like yours.
I can't see the struct having a big performance impact but you can always test it out and profile to see the difference.
Your answer
Follow this Question
Related Questions
Make the same serialized class as a ScriptableObject? 1 Answer
ScriptableObject with Custom Editor resetting data in inspector 1 Answer
Serializing ScriptableObject into scene without writing to asset 1 Answer
Is it possible to serialize and store custom structs on the iPhone in scriptableObjects? 2 Answers