Skip to content

Commit 7fa0e0a

Browse files
committed
fix: Fixed compilation errors for Unity 2019
C# 7 cannot detect if member variables of a readonly struct are changed inside the property setter, so it throws errors when we try to use property setters. Fixed by separating setters logic into methods.
1 parent 734d234 commit 7fa0e0a

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

Editor/Drawers/TypeFieldDrawer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,13 @@ private void OnTypeSelected(Type selectedType)
151151
if (_serializedTypeRef.TypeNameAndAssembly == selectedTypeNameAndAssembly)
152152
return;
153153

154+
// C# 7 is dumb and doesn't know that we don't change member variables in the property setter
155+
#if UNITY_2020_2_OR_NEWER
154156
_serializedTypeRef.TypeNameAndAssembly = selectedTypeNameAndAssembly;
157+
#else
158+
_serializedTypeRef.SetTypeNameAndAssembly(selectedTypeNameAndAssembly);
159+
#endif
160+
155161
GUI.changed = true;
156162
}
157163
}
Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
namespace TypeReferences.Editor.Util
22
{
33
using System;
4+
using System.Diagnostics.CodeAnalysis;
5+
using JetBrains.Annotations;
46
using UnityEditor;
57

68
/// <summary>
@@ -20,39 +22,46 @@ public SerializedTypeReference(SerializedProperty typeReferenceProperty)
2022
_guidProperty = typeReferenceProperty.FindPropertyRelative(nameof(TypeReference.GUID));
2123
_guidAssignmentFailedProperty = typeReferenceProperty.FindPropertyRelative(nameof(TypeReference.GuidAssignmentFailed));
2224

23-
SetGuidIfAssignmentFailed();
25+
FindGuidIfAssignmentFailed();
2426
}
2527

2628
public string TypeNameAndAssembly
2729
{
2830
get => _typeNameProperty.stringValue;
29-
set
30-
{
31-
_typeNameProperty.stringValue = value;
32-
_guidProperty.stringValue = GetClassGuidFromTypeName(value);
33-
_parentObject.ApplyModifiedProperties();
34-
}
31+
set => SetTypeNameAndAssembly(value);
3532
}
3633

3734
public bool TypeNameHasMultipleDifferentValues => _typeNameProperty.hasMultipleDifferentValues;
3835

3936
private bool GuidAssignmentFailed
4037
{
4138
get => _guidAssignmentFailedProperty.boolValue;
42-
set
43-
{
44-
_guidAssignmentFailedProperty.boolValue = value;
45-
_parentObject.ApplyModifiedProperties();
46-
}
39+
// Used in C# 8
40+
[UsedImplicitly] set => SetGUIDAssignmentFailed(value);
4741
}
4842

49-
private string GUID
43+
// Used in C# 8
44+
[UsedImplicitly] private string GUID { set => SetGUID(value); }
45+
46+
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global",
47+
Justification = "The method is used by TypeFieldDrawer in C# 7")]
48+
public void SetTypeNameAndAssembly(string value)
49+
{
50+
_typeNameProperty.stringValue = value;
51+
_guidProperty.stringValue = GetClassGuidFromTypeName(value);
52+
_parentObject.ApplyModifiedProperties();
53+
}
54+
55+
private void SetGUIDAssignmentFailed(bool value)
5056
{
51-
set
52-
{
53-
_guidProperty.stringValue = value;
54-
_parentObject.ApplyModifiedProperties();
55-
}
57+
_guidAssignmentFailedProperty.boolValue = value;
58+
_parentObject.ApplyModifiedProperties();
59+
}
60+
61+
private void SetGUID(string value)
62+
{
63+
_guidProperty.stringValue = value;
64+
_parentObject.ApplyModifiedProperties();
5665
}
5766

5867
private static string GetClassGuidFromTypeName(string typeName)
@@ -61,13 +70,20 @@ private static string GetClassGuidFromTypeName(string typeName)
6170
return TypeReference.GetClassGUID(type);
6271
}
6372

64-
private void SetGuidIfAssignmentFailed()
73+
private void FindGuidIfAssignmentFailed()
6574
{
6675
if ( ! GuidAssignmentFailed || string.IsNullOrEmpty(TypeNameAndAssembly))
6776
return;
6877

78+
// C# 7 is dumb and doesn't know that we don't change member variables in the property setter
79+
80+
#if UNITY_2020_2_OR_NEWER
6981
GuidAssignmentFailed = false;
7082
GUID = GetClassGuidFromTypeName(TypeNameAndAssembly);
83+
#else
84+
SetGUIDAssignmentFailed(false);
85+
SetGUID(GetClassGuidFromTypeName(TypeNameAndAssembly));
86+
#endif
7187
}
7288
}
7389
}

0 commit comments

Comments
 (0)