/
UnityObjectConverter.cs
104 lines (85 loc) · 4.16 KB
/
UnityObjectConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Collections.Generic;
using FullSerializer;
/// <summary>
/// A Unity object converter for Full Serializer: https://github.com/jacobdufault/fullserializer
/// This converter serialises a UnityObject (such as a MonoBehaviour or GameObject)
/// as an integer index into a list maintained by a parent class which handles the *actual* serialisation.
/// This allows us to leverage Unity's built-in serialisation of UnityObjects while also having our own
/// serialisation logic. The external list is accessed during de/serialisation via Serializer.Context.
/// This works with both scene references and asset references.
/// </summary>
public class UnityObjectConverter : fsConverter
{
// -------------------------------------------------------------------------------
public override bool CanProcess(Type type)
{
// This converter applies to any UnityEngine.Object or subclass e.g. MonoBehaviour, ScriptableObject
var unityObjectType = typeof(UnityEngine.Object);
return (type == unityObjectType) || (type.IsSubclassOf(unityObjectType));
}
// -------------------------------------------------------------------------------
public override bool RequestCycleSupport(Type storageType)
{
return false;
}
// -------------------------------------------------------------------------------
public override bool RequestInheritanceSupport(Type storageType)
{
return false;
}
// -------------------------------------------------------------------------------
public override fsResult TrySerialize(object instance, out fsData serialized, Type storageType)
{
// Grab the list of serialised Unity objects from the Context object
var serialisedUnityObjects = Serializer.Context.Get<List<UnityEngine.Object>>();
var unityObjectInstance = instance as UnityEngine.Object;
// If the Unity Object reference is null we don't need a valid index
// into the list of serialisedUnityObjects, so just store -1 and return
if (unityObjectInstance == null)
{
serialized = new fsData(-1);
return fsResult.Success;
}
// Otherwise, search our existing list of object references for a match
var serialisedUnityObjectsIndex = -1;
for (var i = 0; i < serialisedUnityObjects.Count; i++)
{
if (serialisedUnityObjects[i] == unityObjectInstance)
{
serialisedUnityObjectsIndex = i;
break;
}
}
// If this is a new object reference that we don't already have in our list
// add it now and serialize the new object's index in the list
if (serialisedUnityObjectsIndex < 0)
{
serialisedUnityObjectsIndex = serialisedUnityObjects.Count;
serialisedUnityObjects.Add(unityObjectInstance);
}
serialized = new fsData(serialisedUnityObjectsIndex);
return fsResult.Success;
}
// -------------------------------------------------------------------------------
public override fsResult TryDeserialize(fsData data, ref object instance, Type storageType)
{
// Grab the list of serialised Unity objects from the Context object
var serialisedUnityObjects = Serializer.Context.Get<List<UnityEngine.Object>>();
var serialisedUnityObjectsIndex = (int)data.AsInt64;
// Sanity check the index we've just deserialised against the Context list
if (serialisedUnityObjectsIndex < 0 || serialisedUnityObjectsIndex >= serialisedUnityObjects.Count)
{
return fsResult.Warn("Invalid Unity Object reference index, could not deserialise!");
}
instance = serialisedUnityObjects[serialisedUnityObjectsIndex];
return fsResult.Success;
}
// -------------------------------------------------------------------------------
public override object CreateInstance(fsData data, Type storageType)
{
// We never want the serialiser to create Unity objects, null references are fine!
return null;
}
// -------------------------------------------------------------------------------
}