ISerializationCallbackReceiver
Interface for receiving callbacks before serialization and after deserialization, to process datatypes that can't otherwise be serialized or deserialized.
阅读时间3 分钟最后更新于 14 天前
Definition
- Type: Interface
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
public interface ISerializationCallbackReceiver
Remarks
The Unity serializer can automatically serialize most data types, but not all of them. In cases where a data type can't be serialized, you can use the serialization callbacks defined in this interface to manually process the data into a serializable form. For more information on when and why manual processing is necessary, refer to Custom serialization.
Unity invokes just before an object is serialized. Inside this callback, you can transform your data into something Unity understands.
OnBeforeSerializeUnity invokes after an object is deserialized. After Unity has written the data to your fields, use this callback to transform the deserialized data back into the form you want it to have at runtime.
OnAfterDeserializeWork performed in these callbacks must be done with care, as the Unity serializer runs on a different thread from most of the Unity API. It's recommended to process only fields that are directly owned by the object, to keep the processing burden as low as possible.
This interface supports serialization as reference, such as on objects decorated with the SerializeReference attribute. The order of callback execution between these objects isn't guaranteed. However, the following execution orders are guaranteed:
- is called on the host object before it's called on any of the host object's managed references.
OnBeforeSerialize - is called on the host object after it's called on all of the host object's managed references.
OnAfterDeserialize
For a full explanation of the attribute's behaviour, refer to SerializeReference. For more information on when and how Unity performs serialization, refer to Script serialization in the Manual.
SerializeReferenceThe following example uses a collection to store a deck of cards at runtime. The Unity serializer doesn't support , so the example copies the cards into a field in , and rebuilds the stack from the list in .
Stack<T>Stack<T>List<T>OnBeforeSerializeOnAfterDeserializeusing System.Collections.Generic;using UnityEngine;public class CardDeck : MonoBehaviour, ISerializationCallbackReceiver{ // Unity serializes this list. The first element is the top card of the deck. [SerializeField] private List<string> cards = new List<string> { "Ace of Spades", "King of Hearts", "Queen of Clubs" }; // The deck that the script uses at runtime. Unity doesn't serialize Stack<T> fields. private Stack<string> deck; public void OnBeforeSerialize() { // Unity can call OnBeforeSerialize before OnAfterDeserialize, for example on a newly added component. // In that case the stack isn't built yet, and the list already contains the cards. if (deck == null) return; // A stack enumerates its elements from top to bottom, so the top card becomes the first list element. cards.Clear(); cards.AddRange(deck); } public void OnAfterDeserialize() { // Push the cards from the last list element to the first, so that the first element ends up on top. deck = new Stack<string>(); for (int i = cards.Count - 1; i >= 0; i--) deck.Push(cards[i]); } void Start() { // Prints "Drew: Ace of Spades". // In Play mode, the Inspector then shows the two remaining cards, // because OnBeforeSerialize copies the deck back into the list. Debug.Log("Drew: " + deck.Pop()); }}
Additional Resources: SerializeReference, SerializeField.
Methods
Method | Description |
|---|---|
| OnAfterDeserialize | Implement this callback to transform data back into runtime data types after an object is deserialized. |
| OnBeforeSerialize | Implement this callback to transform data into serializable data types immediately before an object is serialized. |