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

Inserting a UI element between two UI elements in horizontal layout group

Discussion in 'Scripting' started by Kalita2127, Aug 26, 2019.

  1. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    279
    So I want to insert a UI element between other two UI elements in horizontal layout group. It does works with switching the sibling index using
    SetSiblingIndex()
    but I'm thinking what if I have hundreds of UI elements. That would need hundreds switching between other UI element!
    Can I make use of LinkedList so I can add an element after a desired index? Or is there any good solution about this?
    Thank you!
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You can store your elements in a list and sort them as you deem appropriate and then set the sibling index on all of them in one go (whenever the order changes ideally), the canvas will dirty as a single unit and everything will be updated at the same time.