Skip to content

Commit 0b3a64d

Browse files
committed
Added custom comparer to build TypeReference collections with
1 parent 876bb23 commit 0b3a64d

4 files changed

Lines changed: 21 additions & 6 deletions

File tree

Editor/Util/SerializedTypeReference.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public SerializedTypeReference(SerializedProperty typeReferenceProperty)
1818
{
1919
_parentObject = typeReferenceProperty.serializedObject;
2020
_typeNameProperty = typeReferenceProperty.FindPropertyRelative(TypeReference.NameOfTypeNameField);
21-
_guidProperty = typeReferenceProperty.FindPropertyRelative(TypeReference.NameOfGuidField);
21+
_guidProperty = typeReferenceProperty.FindPropertyRelative(nameof(TypeReference.GUID));
2222
_guidAssignmentFailedProperty = typeReferenceProperty.FindPropertyRelative(nameof(TypeReference.GuidAssignmentFailed));
2323

2424
SetGuidIfAssignmentFailed();

Runtime/TypeReference.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ public sealed class TypeReference : ISerializationCallbackReceiver
1919
public const string NoneElement = "(None)";
2020

2121
public const string NameOfTypeNameField = nameof(_typeNameAndAssembly);
22-
public const string NameOfGuidField = nameof(_GUID);
2322

2423
public bool GuidAssignmentFailed;
24+
public string GUID;
2525

2626
[SerializeField] private string _typeNameAndAssembly;
27-
[SerializeField] private string _GUID;
2827
private Type _type;
2928

3029
/// <summary>
@@ -79,7 +78,7 @@ public Type Type
7978

8079
public static implicit operator Type(TypeReference typeReference)
8180
{
82-
return typeReference.Type;
81+
return typeReference?.Type;
8382
}
8483

8584
public static implicit operator TypeReference(Type type)
@@ -100,13 +99,13 @@ private void SetClassGuidIfExists(Type type)
10099
{
101100
try
102101
{
103-
_GUID = GetClassGUID(type);
102+
GUID = GetClassGUID(type);
104103
}
105104
// It is thrown on assembly recompiling if field initialization is used on field.
106105
catch (UnityException)
107106
{
108107
GuidAssignmentFailed = true;
109-
_GUID = string.Empty;
108+
GUID = string.Empty;
110109
}
111110
}
112111

Runtime/TypeReferenceComparer.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace TypeReferences
2+
{
3+
using System.Collections.Generic;
4+
5+
/// <summary>Custom comparer that allows to build TypeReference collections.</summary>
6+
public class TypeReferenceComparer : IEqualityComparer<TypeReference>
7+
{
8+
public bool Equals(TypeReference x, TypeReference y) => x?.Type == y?.Type;
9+
10+
public int GetHashCode(TypeReference obj) =>
11+
string.IsNullOrEmpty(obj.GUID) ? obj.Type.GetHashCode() : obj.GUID.GetHashCode();
12+
}
13+
}

Runtime/TypeReferenceComparer.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)