View data persistence with ObjectField

I have set the “view data key” of my ObjectField. On reinstantiating the editor window, the ObjectField was not preserved visually however when I click on it, it pings the correct previously assigned object.

So it seems that view data persistence works, how to make it visually assigned on instantiating the UI the next time?

This looks like a bug. Can you please report it using the Report a Bug tool in the Help menu?

That said, view data persistence is really only meant for view state, not program/tool state, for example: scroll position, foldout expansion state. For remembering the value of a data field, I would use the EditorWindow’s own serialized state. So, use a [SerializeField] field in your EditorWindow to remember to current object selection and restore it in the OnEnable() function (or where you recreate your UI). If you want to remember this longer than the window’s life, use an asset.

1 Like

This is still an issue. More over, i get the same problem with vector2 fields. They show 0,0 in UI, but the values are not 0,0 when i check them in the code.

Was this not fixed since 2019?

Unity 2020.3.8f1

Hi. I was able to reproduce this issue using a Vector2 field. I just created a ticket for this and I’ll post the link to track it whenever is available.

Thank you.

1 Like

Thanks. Can you inform me on the state of the object field? i have the same effect(bug) as with vector2 fields.

I open the editor window, set an asset reference into the object field, close the editor window, open it again and the field shows it’s empty, but when i click on it, it highlights the asset and if i check it from code, the value is not null.

It seems that it is bugged as described in the opening post, or am i doing something wrong with it?

If you can’t reproduce it, i can share my files.

It should work. I’ll update the ticket to include object fields too. You can track the issue here: Unity Issue Tracker - Vector2Field value doesn't get displayed when using View Data Key

Thanks!