Skip to content

Commit

Permalink
Added custom comparer to build TypeReference collections with
Browse files Browse the repository at this point in the history
  • Loading branch information
SolidAlloy committed Oct 1, 2020
1 parent 876bb23 commit 0b3a64d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Editor/Util/SerializedTypeReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public SerializedTypeReference(SerializedProperty typeReferenceProperty)
{
_parentObject = typeReferenceProperty.serializedObject;
_typeNameProperty = typeReferenceProperty.FindPropertyRelative(TypeReference.NameOfTypeNameField);
_guidProperty = typeReferenceProperty.FindPropertyRelative(TypeReference.NameOfGuidField);
_guidProperty = typeReferenceProperty.FindPropertyRelative(nameof(TypeReference.GUID));
_guidAssignmentFailedProperty = typeReferenceProperty.FindPropertyRelative(nameof(TypeReference.GuidAssignmentFailed));

SetGuidIfAssignmentFailed();
Expand Down
9 changes: 4 additions & 5 deletions Runtime/TypeReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ public sealed class TypeReference : ISerializationCallbackReceiver
public const string NoneElement = "(None)";

public const string NameOfTypeNameField = nameof(_typeNameAndAssembly);
public const string NameOfGuidField = nameof(_GUID);

public bool GuidAssignmentFailed;
public string GUID;

[SerializeField] private string _typeNameAndAssembly;
[SerializeField] private string _GUID;
private Type _type;

/// <summary>
Expand Down Expand Up @@ -79,7 +78,7 @@ public Type Type

public static implicit operator Type(TypeReference typeReference)
{
return typeReference.Type;
return typeReference?.Type;
}

public static implicit operator TypeReference(Type type)
Expand All @@ -100,13 +99,13 @@ private void SetClassGuidIfExists(Type type)
{
try
{
_GUID = GetClassGUID(type);
GUID = GetClassGUID(type);
}
// It is thrown on assembly recompiling if field initialization is used on field.
catch (UnityException)
{
GuidAssignmentFailed = true;
_GUID = string.Empty;
GUID = string.Empty;
}
}

Expand Down
13 changes: 13 additions & 0 deletions Runtime/TypeReferenceComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace TypeReferences
{
using System.Collections.Generic;

/// <summary>Custom comparer that allows to build TypeReference collections.</summary>
public class TypeReferenceComparer : IEqualityComparer<TypeReference>
{
public bool Equals(TypeReference x, TypeReference y) => x?.Type == y?.Type;

public int GetHashCode(TypeReference obj) =>
string.IsNullOrEmpty(obj.GUID) ? obj.Type.GetHashCode() : obj.GUID.GetHashCode();
}
}
3 changes: 3 additions & 0 deletions Runtime/TypeReferenceComparer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0b3a64d

Please sign in to comment.