Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to persist UI-specific state with viewDataKey?

Discussion in 'UI Toolkit' started by Pelican_7, Apr 9, 2019.

  1. Pelican_7

    Pelican_7

    Joined:
    Nov 25, 2014
    Posts:
    190
    Hi,

    I'm trying to use the viewDataKey field of VisualElement to persist some UI-specific state (an element's position) across domain reloads.

    I have a custom visual element instantiated in UXML, defined as below. The element has a pan manipulator (which moves the element). My visual element subclass updates a local 'position' field each time a pan is completed, which is what I'm hoping will get serialized/persisted. I can then initialize the element to this serialized position field to maintain its position across domain reloads.
    Code (CSharp):
    1. public class MyContentView : VisualElement
    2. {
    3.     private const string ViewDataKey = "3dbca16b-29fd-4ba8-97af-68f47265c2a1";
    4.  
    5.     [SerializeField] private Vector2 position;
    6.  
    7.     public MyContentView()
    8.     {
    9.         var panManipulator = new PanManipulator(MouseButton.MiddleMouse);
    10.         panManipulator.OnPannedToPosition += OnPannedToPosition;
    11.         this.AddManipulator(panManipulator);
    12.  
    13.         this.viewDataKey = ViewDataKey;
    14.     }
    15.  
    16.     private void OnPannedToPosition(Vector2 position, PanManipulator.PanState state)
    17.     {
    18.         if (state == PanManipulator.PanState.Ended)
    19.         {
    20.             this.position = position;
    21.         }
    22.     }
    23.  
    24.     public class Factory : UxmlFactory<MyContentView> { }
    25. }
    However, the position field of MyContentView is always Vector2.zero after a domain reload. The documentation states that:
    ...so I'm not entirely sure what else I need to configure here. Any help is much appreciated.

    -andy.

    (Unity Version: 2019.1.0b10)
     
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    Unfortunately, the view data API currently only works with some controls that come with UIE (hence the comment on "element that supports it"). It is not extensible at this time. The APIs to enable view data persistence on your own custom controls is internal.
     
    wang37921 likes this.
  3. Pelican_7

    Pelican_7

    Joined:
    Nov 25, 2014
    Posts:
    190
    Thanks a lot for your quick reply @uDamian. Ahh I see, thank you for the info. For now then I will store my UI-specific data on the EditorWindow itself (so it gets serialized) and pass a reference to this data store to the VisualElements that require it.
     
  4. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
    Are there any plans to support custom controls that have state?
     
  5. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    The lack of public access to the APIs I mention above makes it not possible to add persistence support for your custom APIs. We will make them public eventually.