First, the summary
in Unity, the serialization of "things" is a very central thing of unity, and many features are built on a serialized system: Like a view panel, a prefab (prefabs), an instantiation (instantiation), a save scene, Load (loading), the thermal reload of the editor code (hotReloading of editor, Code), Unity's own garbage collector ( Resource.GarbageCollectSharedAssets()
). The serialization system is written in C + + for all internal object types (textures, animationclip, Camera, etc).
What are the requirements for a domain that needs to be serialized?
1, public, or private domain with [Serializefield] attribute
2, non-static;
3, the very quantity;
4, not read-only;
5. Other domain types that can be instantiated
Iii. types of domains that can be instantiated
1. Custom non-abstract class with attributes marked with "Serializable"
2, unity4.5 and after the custom structure, and with the "Serializable" tag attribute;
3. A reference to an object derived from Unityengin.object.
4. Basic data type
5. The element field type is an array that can be serialized
6, the element domain type is the list<t> that can be serialized
Iv. issues needing attention.
1. The serialization system does not support the null value of the custom class. If a class has a field in its definition that is marked with the "seriazable" attribute, then it will be added to the null value when serialized, and the new instance needs a new instance, so that it enters a dead loop, in order to limit, Unity supports up to 7 layers. To solve this problem, you can convert this class to serialize a struct that holds the primary members of the class. Let classes that need to serialize such members inherit from the Iserializationcallbackreceiver interface and implement two of its methods. You can refer to the Unity Manual for a good understanding.
2 . The serialization system is not good for polymorphic support. If there is an array, such as an animal array, which holds three instances of subclasses of the inherited automaton class, such as rabbits, dogs, cats,
Then an instance of three animals will be saved in the array after instantiation.
Unity serialization System Summary-From the Unity Handbook (Script serialization)