Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Public equivalent for DoMeasure?

Discussion in 'UI Toolkit' started by Arkenhammer, Sep 26, 2020.

  1. Arkenhammer

    Arkenhammer

    Joined:
    Nov 10, 2019
    Posts:
    51
    I'm trying to build a list control where I animate existing elements to make space when inserting a new element. The trick here is that my elements are variable sized so I need to measure the height of the new element before I insert it into the parent. It looks like that is the job VisualElement.DoMeasure() does, but it is marked as protected.Is there a correct way to do this or do I need to roll my own independent measurement system?
     
  2. Arkenhammer

    Arkenhammer

    Joined:
    Nov 10, 2019
    Posts:
    51
    So I found a way to do this:
    1. Set "position: absolute; opacity: 0;" on and the element I am adding to the list. That makes it both invisible and prevents it from affecting the layout.

    2. Insert the element in the correct place in the list.

    3. Delay one frame using schedule.Execute().ExecuteLater(). This gives the layout system time to resolve the styles and find the bounds of the element.

    4. ElementHeight = element.resovledStyle.marginTop + element.localBound.height + element.resolvedStyle.marginBottom.

    5. Perform the animation to make space given the now known height.

    6. OnCompleted() set the "position: relative; opacity: 1;"

    That's working for me.
     
    echekanskiy likes this.
  3. echekanskiy

    echekanskiy

    Joined:
    Aug 16, 2020
    Posts:
    16
    Hm, interesting solution. Do you think it will produce the same results if I want to measure element in one panel, and later put it to another panel? What I want - create element, measure its size based on layout and later create editor window with required size and move measured element here.
     
  4. Arkenhammer

    Arkenhammer

    Joined:
    Nov 10, 2019
    Posts:
    51
    In my case I have a variety of different elements I inserting into a list (this is a notification panel), some of which have text strings which will wrap to multiple lines. The goal is to measure of the height of the element when it is wrapped to fit inside the parent panel. What matters is that I insert the element into a parent which has the correct width so I can get the height and animate the other cells moving down as I push the new one on top.

    I think you can do what you want, you may need to think about how the test container affects the layout of your element. Changing the flex properties of the parent can change the the measurement of the child and you might need to tweak them a bit to get a good result.