- Home /
EditorGUILayout.PropertyField cannot draw custom class?
I have a class A
that I marked as [Serializable]
. In the class it contained 3 public string
so if I declare that with public
, in the inspector it would show up as 3 string under a drop down.
[System.Serializable]
public class A {
public string englishText;
public string thaiText;
public string japaneseText;
}
However, I have one bigger class, B
that contained many other things I have written custom inspector for. In this class B I also have some instance of public
A in it. (Let's say the name of this variable is aProp
) I wanted to use A's default inspector style so I think I could use
SerializedProperty aProp = info.FindPropertyRelative("aProp");
and then
EditorGUILayout.PropertyField(aProp,GUIContent.none);
I think it would show up as a foldout, but no, only foldout arrow appeared and nothing would happen if I click on it except the arrow will flip pointing downward. Nothing got folded out. Why? Is the PropertyField limited to 1 line so foldout cannot display?
Answer by karl_ · Aug 11, 2016 at 01:41 PM
If you're just wanting those properties to draw, include the includeChildren = true
parameter in your EditorGUILayout.PropertyField
call.
If you have a custom editor for class A
this approach will not utilize it however.
Answer by edwardrowe · Aug 05, 2016 at 04:58 AM
EDIT: Karl's post is definitely the right way to handle this. I'm leaving this post up just in case the code snippet is useful, but please upvote his answer!
I had this same issue with the same foldout behaviour, and I'm not sure why this doesn't work, exactly.
I found a workaround thanks to vexe in this post: http://answers.unity3d.com/questions/907147/unable-to-draw-propertyfield-in-inspector-for-some.html
Here's my code (notice in my case I don't use FindPropertyRelative, since aProp is a member of my class):
var aProp = this.serializedObject.FindProperty("aProp");
int startingDepth = aProp.depth;
// Move into the first child of aProp
aProp.NextVisible(true);
do
{
EditorGUILayout.PropertyField(aProp, true);
aProp.NextVisible(false);
// Quit iterating when you are back at the original depth (you've drawn all children)
} while (aProp.depth > startingDepth);
If anyone knows a better way or at least knows why EditorGUILayout.PropertyLayout on the Serialized type with Serialized children doesn't work, I'd still love to know.
Awesome! I used this, but with EditorGUI ins$$anonymous$$d, to make a ReadOnly PropertyDrawer that properly displayed custom class PropertyDrawers, thanks!
Interesting that it's still relevant 4 years later.
Your answer
Follow this Question
Related Questions
Inspector Overlapping Text Label at a Position 1 Answer
Draw on Multiple Lines with EditorGUI 1 Answer
Custom tree-like editor 0 Answers
Property Drawer SerializedProperty is null 2 Answers
Custom Inspector "Multi-object editing not supported" 4 Answers