Search Unity

Question How to make UI change position depend on other UI element without making it as the child

Discussion in 'UGUI & TextMesh Pro' started by ArikaHoshino, Dec 29, 2022.

  1. ArikaHoshino

    ArikaHoshino

    Joined:
    Sep 14, 2020
    Posts:
    32
    upload_2022-12-29_15-23-12.png

    Hi,

    The “PlayerChoiceRoot” is the lower box and has “Vertial Layout Group” and “Content Size Filter” so I cannot set the upper box “ConversentGroup” to its child and set anchor. Is there a way to make the upper box dynamicly moving as the size of lower box expand or shrink WITHOUT script?

    Thank you!
     
  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    602
    Two potential solutions:

    a) set the upperBox as child of lower box, but add Layout element with "Ignore Layout" enabled. This way you can use the anchor aproach even though the bottom box has vertical layout.

    b) Replace content size fitter with layout behavior: (I don't promise that I listed everything necesarry to get this working)
    * Add parent box with vertical layout and Control Child Size: Height
    * place PlayerChoiceRoot and Conversation group as child of parent box
    * remove ContentSizeFitter from PlayerChoiceRoot (can't have contentSize fitter for objects inside layout)
    * make sure you don't have "Child force Expand" enabled for either of layouts

    From what I understand it will work because parent layout with "control child size" will set the size of child layout (PlayerChoiceRoot) to it's desired size which is calculated based on the content of PlayerChoiceRoot.

    Right combination of layouts and layout element componets can produce similar effect as content size fitter, but it's a bit trickier to setup since there more moving parts involved, sometimes you have to change the settings of parent layout instead of the layout which is misbehaving, and if you have it wrong it will break your layout.