Search Unity

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

Discussion in 'UI Toolkit' started by bsterling250DI, Mar 27, 2020.

  1. bsterling250DI

    bsterling250DI

    Joined:
    Sep 25, 2014
    Posts:
    78
    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?
     
    tonytopper likes this.
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    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.
     
  3. bsterling250DI

    bsterling250DI

    Joined:
    Sep 25, 2014
    Posts:
    78
    ahh, thanks so much for clarifying. That does sound useful in some cases. Appreciate that. (that post would make a "good-start" for documentation.
     
  4. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
  5. awesomedata

    awesomedata

    Joined:
    Oct 8, 2014
    Posts:
    1,419
    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.)
     
    NickD3v and tonytopper like this.
  6. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    We're actively working on a rewrite of our UI Toolkit docs. I'll make sure to pass your feedback along. Thanks!
     
    awesomedata likes this.
  7. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    374
    Last edited: Mar 24, 2023
  8. jpvanoosten

    jpvanoosten

    Joined:
    Nov 20, 2009
    Posts:
    50
    It seems that the view data key does not work on a
    ScrollView
    at runtime?

    Unity 2021.2.12f1

    Can anyone else confirm this?
     
  9. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    Yes, this feature only works in the Editor.
     
  10. Dmitry-Pyalov

    Dmitry-Pyalov

    Joined:
    Dec 13, 2011
    Posts:
    125
    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?
     
    Novaze likes this.
  11. sramzel

    sramzel

    Joined:
    Mar 23, 2023
    Posts:
    1
    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.
    `
     
    s_marcell likes this.
  12. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    934
    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.
     
  13. andrianovyaroslav27

    andrianovyaroslav27

    Joined:
    Sep 19, 2021
    Posts:
    8
    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?
     
  14. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,166
    @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.

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