- Home /
Custom Inspector, Collections, SetDirty
I'm struggling here with custom editors for a while, and still it's not clear to me how SetDirty works.
In Custom Inspector I currently working on I receive a collection of scripts in child objects and modify theirs values. Apparently this changes is not applied (after Unity restart them is back to default values)
So in my OnInspectorGUI I work with these two, layer (current edited) and items (childs of layer):
var layer = (target as BLayer);
var items = layer.GetComponentsInChildren<BItem>();
I iterate through items in OnInspectorGUI, change its properties, gameObject name, properties of SpriteRenderer attached to BItem object...
At the end of OnInspectorGUI I put lots of "Set Dirty" but nonetheless nothing is applied after Unity restart...
if (GUI.changed)
{
UpdateHierarchy();
EditorUtility.SetDirty(target);
EditorUtility.SetDirty(layer);
Array.ForEach(items, i => EditorUtility.SetDirty(i.gameObject));
}
I also tried to put serializedObject.Update();
in a first line of OnInspectorGUI and serializedObject.ApplyModifiedProperties();
in a last one. Not sure what actually this lines should do, but just in case.
I'll try to find some comprehensive tutorials on this subject, but for now I'll be really happy if you tell me what I do wrong. Thanks!