Skip to content

Commit 5d71424

Browse files
committed
Fix IsTypeSerializable
1 parent 3908308 commit 5d71424

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

Editor/Utilities/TriUnitySerializationUtilities.cs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections;
32
using System.Collections.Generic;
43
using System.Reflection;
54
using UnityEngine;
@@ -9,6 +8,10 @@ namespace TriInspector.Utilities
98
{
109
internal static class TriUnitySerializationUtilities
1110
{
11+
private static readonly Assembly CoreLibAssembly = typeof(List<>).Assembly;
12+
private static readonly Assembly SystemCoreAssembly = typeof(HashSet<>).Assembly;
13+
private static readonly Assembly SystemAssembly = typeof(LinkedList<>).Assembly;
14+
1215
public static bool IsSerializableByUnity(FieldInfo fieldInfo)
1316
{
1417
if (fieldInfo.GetCustomAttribute<NonSerializedAttribute>() != null ||
@@ -32,16 +35,20 @@ public static bool IsSerializableByUnity(FieldInfo fieldInfo)
3235

3336
public static bool IsTypeSerializable(Type type, bool allowCollections = true)
3437
{
35-
if (type == typeof(object))
38+
if (type == typeof(object) || type.IsAbstract || type.IsInterface)
3639
{
3740
return false;
3841
}
3942

40-
if (type == typeof(string) ||
41-
type == typeof(bool) ||
42-
type == typeof(char) ||
43-
type == typeof(int) ||
44-
type == typeof(float) ||
43+
if (type.IsEnum)
44+
{
45+
var underlyingType = Enum.GetUnderlyingType(type);
46+
47+
return underlyingType != typeof(long) && underlyingType != typeof(ulong);
48+
}
49+
50+
if (type.IsPrimitive ||
51+
type == typeof(string) ||
4552
type == typeof(Vector2) ||
4653
type == typeof(Vector2Int) ||
4754
type == typeof(Vector3) ||
@@ -66,20 +73,18 @@ public static bool IsTypeSerializable(Type type, bool allowCollections = true)
6673
return true;
6774
}
6875

69-
if (type.IsEnum)
76+
if (typeof(Delegate).IsAssignableFrom(type))
7077
{
71-
return true;
72-
}
73-
74-
if (type.IsPrimitive)
75-
{
76-
return true;
78+
return false;
7779
}
7880

7981
if (type.IsArray)
8082
{
8183
var elementType = type.GetElementType();
82-
return allowCollections && IsTypeSerializable(elementType, allowCollections: false);
84+
85+
return type.GetArrayRank() == 1 &&
86+
allowCollections &&
87+
IsTypeSerializable(elementType, allowCollections: false);
8388
}
8489

8590
if (type.IsGenericType)
@@ -89,7 +94,9 @@ public static bool IsTypeSerializable(Type type, bool allowCollections = true)
8994
if (genericTypeDefinition == typeof(List<>))
9095
{
9196
var elementType = type.GetGenericArguments()[0];
92-
return allowCollections && IsTypeSerializable(elementType, allowCollections: false);
97+
98+
return allowCollections &&
99+
IsTypeSerializable(elementType, allowCollections: false);
93100
}
94101

95102
if (genericTypeDefinition == typeof(Dictionary<,>))
@@ -98,7 +105,9 @@ public static bool IsTypeSerializable(Type type, bool allowCollections = true)
98105
}
99106
}
100107

101-
if (typeof(IEnumerable).IsAssignableFrom(type))
108+
if (type.Assembly == CoreLibAssembly ||
109+
type.Assembly == SystemAssembly ||
110+
type.Assembly == SystemCoreAssembly)
102111
{
103112
return false;
104113
}

0 commit comments

Comments
 (0)