Quantcast
Channel: Latest Questions by coffiarts
Viewing all articles
Browse latest Browse all 15

How to edit array/list property with custom PropertyDrawer?

$
0
0
This is not about custom inspectors for arrays/lists *in general* (I assume/hope that I already know how to do that). I am having a specifically weird behaviour only when using arrays/lists as *nested* properties. **In brief:** Using EditorGUI.PropertyField on a SerializedProperty (which is actually a string[]) won't display a proper array/list inspector element. Instead, I am only seeing a dull "foldout icon". **And In details...** I am using a **custom PropertyDrawer** (see source code below) for handling a simple custom data class instance inside the Inspector. My approach seems to be very simple, mainly follwowing the Unity docs at: https://docs.unity3d.com/ScriptReference/PropertyDrawer.html Here's my"data item class": [Serializable] public class ThesaurusItem { public string word; public string[] syllables; } The issue goes with its "syllables" property (string[]). The data class is used as a plain and straight property inside the surrounding MonoBehaviour class: public class Thesaurus : MonoBehaviour { public ThesaurusItem testItem; } Here's my issue: Any array or list type properties like "syllables" are only displayed as a "foldout" icon - and otherwise emtpy. In particular, there's no "size" input field at all. This is what I'd **expect** to see in the Inspector (which is what I get for a simple public string[] class property): ![alt text][1] And here's **what I actually get**: ![alt text][2] As soon as my string[] property is handled as part of the data item class by the custom drawer, it is nothing more than a "foldout" icon - with nothing else otherwise. I can click it (i.e. it turns into a downward arrow), but it doesn't expand anything. Of course my array property is still empty, so I don't expect to see any content. But I **would expect** to see at least the "size" field. Finally, here's my PropertyDrawer: // Note that this is more or less identical to Unity's example code here: https://docs.unity3d.com/ScriptReference/PropertyDrawer.html [CustomPropertyDrawer(typeof(ThesaurusItem))] public class ThesaurusItemDrawer : PropertyDrawer { // Draw the property inside the given rect public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // Using BeginProperty / EndProperty on the parent property means that // prefab override logic works on the entire property. EditorGUI.BeginProperty(position, label, property); // Draw label position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label); // Don't make child fields be indented var indent = EditorGUI.indentLevel; EditorGUI.indentLevel = 0; // Calculate rects Rect wordRect = new Rectposition.x, position.y, 90, position.height); Rect syllablesRect = new Rect(wordRect.position.x + wordRect.width + 20, position.y, 50, position.height); // Draw fields - pass GUIContent.none to each so they are drawn without labels EditorGUI.PropertyField(wordRect, property.FindPropertyRelative("word"), GUIContent.none); EditorGUI.PropertyField(syllablesRect, property.FindPropertyRelative("syllables"), GUIContent.none, true); // Set indent back to what it was EditorGUI.indentLevel = indent; EditorGUI.EndProperty(); } } Any ideas what's going wrong here? [1]: /storage/temp/130351-propertydrawer-arrays-01.png [2]: /storage/temp/130353-propertydrawer-arrays-03.png

Viewing all articles
Browse latest Browse all 15

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>