11#if UNITY_2019_3_OR_NEWER
22using System ;
3- using System . Runtime . Serialization ;
43
54using UnityEditor ;
65using UnityEngine ;
@@ -17,13 +16,12 @@ public class ReferencePickerAttributeDrawer : ToolboxSelfPropertyDrawer<Referenc
1716 private static readonly TypeAppearanceContext sharedAppearance = new TypeAppearanceContext ( sharedConstraint , TypeGrouping . None , true ) ;
1817 private static readonly TypeField typeField = new TypeField ( sharedConstraint , sharedAppearance ) ;
1918
20-
2119 private void UpdateContexts ( ReferencePickerAttribute attribute )
2220 {
2321 sharedAppearance . TypeGrouping = attribute . TypeGrouping ;
2422 }
2523
26- private Type GetParentType ( SerializedProperty property , ReferencePickerAttribute attribute )
24+ private Type GetParentType ( ReferencePickerAttribute attribute , SerializedProperty property )
2725 {
2826 var fieldInfo = property . GetFieldInfo ( out _ ) ;
2927 var fieldType = property . GetProperType ( fieldInfo ) ;
@@ -42,7 +40,7 @@ private Type GetParentType(SerializedProperty property, ReferencePickerAttribute
4240 return fieldType ;
4341 }
4442
45- private void CreateTypeProperty ( Rect position , SerializedProperty property , Type parentType )
43+ private void CreateTypeProperty ( SerializedProperty property , Type parentType , ReferencePickerAttribute attribute , Rect position )
4644 {
4745 TypeUtilities . TryGetTypeFromManagedReferenceFullTypeName ( property . managedReferenceFullTypename , out var currentType ) ;
4846 typeField . OnGui ( position , true , ( type ) =>
@@ -51,7 +49,7 @@ private void CreateTypeProperty(Rect position, SerializedProperty property, Type
5149 {
5250 if ( ! property . serializedObject . isEditingMultipleObjects )
5351 {
54- UpdateTypeProperty ( property , type ) ;
52+ UpdateTypeProperty ( property , type , attribute ) ;
5553 }
5654 else
5755 {
@@ -61,7 +59,7 @@ private void CreateTypeProperty(Rect position, SerializedProperty property, Type
6159 using ( var so = new SerializedObject ( target ) )
6260 {
6361 SerializedProperty sp = so . FindProperty ( property . propertyPath ) ;
64- UpdateTypeProperty ( sp , type ) ;
62+ UpdateTypeProperty ( sp , type , attribute ) ;
6563 }
6664 }
6765 }
@@ -73,9 +71,10 @@ private void CreateTypeProperty(Rect position, SerializedProperty property, Type
7371 } , currentType , parentType ) ;
7472 }
7573
76- private void UpdateTypeProperty ( SerializedProperty property , Type referenceType )
74+ private void UpdateTypeProperty ( SerializedProperty property , Type targetType , ReferencePickerAttribute attribute )
7775 {
78- var obj = referenceType != null ? FormatterServices . GetUninitializedObject ( referenceType ) : null ;
76+ var forceUninitializedInstance = attribute . ForceUninitializedInstance ;
77+ var obj = ReflectionUtility . CreateInstance ( targetType , forceUninitializedInstance ) ;
7978 property . serializedObject . Update ( ) ;
8079 property . managedReferenceValue = obj ;
8180 property . serializedObject . ApplyModifiedProperties ( ) ;
@@ -106,7 +105,6 @@ private Rect PrepareTypePropertyPosition(bool hasLabel, in Rect labelPosition, i
106105 return position ;
107106 }
108107
109-
110108 protected override void OnGuiSafe ( SerializedProperty property , GUIContent label , ReferencePickerAttribute attribute )
111109 {
112110 //NOTE: we want to close scope manually because ExitGUIException can interrupt drawing and SerializedProperties stack
@@ -122,8 +120,8 @@ protected override void OnGuiSafe(SerializedProperty property, GUIContent label,
122120 var hasLabel = ! string . IsNullOrEmpty ( label . text ) ;
123121 var position = PrepareTypePropertyPosition ( hasLabel , in labelRect , in inputRect , isPropertyExpanded ) ;
124122
125- var parentType = GetParentType ( property , attribute ) ;
126- CreateTypeProperty ( position , property , parentType ) ;
123+ var parentType = GetParentType ( attribute , property ) ;
124+ CreateTypeProperty ( property , parentType , attribute , position ) ;
127125 if ( isPropertyExpanded )
128126 {
129127 ToolboxEditorGui . DrawPropertyChildren ( property ) ;
@@ -134,7 +132,6 @@ protected override void OnGuiSafe(SerializedProperty property, GUIContent label,
134132 }
135133 }
136134
137-
138135 public override bool IsPropertyValid ( SerializedProperty property )
139136 {
140137 return property . propertyType == SerializedPropertyType . ManagedReference ;
0 commit comments