当您想要序列化 Unity 的序列化器不支持的内容(例如,C# Dictionary)时,您可以在您的类中实现 ISerializationCallbackReceiver 接口。这允许您实现回调,Unity 会在序列化和反序列化的关键点调用这些回调。
您可以使用序列化回调在运行时为难以序列化的数据提供与序列化时不同的表示形式。您可以在 Unity 序列化数据之前将其转换为 Unity 理解的内容。在 Unity 将数据写入您的字段后,您可以将序列化后的数据转换回您希望它在运行时具有的形式。
OnBeforeSerialize()
回调。在此回调中,您可以将数据转换为 Unity 理解的内容。例如,要序列化 C# Dictionary,请将数据从 Dictionary 复制到键数组和值数组中。OnBeforeSerialize()
回调完成后,Unity 会序列化这些数组。OnAfterDeserialize()
回调。在此回调中,您可以将数据转换回对内存中的对象方便的形式。例如,使用键和值数组重新填充 C# Dictionary。假设您想要一个树形数据结构。如果您让 Unity 直接序列化数据结构,则“不支持空值”的限制会导致您的数据流变得非常大,从而导致许多系统中的性能下降。
using UnityEngine;
using System.Collections.Generic;
using System;
public class VerySlowBehaviourDoNotDoThis : MonoBehaviour {
[Serializable]
public class Node {
public string interestingValue = "value";
//The field below is what makes the serialization data become huge because
//it introduces a 'class cycle'.
public List<Node> children = new List<Node>();
}
//this gets serialized
public Node root = new Node();
void OnGUI() {
Display (root);
}
void Display(Node node) {
GUILayout.Label ("Value: ");
node.interestingValue = GUILayout.TextField(node.interestingValue, GUILayout.Width(200));
GUILayout.BeginHorizontal ();
GUILayout.Space (20);
GUILayout.BeginVertical ();
foreach (var child in node.children) {
Display (child);
}
if (GUILayout.Button ("Add child")) {
node.children.Add (new Node ());
}
GUILayout.EndVertical ();
GUILayout.EndHorizontal ();
}
}
在这种情况下,您告诉 Unity 不要直接序列化树,并创建一个单独的字段以使用适合 Unity 序列化器的序列化格式存储树。
using System.Collections.Generic;
using System;
public class BehaviourWithTree : MonoBehaviour, ISerializationCallbackReceiver {
// Node class that is used at runtime.
// This is internal to the BehaviourWithTree class and is not serialized.
public class Node {
public string interestingValue = "value";
public List<Node> children = new List<Node>();
}
// Node class that we will use for serialization.
[Serializable]
public struct SerializableNode {
public string interestingValue;
public int childCount;
public int indexOfFirstChild;
}
// The root node used for runtime tree representation. Not serialized.
Node root = new Node();
// This is the field we give Unity to serialize.
public List<SerializableNode> serializedNodes;
public void OnBeforeSerialize() {
// Unity is about to read the serializedNodes field's contents.
// The correct data must now be written into that field "just in time".
if (serializedNodes == null) serializedNodes = new List<SerializableNode>();
if (root == null) root = new Node ();
serializedNodes.Clear();
AddNodeToSerializedNodes(root);
// Now Unity is free to serialize this field, and we should get back the expected
// data when it is deserialized later.
}
void AddNodeToSerializedNodes(Node n) {
var serializedNode = new SerializableNode () {
interestingValue = n.interestingValue,
childCount = n.children.Count,
indexOfFirstChild = serializedNodes.Count+1
}
;
serializedNodes.Add (serializedNode);
foreach (var child in n.children) {
AddNodeToSerializedNodes (child);
}
}
public void OnAfterDeserialize() {
//Unity has just written new data into the serializedNodes field.
//let's populate our actual runtime data with those new values.
if (serializedNodes.Count > 0) {
ReadNodeFromSerializedNodes (0, out root);
} else
root = new Node ();
}
int ReadNodeFromSerializedNodes(int index, out Node node) {
var serializedNode = serializedNodes [index];
// Transfer the deserialized data into the internal Node class
Node newNode = new Node() {
interestingValue = serializedNode.interestingValue,
children = new List<Node> ()
}
;
// The tree needs to be read in depth-first, since that's how we wrote it out.
for (int i = 0; i != serializedNode.childCount; i++) {
Node childNode;
index = ReadNodeFromSerializedNodes (++index, out childNode);
newNode.children.Add (childNode);
}
node = newNode;
return index;
}
// This OnGUI draws out the node tree in the Game View, with buttons to add new nodes as children.
void OnGUI() {
if (root != null) {
Display (root);
}
}
void Display(Node node) {
GUILayout.Label ("Value: ");
// Allow modification of the node's "interesting value".
node.interestingValue = GUILayout.TextField(node.interestingValue, GUILayout.Width(200));
GUILayout.BeginHorizontal ();
GUILayout.Space (20);
GUILayout.BeginVertical ();
foreach (var child in node.children) {
Display (child);
}
if (GUILayout.Button ("Add child")) {
node.children.Add (new Node ());
}
GUILayout.EndVertical ();
GUILayout.EndHorizontal ();
}
}