Skip to content

Commit 99ce2de

Browse files
committed
fix: Fixed exception when creating a new unity object through Creatable attribute
1 parent 07c73ae commit 99ce2de

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

Editor/Drawers/CreatableObjectDrawer.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,24 @@ public override void OnGUI(Rect rect, SerializedProperty property, GUIContent la
7070
if ( ! GUI.Button(buttonRect, "+"))
7171
return;
7272

73-
// this is for scriptable objects
73+
// If we create a new concrete type, we will force domain reload in the middle of OnGUI call. This will create an exception in the UnityEditor code.
74+
// To avoid that, we execute the creation on editor application update instead.
75+
_propertyForCreation = property;
76+
_propertyTypeForCreation = propertyType;
77+
EditorApplication.update += OneTimeUpdate;
78+
}
79+
80+
private SerializedProperty _propertyForCreation;
81+
private Type _propertyTypeForCreation;
82+
83+
private void OneTimeUpdate()
84+
{
85+
CreateUnityObject(_propertyForCreation, _propertyTypeForCreation);
86+
EditorApplication.update -= OneTimeUpdate;
87+
}
88+
89+
private static void CreateUnityObject(SerializedProperty property, Type propertyType)
90+
{
7491
if (propertyType.InheritsFrom(typeof(ScriptableObject)))
7592
{
7693
CreateScriptableObject(property, propertyType);

0 commit comments

Comments
 (0)