Question by
Jeff-Kesselman · May 06, 2016 at 05:05 PM ·
serializationtestingserializedpropertyserializedobject
How to test an objects Unity serializability?
I am trying to write a test harness to test an custom class''s Serializability with the Unity Serializer. My current solution is to make it a field on a parent ScriptableObject, serialize that ScriptableObject, and then attempt to read it back as a property off the SerializedObject.
Its all working except the last part, when I try to read it back I get null. I've examined the property and it seems to have null fro its object value, but says it has chidren (which I've been unable to drill down into)>
Here are the classes:
Class being tested:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class SerializableTypeReference : UnityEngine.Object{
[SerializeField]
private string _name;
private System.Type _type=null;
public SerializableTypeReference()
{
}
public SerializableTypeReference(System.Type t)
{
_name = t.AssemblyQualifiedName;
_type = t;
}
public SerializableTypeReference(string name)
{
_name = name;
_type = null;
}
public System.Type Type
{
get
{
if (_type == null)
{
_type = System.Type.GetType(_name);
}
return _type;
}
}
}
ScriptableObject wrapper:
using UnityEngine;
using System.Collections;
public class SerializationTestClass : ScriptableObject
{
public SerializableTypeReference stype;
}
Test Code:
using UnityEngine;
using UnityEditor;
using NUnit.Framework;
[TestFixture]
public class SerializableTypesTest {
[Test]
public void TypeReferenceTest()
{
Assert.DoesNotThrow(delegate ()
{
System.Type t = typeof(SerializableTypesTest);
Assert.IsNotNull(t);
SerializationTestClass test = ScriptableObject.CreateInstance<SerializationTestClass>();
Assert.IsNotNull(test);
test.stype = new SerializableTypeReference(t);
Assert.IsNotNull(test.stype);
SerializedObject ser = (SerializedObject)new SerializedObject(test);
Assert.IsNotNull(ser);
SerializedProperty prop = ser.FindProperty("stype");
Assert.IsNotNull(prop);
SerializableTypeReference test2 = prop.objectReferenceValue as SerializableTypeReference;
Assert.IsNotNull(test2);
Assert.IsTrue(t == test2.Type);
});
}
}
Exception in test:
TypeReferenceTest [0.013s]
---
Expected: No Exception to be thrown
But was: ( Expected: not null
But was: null
)
at NUnit.Framework.Assert.That (System.Object actual, IResolveConstraint expression, System.String message, System.Object[] args) [0x00000] in <filename unknown>:0
at NUnit.Framework.Assert.IsNotNull (System.Object anObject) [0x00000] in <filename unknown>:0
at SerializableTypesTest.<TypeReferenceTest>m__2 () [0x00060] in C:\Users\jeffp\Plastic\UA\GGVRExp\Assets\Scripts\Utilities\Types\UnitySerializable\Editor\SerializableTypesTest.cs:24
at NUnit.Framework.Constraints.VoidInvocationDescriptor.Invoke () [0x00000] in <filename unknown>:0
at NUnit.Framework.Constraints.ExceptionInterceptor.Intercept (System.Object invocation) [0x00000] in <filename unknown>:0
---
at SerializableTypesTest.TypeReferenceTest () [0x00000] in C:\Users\jeffp\Plastic\UA\GGVRExp\Assets\Scripts\Utilities\Types\UnitySerializable\Editor\SerializableTypesTest.cs:11
Comment