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

Callback from ContentZoomer

Discussion in 'UI Toolkit' started by SteveSmithSoftware, Dec 4, 2020.

  1. SteveSmithSoftware

    SteveSmithSoftware

    Joined:
    Mar 23, 2014
    Posts:
    196
    Using UIElements GraphView and either SetZoom or adding the ContentZoomer manipulator manually there appears to be no way of getting a callback when the content zoom level is changed, idealy I would like to get a new referenceScale value so that I can save it and restore it on restart.

    Any other options?
     
  2. SteveSmithSoftware

    SteveSmithSoftware

    Joined:
    Mar 23, 2014
    Posts:
    196
    Figured it out

    SetupZoom(ContentZoomer.DefaultMinScale, ContentZoomer.DefaultMaxScale);
    this.viewTransformChanged = GraphTransformChanged;

    and

    void GraphTransformChanged(GraphView graphView)
    {
    Debug.Log(graphView.referenceScale);
    }
    and it was actually scale I wanted not referenceScale but np. I got something
     
    Last edited: Dec 8, 2020
  3. SteveSmithSoftware

    SteveSmithSoftware

    Joined:
    Mar 23, 2014
    Posts:
    196
    OK..
    Now I have my new scale factor back,
    The next question is how do I set the scale to this value (assuming I have saved it somewhere, which I have) the next time the GraphView is opened?

    I have tried to use a viewDataKey with the graphview in the hope that it would restore the zoom level but that does not appear to work, at least in 2019.4.

    Any other options?
     
    Last edited: Dec 8, 2020
  4. SteveSmithSoftware

    SteveSmithSoftware

    Joined:
    Mar 23, 2014
    Posts:
    196
    viewDataKey on a GraphView also seems not to work in 2020.1

    Anyone from Unity care to comment?
     
  5. SteveSmithSoftware

    SteveSmithSoftware

    Joined:
    Mar 23, 2014
    Posts:
    196
    Solved this bit

    you need to have a viewDataKey for every Element in the hierarchy all the way back to rootVisualElement

    wouldn't documentation be a wonderful thing?
     
  6. SteveSmithSoftware

    SteveSmithSoftware

    Joined:
    Mar 23, 2014
    Posts:
    196
    Life is never simple

    So in 2019 and 2020 I have zoom restoration working using viewDataKey.
    However...
    My positioning and sizing code, which works just fine without viewDataKey no longer works.

    I am presuming this is because the values stored by viewDataKey are being applied after I do my

    SetPosition()
    and
    style.width =
    style.height =

    Although it does seem strange that viewDataKey does not save/restore this data as I do for 2018 which is what I would expect making the above moot for 2019 and newer

    So 1 step forward, 2 steps back

    Any options?
     
  7. patrickf

    patrickf

    Unity Technologies

    Joined:
    Oct 24, 2016
    Posts:
    57
    Hi, have you tried scheduling your positioning code for later execution?

    rootVisualElement.schedule.Execute(() =>
    {
    // Your positioning code
    }).ExecuteLater(0);
     
  8. stevphie123

    stevphie123

    Joined:
    Mar 24, 2021
    Posts:
    81
    you can do this the easy way via UpdateViewTransform(viewTransformPos, viewTransformScale)

    Just save the pos or scale and load it back