Can someone explain the "View Data Key" and it's uses?

Hello,

I see this “View Data Key” property on pretty much every object in UI Toolkit, but what is it for and how does it work?

1 Like

This key only really applies to ScrollView, ListView, and Foldout. If you give any of these a unique key (not enforced, but recommended), they will “magically” remember some of their internal view state. For ScrollView, it will remember scroll position between domain reloads and window close/reopens. For ListView, it will remember selection. And for Foldout, it will remember expanded state.

The reason this is on all elements is because this key is generated based on all the parents of an element. For example, if you make a custom control that contains a ScrollView inside, you need to give this child ScrollView a key so that its view data persistence (scroll position) is enabled (ex. “my-scroll-view”). However, if you had multiple instances of your custom control they would all have ScrollViews with the same key (functionally, they would all restore to the same scroll position instead of remembering their individual scroll positions). Therefore, when you use your custom control, you give each instance a unique view data key, say “instance1” and “instance2”, and then end up with these unique keys:

custom_control1 - view-data-key: “instance1”
ScrollView - view-data-key: “instance1__my-scroll-view”
custom_control2 - view-data-key: “instance2”
ScrollView - view-data-key: “instance2__my-scroll-view”

The composition of parents’ view data keys with your own is done automatically.

This API was not fully fleshed out before we went out of Experimental and so the APIs necessary to extend this feature to your own custom controls was made internal. We plan to open up the API in the future.

To clarify, this is a pure convenience API for those times were you’re getting sick of constantly re-expanding a Foldout every time you go into/out of Playmode. It does not affect the functionality of a control in any way.

4 Likes

ahh, thanks so much for clarifying. That does sound useful in some cases. Appreciate that. (that post would make a “good-start” for documentation.

We can always improve our docs but I think the existing docs on ViewData are not a bad start:

I think you could copy/paste your post into the docs and make it A LOT more clear. Your examples are much more useful than the existing ones. Unity documentation suffers from this lack of diverse examples constantly.

plzkthks!

(Great explanation though. Seriously.)

2 Likes

We’re actively working on a rewrite of our UI Toolkit docs. I’ll make sure to pass your feedback along. Thanks!

1 Like

@uDamian Awesome, always glad to hear docs improvements!

P.S. - Also thanks for your 2019 Unite Copenhagen talk with UIElements/UI Toolkit!

It seems that the view data key does not work on a ScrollView at runtime?

Unity 2021.2.12f1

Can anyone else confirm this?

Yes, this feature only works in the Editor.

Is there any way to run your own code after EditorWindow GUI state is restored from ViewData (e.g. on domain reload), so I can configure other state based on restored ViewData state?

1 Like

This applies to ObjectField too. At least for my case. If ‘ViewDataKey’ is set and I set RegisterCallback, I always get an event with the last object that was assigned to the field (I'm using it in an editor window). I was pulling my hair out trying to figure out why it kept triggering.

1 Like

How does it handle the case when we have multiple serialized properties/variables of the same type in a script?

In my case it doesn’t work properly, I have to toggle all the foldouts for the state to be updated which kind of makes it useless in most cases.

In my case, I had to create a custom inspector and assign each of the property fields a different key for it to work well for each field independently for each other.

This feature doesn’t seem to work when there are nested foldouts. In my custom editor I have 2 layers of foldouts at max, and it doesn’t remember the state of the root ones. Not a big issue for me, but is it the intended behaviour?

@andrianovyaroslav27 Have solved your problem? in my case child foldouts don’t remember state even all foldout has a unique name. TestItems foldout works fine.

9617603--1365170--upload_2024-2-1_13-3-36.png