@@ -40,15 +40,15 @@ static CreatableObjectDrawer()
4040
4141 public override void OnGUI ( Rect rect , SerializedProperty property , GUIContent label )
4242 {
43- ( _ , Type type ) = property . GetFieldInfoAndType ( ) ;
43+ Type propertyType = property . GetObjectType ( ) ;
4444
45- if ( ! type . InheritsFrom ( typeof ( ScriptableObject ) ) && ! type . InheritsFrom ( typeof ( MonoBehaviour ) ) )
45+ if ( ! propertyType . InheritsFrom ( typeof ( ScriptableObject ) ) && ! propertyType . InheritsFrom ( typeof ( MonoBehaviour ) ) )
4646 {
4747 EditorGUILayoutHelper . DrawErrorMessage ( "Creatable attribute can only be used on ScriptableObjects and MonoBehaviours." ) ;
4848 return ;
4949 }
5050
51- if ( type . IsAbstract )
51+ if ( propertyType . IsAbstract )
5252 {
5353 EditorGUILayoutHelper . DrawErrorMessage ( "Creatable attribute can only be used on fields of non-abstract type." ) ;
5454 return ;
@@ -65,32 +65,37 @@ public override void OnGUI(Rect rect, SerializedProperty property, GUIContent la
6565 ( var objectRect , var buttonRect ) = rect . CutVertically ( buttonWidth , true ) ;
6666 objectRect . width -= paddingBetween ;
6767
68- GenericObjectDrawer . ObjectField ( objectRect , property , label ) ;
68+ DrawObjectField ( objectRect , property , label ) ;
6969
7070 if ( ! GUI . Button ( buttonRect , "+" ) )
7171 return ;
7272
7373 // this is for scriptable objects
74- if ( type . InheritsFrom ( typeof ( ScriptableObject ) ) )
74+ if ( propertyType . InheritsFrom ( typeof ( ScriptableObject ) ) )
7575 {
76- CreateScriptableObject ( property , type ) ;
76+ CreateScriptableObject ( property , propertyType ) ;
7777 }
7878 else
7979 {
80- CreateMonoBehaviour ( property , type ) ;
80+ CreateMonoBehaviour ( property , propertyType ) ;
8181 }
8282 }
8383
84+ protected virtual void DrawObjectField ( Rect objectRect , SerializedProperty property , GUIContent label )
85+ {
86+ GenericObjectDrawer . ObjectField ( objectRect , property , label ) ;
87+ }
88+
8489 #region Create Scriptable Object
8590
86- private void CreateScriptableObject ( SerializedProperty property , Type type )
91+ private static void CreateScriptableObject ( SerializedProperty property , Type type )
8792 {
8893 var asset = CreateAsset ( property , type ) ;
8994 property . objectReferenceValue = asset ;
9095 EditorGUIUtility . PingObject ( asset ) ;
9196 }
9297
93- private ScriptableObject CreateAsset ( SerializedProperty property , Type type )
98+ private static ScriptableObject CreateAsset ( SerializedProperty property , Type type )
9499 {
95100 var folderPath = ProjectWindowUtilProxy . GetActiveFolderPath ( ) ;
96101
@@ -121,12 +126,12 @@ private ScriptableObject CreateAsset(SerializedProperty property, Type type)
121126 return asset ;
122127 }
123128
124- private bool IsValidPath ( string path )
129+ private static bool IsValidPath ( string path )
125130 {
126131 return ! string . IsNullOrEmpty ( path ) && IsInProject ( path ) && HasCorrectExtension ( path ) ;
127132 }
128133
129- private bool IsInProject ( string path )
134+ private static bool IsInProject ( string path )
130135 {
131136 if ( PathHelper . IsSubPathOf ( path , _assetsPath ) || PathHelper . IsSubPathOf ( path , _packagesPath ) )
132137 return true ;
@@ -135,7 +140,7 @@ private bool IsInProject(string path)
135140 return false ;
136141 }
137142
138- private bool HasCorrectExtension ( string path )
143+ private static bool HasCorrectExtension ( string path )
139144 {
140145 if ( path . EndsWith ( ".asset" ) )
141146 return true ;
@@ -152,7 +157,7 @@ private bool HasCorrectExtension(string path)
152157 private const string PropertyPathKey = "CreatableObjectDrawer_PropertyPath" ;
153158 private const string AddedComponentTypeKey = "CreatableObjectDrawer_AddedComponent" ;
154159
155- private void CreateMonoBehaviour ( SerializedProperty property , Type type )
160+ public static void CreateMonoBehaviour ( SerializedProperty property , Type type , Action < Component , bool > onComponentAdded = null )
156161 {
157162 var parentComponent = property . serializedObject . targetObject as MonoBehaviour ;
158163
@@ -163,6 +168,8 @@ private void CreateMonoBehaviour(SerializedProperty property, Type type)
163168
164169 var component = AddComponentHelper . AddComponent ( gameObject , type , out bool reloadRequired ) ;
165170
171+ onComponentAdded ? . Invoke ( component , reloadRequired ) ;
172+
166173 if ( reloadRequired )
167174 {
168175 PersistentStorage . SaveData ( TargetComponentKey , property . serializedObject . targetObject ) ;
0 commit comments