- Home /
what does [Serializable] mean in C Sharp
I realize this is probably a stupid question and the answer in online, but from the descriptions im finding i just dont get it
unitys documnetation says "The Serializable attribute lets you embed a class with sub properties in the inspector."
what subproperties is this talking about and why would i need to use [Serializable] in a script?
Answer by Michael-Glen-Montague · May 21, 2015 at 10:36 PM
[Serializable]is an attribute added above a class name to allow it and its fields to be broken down into abyte[]array (binary data) and passed around easily. A common use for it is to serialize a class filled with data about your current game session to a save file, and then later you can deserialize the binarybyte[]data out of that save file as anobject. You then cast the type of thatobjectback to the type of whatever class it was before.[HideInInspector]is an attribute added above apublicfield to hide it from Unity's inspector. Whatever value you had set it to before you hid it will be saved because it's still serialized.[NonSerialized]is added above apublicfield to disable serialization on it, meaning it won't be visible in Unity's inspector and its value won't be saved for the next time you open the project. This is best used when the field's value is set during run-time rather than in the editor.[SerializeField]is added above aprivatefield to allow serialization on it, thus making it visible in Unity's inspector and allowing its data to be saved.
Classes with the [Serialized] attribute are also visible in the inspector, meaning you can edit their values and Unity will serialize (save) it for the next time you open the project.
Answer by tanoshimi · May 21, 2015 at 09:55 PM
You'd use it to keep a class member private, but still expose it so its initial value could be set via the inspector.
Answer by DoTA_KAMIKADzE · May 21, 2015 at 10:12 PM
Basically this is sort of "defined way to save state of your object(variables, etc.) so that they later can be read and set to original state", think of it like a mark that allow writing all of your object values into file,DB,RAM,stream,etc. which can basically be used in tons of different ways, but all comes to the same purpose - recreate your object exactly as it was or part of it which you need, you can even make your own Serialization/Deserialization with which you decide how and what. If that makes no sense to you then you can always get in touch of what it is like for example THERE.
P.S. Ah yeah also forgot to answer the last part of your question:
1) Something like a class that contains few variables.
2) Why do you need to use? You don't. You can if you want to see it in inspector and/or save its state.
Didnt really understand the first half of what you said, but everything after ps was very helpful thank you
Answer by UnityDeveloper2 · Mar 16, 2018 at 03:18 AM
You can use [Serializable] aka [System.Serializable] on certain classes to expose object behavior in the editor that would otherwise need to be inherited and hard-coded.
An example is UnityEvent (see https://docs.unity3d.com/Manual/UnityEvents.html "Generic UnityEvents"), which uses Serializable to allow the user to add an arbitrary number of function callbacks in the event using the Inspector.
Your answer