11namespace 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