Custom Editor: Changing isExpanded from another editor
I have a similar problem to this question: https://answers.unity.com/questions/1420119/changing-isexpanded-property-from-another-editor.html
I'm working on a custom editor to change values of another component. I have this code:
public override void OnInspectorGUI()
{
var component = obj.GetComponent(targetComponent.GetType());
var serializedObj = new SerializedObject(component);
var serializedProperty = serializedObject.GetIterator();
foreach(var serializedField in GetField(serializedProperty)
{
EditorGUILayout.PropertyField(serializedField);
}
serializedObj.ApplyModifiedProperties();
}
public static IEnumerable<SerializedProperty> GetField(SerializedProperty prop)
{
if(prop.NextVisible(true))
{
do
{
yield return prop;
}while(prop.NextVisible(false));
}
}
I can change all values just fine except for one thing:
If there is an array or list in the target component it can't be expanded or collapsed.
If I edit the target component directly in the inspector, I can do it, and it will transfer the state to the custom editor - but I can't do it from the custom editor directly.
Here is what I've tried: (inside foreach loop):
if(serializedField.isArray){
serializedField.isExpanded = true;
}
Does nothing. I can see if the array is expanded or not with isExpanded
, but nothing happens when I try to modify it.
The only thing I've noticed when I continuously click the expand arrow is that it tries to expand, but quickly returns to a collapsed state.