Serialize a variable in a custom inspector?

It seems like mocking stuff with [SerializeField] in an Editor class won’t serialize the thing after an assembly reload. I have a simple boolean to represent a fold status for my editor - I want to serialize it. i.e. let’s say I unfolded in editor mode, went to play mode and see that it’s still unfolded.

The way I used to do it is have the boolean stored in the target MonoBehaviour - (the type whom I’m making the editor for) - but that’s just not clean, why does that MB have to store something, only its editor needs? it doesn’t make sense.

Here’s what my editor looks like:

21510-fsm.png

If knowing any code would help you, let me know.

Thanks.

You need to store the data somewhere persistent. It works in the MonoBehaviour because Unity serializes it and recreates it after the code reload. Other than that, you could use EditorPrefs to store your editor-only data, but if you want to store this attribute per instance (so the expanded/collapsed state is stored separately for different edited objects) then it will get tricky.