Why a string in a Serialized Class is considered as an Array?

I have an ActionDialog Class:

    [System.Serializable]
    public class ActionDialog
    {
        public enum Speaker
        {
            Avatar,
            Npc
        }
        public Speaker speaker;
        public string content;
    }

I created a custom editor which get all the children from the class and draw a HelpBox if the child is an array:

foreach (SerializedProperty property in ExtensionMethods.GetChildren(list))
                    {
                        if (property.isArray)
                        {
                            Debug.Log(property.name);
                            GUILayout.BeginVertical(EditorStyles.helpBox);
                            EditorList.Show(property, options);
                            GUILayout.EndVertical();
                        }
                          
                        else
                            ShowElements(property, options);
                    }

and then the content just appears to be a list and is wrapped in a HelpBox???
3107123--234756--QQ截图20170614143511.png
Does anyone have an idea about how to fix this???

if (property.isArray && property.propertyType != SerializedPropertyType.String) Feels like the easiest fix.

1 Like

Well a string can be considered as an array of char.

That would be my fix too.

2 Likes

This reminds me of code I wrote in my SerializedPropertyExtensions class, where while I’m iterating SerializableProperties, and when I iterated to a property, I also checked if the previous(i.e. parent) property was a String. If this happens I skip to the endproperty for the string because in all my attempts to actually mess with the array elements for a string property, it ended with nothing but exceptions and broken inspectors.

Seems the UnityEditor also has a special case for strings and implicitly skips their array data by default. But the data is still there… strings are still counted as Arrays, and you can still run into them with Next(true).

Thank you for your solusion! :slight_smile: