커스텀 직렬화
Unity 시리얼라이저에서 지원하지 않는 추가 항목을 직렬화하는 방법입니다.
읽는 시간 4분최근 업데이트: 9일 전
Unity의 시리얼라이저가 지원하지 않는 요소(예: C# Dictionary)를 직렬화하려는 경우 클래스에 ISerializationCallbackReceiver 인터페이스를 구현할 수 있습니다. 이를 통해 Unity Serialization 역직렬화 중에 주요 지점에서 호출하는 콜백을 구현할 수 있습니다.
Serialization 콜백을 사용하여 런타임 시점에 Serialization 시점과 달리 직렬화하기 어려운 데이터를 표현할 수 있습니다. 데이터를 Unity 직렬화하기 직전에 Unity 이해하는 데이터로 변환할 수 있습니다. Unity 데이터를 필드에 기록한 후 직렬화된 데이터를 런타임 시 원하는 형태로 다시 변환할 수 있습니다.
- 오브젝트가 직렬화될 때 Unity는 콜백을 호출합니다. 이 콜백에서 데이터를 Unity 이해하는 데이터로 변환할 수 있습니다. 예를 들어 C# 딕셔너리를 직렬화하려면 딕셔너리의 데이터를 키 배열과 값 배열로 복사합니다.
OnBeforeSerialize() - 콜백이 완료되면 Unity가 배열을 직렬화합니다.
OnBeforeSerialize() - 나중에 오브젝트가 역직렬화되면 Unity가 콜백을 호출합니다. 이 콜백에서는 데이터를 메모리에 있는 오브젝트에 편리한 형태로 다시 변환할 수 있습니다. 예를 들어 키와 값 배열을 사용하여 C# Dictionary를 다시 채웁니다.
OnAfterDeserialize()
예제 1: Unity 기본 Serialization 인해 성능 문제가 발생합니다.
트리 데이터 구조를 원한다고 가정해 보겠습니다. Unity 직접 데이터 구조를 직렬화하도록 허용하면 "null 대한 지원 없음" 제한으로 인해 데이터 스트림이 매우 커지고 많은 시스템에서 성능이 저하됩니다.
using UnityEngine;using System.Collections.Generic;using System;public class VerySlowBehaviourDoNotDoThis : MonoBehaviour { [직렬화 가능] 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: 커스텀 Serialization 성능 문제를 방지
이 경우 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. [직렬화 가능] 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.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 노드) { var serializedNode = serializedNodes [index]; // Transfer the deserialized data into the internal Node class Node newNode = new Node() { 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 (); }}