Search Unity

Deeply copy one a Visual Element From One Window To Another

Discussion in 'UI Toolkit' started by BinaryCats, Jan 23, 2020.

  1. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    Hello,

    Currently with unity, If you want to cancel a closing window the only option (to my knowledge) is to re-open the window.

    With IMGUI that wasn't much of a problem, as each part of the UI required some data to be stored about it. For example, If a button was disabled you would have a bool defining whether or not the button should draw in the enabled or disabled states.

    With RMGUI, this isn't necessary anymore because you can change the state with events and there is no need to track that data anymore (obviously you could those variables back in, but they would likely only be used in this edge case)

    I have a Visual Element, Which has several levels of nesting of child elements. This Visual Element controls the drawing of one data type. When the user does a number of actions, classes are added/removed; controlling the style of the VE and its children.

    When the user comes to close the Utility Window, I prompt the user if they have unsaved changes. As standard practice, there is a cancel button which "stops" the closing of the window.

    As Unity doesn't let you stop the closing of the window I need to re-create the window, and its the styling of the Visual Elements that has made me shriek, not unlike an infomercial, "There has to be a better way!"
    Currently I have to go through each VE and its children, and copy the class list from the old window to the new one.

    Is there a simpler way of doing this?

    Thanks in advance
     
  2. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    If you could serialize it back to uxml you can then deserialize it into a new window.
     
  3. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    Is this a thing? How would I go about serializing the current state of the uxml?
     
  4. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    While it is more work, I would advice against storing application state inside your UI (especially as part of styles). Same way you stored important persisted information outside the UI in IMGUI, you should continue to do so with UIElements.

    Now, some things do make sense to store in the UI but persist across Domain Reloads. For this we have the View Data feature. If you give a ScrollView.viewDataKey a name, it will remember its scroll position the next time you reload the window. Unfortunately, enabling this feature for a custom C# control is not currently public. It may be possible through reflection but it would be tricky. That said, this feature would still not persist things like your current style class list on an element.

    So, two options:

    1. Pretend you're still using IMGUI and store all values that need to persist in the EditorWindow as serializables. Then, in OnEnable(), recreate the UI by refererring to these saved fields.

    2. For this specific case, and if your UI does not reference anything from the current EditorWindow, you might be able to de-parent it from the rootVisualElement, put it in a ScriptableObject that does not get unloaded, then take it out and parent it to the new EditorWindow's rootVisualElement. The full UI hierarchy, with all child elements, should stay intact, but there's still a lot that can go wrong. This is just an idea. I'm not really recommending this.
     
    BinaryCats likes this.
  5. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    Thanks for the detailed reply. I opted to store the state of the UI in varibles in the custom element, I think it highlights that these sub-elements in my custom element could be their own custom element (hope that makes sense), storing their state would be much cleaner then

    I was hoping you would reply "oh don't worry there is this undocumented feature that allows you to halt a closing window) but hey ho :)
     
  6. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    We might have such a feature in the future. No promises. Just know that we internally feel the same pain you do on this. :)
     
    BinaryCats likes this.