Skip to content

Commit f2a652a

Browse files
Fix SerializeReference on non serializable collections (#167)
1 parent 6c844f0 commit f2a652a

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

Editor/Utilities/TriUnitySerializationUtilities.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,27 @@ public static bool IsSerializableByUnity(FieldInfo fieldInfo)
2727

2828
if (fieldInfo.GetCustomAttribute<SerializeReference>() != null)
2929
{
30-
return true;
30+
// if it's a list or array, the base type should be serializable
31+
if (fieldInfo.FieldType.IsArray)
32+
{
33+
var type = fieldInfo.FieldType.GetElementType();
34+
if (type.IsSerializable || type.IsInterface)
35+
return true;
36+
else
37+
return false;
38+
}
39+
else if (fieldInfo.FieldType.IsGenericType && fieldInfo.FieldType.GetGenericTypeDefinition() == typeof(List<>))
40+
{
41+
var type = fieldInfo.FieldType.GenericTypeArguments[0];
42+
if (type.IsSerializable || type.IsInterface)
43+
return true;
44+
else
45+
return false;
46+
}
47+
else
48+
{
49+
return true;
50+
}
3151
}
3252

3353
if (fieldInfo.IsPublic || fieldInfo.GetCustomAttribute<SerializeField>() != null)

0 commit comments

Comments
 (0)