1
1
#if UNITY_2019_3_OR_NEWER
2
2
using System ;
3
- using System . Runtime . Serialization ;
4
3
5
4
using UnityEditor ;
6
5
using UnityEngine ;
@@ -17,13 +16,12 @@ public class ReferencePickerAttributeDrawer : ToolboxSelfPropertyDrawer<Referenc
17
16
private static readonly TypeAppearanceContext sharedAppearance = new TypeAppearanceContext ( sharedConstraint , TypeGrouping . None , true ) ;
18
17
private static readonly TypeField typeField = new TypeField ( sharedConstraint , sharedAppearance ) ;
19
18
20
-
21
19
private void UpdateContexts ( ReferencePickerAttribute attribute )
22
20
{
23
21
sharedAppearance . TypeGrouping = attribute . TypeGrouping ;
24
22
}
25
23
26
- private Type GetParentType ( SerializedProperty property , ReferencePickerAttribute attribute )
24
+ private Type GetParentType ( ReferencePickerAttribute attribute , SerializedProperty property )
27
25
{
28
26
var fieldInfo = property . GetFieldInfo ( out _ ) ;
29
27
var fieldType = property . GetProperType ( fieldInfo ) ;
@@ -42,7 +40,7 @@ private Type GetParentType(SerializedProperty property, ReferencePickerAttribute
42
40
return fieldType ;
43
41
}
44
42
45
- private void CreateTypeProperty ( Rect position , SerializedProperty property , Type parentType )
43
+ private void CreateTypeProperty ( SerializedProperty property , Type parentType , ReferencePickerAttribute attribute , Rect position )
46
44
{
47
45
TypeUtilities . TryGetTypeFromManagedReferenceFullTypeName ( property . managedReferenceFullTypename , out var currentType ) ;
48
46
typeField . OnGui ( position , true , ( type ) =>
@@ -51,7 +49,7 @@ private void CreateTypeProperty(Rect position, SerializedProperty property, Type
51
49
{
52
50
if ( ! property . serializedObject . isEditingMultipleObjects )
53
51
{
54
- UpdateTypeProperty ( property , type ) ;
52
+ UpdateTypeProperty ( property , type , attribute ) ;
55
53
}
56
54
else
57
55
{
@@ -61,7 +59,7 @@ private void CreateTypeProperty(Rect position, SerializedProperty property, Type
61
59
using ( var so = new SerializedObject ( target ) )
62
60
{
63
61
SerializedProperty sp = so . FindProperty ( property . propertyPath ) ;
64
- UpdateTypeProperty ( sp , type ) ;
62
+ UpdateTypeProperty ( sp , type , attribute ) ;
65
63
}
66
64
}
67
65
}
@@ -73,9 +71,10 @@ private void CreateTypeProperty(Rect position, SerializedProperty property, Type
73
71
} , currentType , parentType ) ;
74
72
}
75
73
76
- private void UpdateTypeProperty ( SerializedProperty property , Type referenceType )
74
+ private void UpdateTypeProperty ( SerializedProperty property , Type targetType , ReferencePickerAttribute attribute )
77
75
{
78
- var obj = referenceType != null ? FormatterServices . GetUninitializedObject ( referenceType ) : null ;
76
+ var forceUninitializedInstance = attribute . ForceUninitializedInstance ;
77
+ var obj = ReflectionUtility . CreateInstance ( targetType , forceUninitializedInstance ) ;
79
78
property . serializedObject . Update ( ) ;
80
79
property . managedReferenceValue = obj ;
81
80
property . serializedObject . ApplyModifiedProperties ( ) ;
@@ -106,7 +105,6 @@ private Rect PrepareTypePropertyPosition(bool hasLabel, in Rect labelPosition, i
106
105
return position ;
107
106
}
108
107
109
-
110
108
protected override void OnGuiSafe ( SerializedProperty property , GUIContent label , ReferencePickerAttribute attribute )
111
109
{
112
110
//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,
122
120
var hasLabel = ! string . IsNullOrEmpty ( label . text ) ;
123
121
var position = PrepareTypePropertyPosition ( hasLabel , in labelRect , in inputRect , isPropertyExpanded ) ;
124
122
125
- var parentType = GetParentType ( property , attribute ) ;
126
- CreateTypeProperty ( position , property , parentType ) ;
123
+ var parentType = GetParentType ( attribute , property ) ;
124
+ CreateTypeProperty ( property , parentType , attribute , position ) ;
127
125
if ( isPropertyExpanded )
128
126
{
129
127
ToolboxEditorGui . DrawPropertyChildren ( property ) ;
@@ -134,7 +132,6 @@ protected override void OnGuiSafe(SerializedProperty property, GUIContent label,
134
132
}
135
133
}
136
134
137
-
138
135
public override bool IsPropertyValid ( SerializedProperty property )
139
136
{
140
137
return property . propertyType == SerializedPropertyType . ManagedReference ;
0 commit comments