- Home /
Mixing custom editors with custom property drawers, possible ?
Is it possible to display custom property drawers inside custom editors ? I've been struggling to achieve this result without much luck.
Here's an oversimplification of my problem:
[Serializable]
public class A
{
public int intValue;
}
[Serializable]
public class B
{
public A instanceOfA;
}
I have a custom editor for class B and a custom drawer for class A and i want to use the custom drawer instead of reimplementing the functionality inside the custom editor.
The custom drawer displays fine inside a class that doesn't have a custom editor (uses the default inspector), but i can't seem to be able to "call" it inside my custom editor.
From what i've read, custom drawers are called if i use the EditorGUILayout.PropertyField method, but the problem with that is it expects a SerializableProperty and i can't get a hold of one of those since my classes don't extend Object.
Any help with this would be greatly appreciated, even if it means letting me know that this isn't yet possible.
Thank you !
Answer by Bunny83 · Apr 10, 2013 at 08:44 PM
I never tried it, but i guess it should be possible. Have you tried using a PropertyField in your custom editor? I think that should actually draw the property with a proper PropertyDrawer.
edit
Well a PropertyField of course needs a SerializedProperty to work with. This can be aquired from a SerializedObject.
Inside a custom editor you already get such an object in the serializedObject variable. Note: If your editor allows multiedit this SerializedObject represents all selected objects at once!
You can also create your own SerializedObject like this:
SerializedObject obj = new SerializedObject(objectToInspect);
// And as well by passing an array of objects:
SerializedObject obj = new SerializedObject(arrayOfObjectsToInspect);
isEditingMultipleObjects can be used to determine if multiple objects are edited. Also important, if you want to support multiediting, is hasMultipleDifferentValues.
Solved !
@Bunny83 I wanted to use that but like i mentioned in my post, i noticed it required a SerializableProperty and i had no idea how to get one of those from the objects i was working with.
That's until i realized that my custom editor, besides having a target object that i was casting to class B and obtaining/displaying properties for, also had a serializedObject member generated from the current $$anonymous$$onoBehaviour that is being edited.
From that i was able to obtain the property i needed via the FindProperty method of the serializedObject.
I basically did serializedObject.FindProperty("instanceOfA") and i got a hold of the serialized property used for instanceOfA, and i could then pass that to PropertyField which correctly displayed my custom drawer.
@mihaiailioai, if @bunny83 answer's is correct, then you should mark it so. Upvote to the question and answer because I was wondering about that too when I start using c# nested class.
@$$anonymous$$ihaiAilioai: Yes, sorry, i just thought that you know how the SerializedObject / Property works since you talked about the property drawer and custom editor like you used it already a few times ;) I added some basic information on how to use the SerializedObject
I know this Question is old but PropertyDrawers are not working in my custom editor. I made a [Serializable] public class Foo { [TextArea(10,10)] public string text; }
. If use Foo in a $$anonymous$$onoBehavior I get the correct large text area. But if I use Foo in a custom EditorWindow and draw the SerializedObject in OnGUI with EditGUILayout.PropertyField(serializedProperty, true)
, the fields I defined in Foo
show up but all the attributes about how to display them are ignored. Any idea how to fix that?