- Home /
Question by
Muit · Feb 27, 2016 at 10:43 PM ·
editoreditor-scriptingserializationpropertydrawerserialize
SerializedProperty with children class fields
public class MyClass {
public string name;
}
public class MyChildrenClass : MyClass {
public int live = 10;
}
public class MyObject : ScriptableObject {
public MyClass myClass = new MyChildrenClass();
}
How can I get the MyChildrenClass properties form the SerializedProperty?
SerializedObject object = new SerializedObject(new MyObject());
SerializedProperty myClassSerialized = object.FindProperty("myClass");
//Doesnt get live property becouse it's interpretated as "MyClass"
SerializedProperty m_live = myClassSerialized.FindPropertyRelative("live");
Comment
Answer by deedilus · Jul 06, 2017 at 10:31 AM
You can iterate through child props of serialised classes by using an enumerator. You can do this a couple different ways, one being using GetEnumerator:
var childEnum = serializedObject.FindProperty("myClass").GetEnumerator();
while (childEnum.MoveNext())
{
var current = childEnum.Current as SerializedProperty;
if (current.name == "live")
{
// Do things
}
}
Or you can use Next or NextVisible with the enterChildren
argument set to true:
var myClassProps = serializedObject.FindProperty("myClass");
while (myClassProps.NextVisible(true))
{
if (myClassProps.name == "live")
{
// Do things
}
}
Answer by l8huang · Feb 20, 2018 at 02:32 AM
You can use property.GetEndProperty() and SerializedProperty.EqualContents():
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var endProperty = property.GetEndProperty();
while (property.NextVisible(true))
{
if (SerializedProperty.EqualContents(property, endProperty))
{
break;
}
...
Hello.
I am on unity 2018.3
these solutions don't work in public override void OnInspectorGUI() {} Any ideas for using in a custom inspector,Hello, I am trying both of these solutions and they do not work. Any ideas?