1
1
namespace TypeReferences . Editor . Util
2
2
{
3
3
using System ;
4
+ using System . Diagnostics . CodeAnalysis ;
5
+ using JetBrains . Annotations ;
4
6
using UnityEditor ;
5
7
6
8
/// <summary>
@@ -20,39 +22,46 @@ public SerializedTypeReference(SerializedProperty typeReferenceProperty)
20
22
_guidProperty = typeReferenceProperty . FindPropertyRelative ( nameof ( TypeReference . GUID ) ) ;
21
23
_guidAssignmentFailedProperty = typeReferenceProperty . FindPropertyRelative ( nameof ( TypeReference . GuidAssignmentFailed ) ) ;
22
24
23
- SetGuidIfAssignmentFailed ( ) ;
25
+ FindGuidIfAssignmentFailed ( ) ;
24
26
}
25
27
26
28
public string TypeNameAndAssembly
27
29
{
28
30
get => _typeNameProperty . stringValue ;
29
- set
30
- {
31
- _typeNameProperty . stringValue = value ;
32
- _guidProperty . stringValue = GetClassGuidFromTypeName ( value ) ;
33
- _parentObject . ApplyModifiedProperties ( ) ;
34
- }
31
+ set => SetTypeNameAndAssembly ( value ) ;
35
32
}
36
33
37
34
public bool TypeNameHasMultipleDifferentValues => _typeNameProperty . hasMultipleDifferentValues ;
38
35
39
36
private bool GuidAssignmentFailed
40
37
{
41
38
get => _guidAssignmentFailedProperty . boolValue ;
42
- set
43
- {
44
- _guidAssignmentFailedProperty . boolValue = value ;
45
- _parentObject . ApplyModifiedProperties ( ) ;
46
- }
39
+ // Used in C# 8
40
+ [ UsedImplicitly ] set => SetGUIDAssignmentFailed ( value ) ;
47
41
}
48
42
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 )
50
56
{
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 ( ) ;
56
65
}
57
66
58
67
private static string GetClassGuidFromTypeName ( string typeName )
@@ -61,13 +70,20 @@ private static string GetClassGuidFromTypeName(string typeName)
61
70
return TypeReference . GetClassGUID ( type ) ;
62
71
}
63
72
64
- private void SetGuidIfAssignmentFailed ( )
73
+ private void FindGuidIfAssignmentFailed ( )
65
74
{
66
75
if ( ! GuidAssignmentFailed || string . IsNullOrEmpty ( TypeNameAndAssembly ) )
67
76
return ;
68
77
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
69
81
GuidAssignmentFailed = false ;
70
82
GUID = GetClassGuidFromTypeName ( TypeNameAndAssembly ) ;
83
+ #else
84
+ SetGUIDAssignmentFailed ( false ) ;
85
+ SetGUID ( GetClassGuidFromTypeName ( TypeNameAndAssembly ) ) ;
86
+ #endif
71
87
}
72
88
}
73
89
}
0 commit comments