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.
public class MyContentView : VisualElement
{
private const string ViewDataKey = "3dbca16b-29fd-4ba8-97af-68f47265c2a1";
[SerializeField] private Vector2 position;
public MyContentView()
{
var panManipulator = new PanManipulator(MouseButton.MiddleMouse);
panManipulator.OnPannedToPosition += OnPannedToPosition;
this.AddManipulator(panManipulator);
this.viewDataKey = ViewDataKey;
}
private void OnPannedToPosition(Vector2 position, PanManipulator.PanState state)
{
if (state == PanManipulator.PanState.Ended)
{
this.position = position;
}
}
public class Factory : UxmlFactory<MyContentView> { }
}
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)