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

Move VisualElement Child to front

Discussion in 'UI Toolkit' started by DukeGrimm, Jan 22, 2021.

  1. DukeGrimm

    DukeGrimm

    Joined:
    Sep 29, 2012
    Posts:
    61
    I have a container Visual element using Templates that can be dragged around.
    Dragging works, however, they are layered as they are in the UXML Hierarchy.

    How would i pull a element to the top to be fully visible when clicked.

    I Found "BringToFront" on VisualElement, but doesn't seem to do anything

    Any suggestions on reordering the children of a Parent Element from the context of one of the children?

    Thanks!

    Running 2020.2.0b11, Ui Toolkit/Builder 1.0-preview.13
     
  2. DukeGrimm

    DukeGrimm

    Joined:
    Sep 29, 2012
    Posts:
    61
    So I figured out a few things. BringToFront is the correct method to use to bring a UI element in front of it's siblings.
    There are a few caveats though.

    When using uxml templates remember that the template gets wrapped by a VisualElement. So when you want to bringToFront, you need to run it on this.parent rather than this.BringToFront.

    The second issue I ran into was with things bouncing around. Couldn't figure out what the issue was. Then it occurred to me that the template container was set to position relative rather than absolute. Switching to absolute resolved that issue.

    Now when a template is clicked on, it is brought to front of other overlapping sibling elements, and it's position remains the same.
     
    Deimz and Midiphony like this.
  3. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    515
    In case someone else has the same problem, I'd like to add that parent.bringToFront(child) will modify the order of the visual elements inside the parent, so the relative layout will effectively be affected. You can use the UI debugger to see what is changed in the hierarchy when calling the method to get more insight.
     
  4. Deimz

    Deimz

    Joined:
    Mar 18, 2013
    Posts:
    1
    Thanks a lot Bro, you saved me a lot of time, I am also working on modal system and got the same probleme with relative position, just put absolute position in my template and now it's working like a charm :)