- Home /
The question is answered, right answer was accepted
EditorGUILayout.Foldout not working properly - results in argument exceptions
:
For starters, I have successfully created a CustomPropertyDrawer that presents an also functioning custom property type.
:
The problem occurs when I try to create a Foldout that contains everything that that CustomPropertyDrawer does within OnGUI.
:
I followed the information on these links:
:
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
showMatrix = EditorGUILayout.Foldout(showMatrix, "Matrix");
if (showMatrix)
{
// custom property code
}
}
:
Above is exactly what my code looks like (I even tried commenting out the "custom property code" that you see in the code snippet above, to no avail).
:
So, my question is, why is this not working? Is it a bug? Or is this outdated and I need to find a new solution?
Answer by Bunny83 · Jan 22, 2018 at 12:46 AM
Propertydrawers can't use GUILayout or EditorGUILayout stuff. A PropertyDrawer gets a rect that it has to use. Everything you draw has to fit into the "position" rect. If you need more space for your property you have to override the GetPropertyHeight method and return the required height.
Again you can't use any layout functions since the default inspector doesn't even propergate the layout event to property drawers.
So... could I, say use a "collapsed" bool field to get the same effect as the EditorGUILayout.Foldout?
:
Like this:
:
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
SerializedProperty = property.FindPropertyRelative("collapsed");
EditorGUI.PropertyField(newPosition, collapsed, GUIContent.none);
if (!collapsed.boolValue)
{
// custom property code
}
}
Unfortunately... the "code sample" above is not working properly...
:
Edit: Just tried this -- it functions properly, however it treats the entire property that appears when false as a button. A$$anonymous$$A Anywhere I click in the matrix that I have made appear closes it again. Thoughts?
:
I will mark this as the best answer, seeing as you did technically answer the original question.
Uhm, your code seems a bit messed up and contains many things that aren't declared / initialized anywhere. You don't need to define a collapsed field. Any SerializedProperty with children has an isExpanded field which you can use for exact this purpose.
You also don't need to use a PropertyField. It's fine to use an EditorGUI.Foldout
. It's just the layout variant which can't be used inside a property drawer. Of course you have to distribute your available space (the position rect) yourself. Something like this:
Rect foldoutRect = position;
foldoutRect.height = 15f;
position.y$$anonymous$$in = foldoutRect.y$$anonymous$$ax;
This will basically "cut off" 15 pixels from the original property space. The foldoutRect represents that top area which you can use for your foldout. The remaining position rect would be the space below.
Yes, my code snippet got screwed up. Regardless the version I posted did not work. And yeah, I kinda figured it was rather convoluted way of doing things.
:
In response to your comment -- I simply changed EditorGUI.Foldout ins$$anonymous$$d of EditorGUILayout.Foldout and everything is working exactly as needed... thanks!
This may be a bit of a hack but I think that GUILayout starts working in PropertyDrawers if you add a class like this to your project:
[CustomEditor(typeof(UnityEngine.Object), true)]
public class UnityObjectEditor : Editor
{
}
You can also replace UnityEngine.Object with $$anonymous$$onoBehaviour or ScriptableObject.
Follow this Question
Related Questions
Callback when custom PropertyAttribute changes 0 Answers
Unity won't let me override the default way of drawing an array/list? 2 Answers
How to get width at which Inspector does line breaks (for Vectors) 1 Answer
How to show type text in EditorGUI.ObjectField? 1 Answer
How to get notification of moving a GameObject in the hierachy when editing the scene? 1 Answer