自定义序列化
如何 Serialize Unity 序列化程序不支持的其他项。
阅读时间5 分钟最后更新于 5 天前
当您希望 Serialize Unity 的 Serializer 不支持的内容(例如,C# Dictionary)时,可以在类中实现 ISerializationCallbackReceiver 接口。这允许您实现 Unity 在 Serialization 和反序列化期间在 Key 点调用的回调。
您可以使用 Serialization 回调在运行时为难以序列化的数据提供与 Serialization 时间不同的表示形式。您可以在 Unity 序列化数据之前将数据变换为 Unity 理解的内容。Unity 将数据写入字段后,可以将序列化的数据变换回运行时时所需的形式。
- 当对象即将序列化时,Unity 会调用 回调。在此回调中,您可以将数据变换为 Unity 理解的内容。例如,Serialize C# 字典,请将字典中的数据复制到密钥Array和值数Array。
OnBeforeSerialize() - 回调完成后,Unity 会序列化数组。
OnBeforeSerialize() - 稍后,当对象反序列化时,Unity 会调用 回调。在此回调中,可以将数据变换回对内存中的Object方便的形式。例如,使用Key和值组重新填充 C# Dictionary。
OnAfterDeserialize()
示例 1:Unity 默认Serializationrialization会导致性性能问题
假设您希望拥有树数据结构。如果让 Unity 直接Serialize数据结构,“不Support null”限制将导致数据流变得非常大,从而导致许多系统的性能下降:
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 (); }}
示例 2:自定义序列化可避免性性能问题
在这种情况下,您告诉 Unity 不要直接Se树alize树,并make一个单独的Field来以适合 Unity 序列化程序序列Format式存储树:
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.未序列化。 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.数量+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 (); } 整型 ReadNod整型 索引 out 节点 节点 { } var serializedNode = serializedNodes [index]; // Transfer the deserialized data into the internal Node class 节点 newNode = 新建节点() { interestingValue = serializedNode.interestingValue, 子对象 = 新建列表<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 (); }}