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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Style - size problem

Discussion in 'UI Toolkit' started by max_gambit, Mar 18, 2022.

  1. max_gambit

    max_gambit

    Joined:
    Jan 10, 2020
    Posts:
    3
    I have image A and text field B(no label) in a container C.
    Let E be container C width - image A width
    Container C is in container D.
    Container C has some start height.
    Container C is filling container D in width(so container D is controlling width).
    Image A has const width.
    I want text field B:
    - Start near image A.
    - If text width is less than E width:
    -- Have minimal width(text field background with not filling out container C, just enough for text)
    -- Have stretched height(same height as container C)​
    - If text width is more than E width:
    -- Do not extend container C in width, create another line
    -- Possibly extend container C in height​

    I don't know how to do this. I can only do:
    1. Correct height, bad align(starts near right border of container C) and expands container C width when text is more than E width
    2. Correct height, correct align, doesn't expand container C width, but stretched in width and also doesn't expand container C height.​

    Current styles:
    Code (CSharp):
    1. styleAccess = containerC.style;
    2. styleAccess.flexDirection = FlexDirection.Row;
    3. styleAccess.minHeight = 40f;
    4. styleAccess.alignItems = Align.Center;
    Bad style 1:
    Code (CSharp):
    1. styleAccess = textFieldB.style;
    2. styleAccess.minHeight = 40f;
    3.  
    4. styleAccess.whiteSpace = WhiteSpace.Normal;
    5. styleAccess.alignSelf = Align.Stretch;
    6. styleAccess.flexWrap = Wrap.Wrap;
    Bad style 2:
    Code (CSharp):
    1. styleAccess = textFieldB.style;
    2. styleAccess.minHeight = 40f;
    3.  
    4. styleAccess.position = Position.Absolute;
    5. styleAccess.left = 20f;
    6. styleAccess.right = 0f;
    7.  
    8. styleAccess.whiteSpace = WhiteSpace.Normal;
    9. styleAccess.alignSelf = Align.Stretch;
    10. styleAccess.flexWrap = Wrap.Wrap;

    Anybody?
     
  2. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    521
    Hi, I read this post 3 or 4 times and still have issue visualizing the full pictures on Friday at 6pm.

    For now, I will just suggest a few parameters that could help you achieve this :
    • flex-direction: row-reverse
    • flex-wrap: wrap-reverse
    • justify-content: flex-start | flex-end | center | space-between | space-around

    You are already using alignSelf, alignItems: they are essential tool for what you are doing.

    I would also search on regular htlm/css solutions for this, they should be equivalent.

    The last option would be to use the geometry changed event to reorganize the layout.

    Unfortunately, we do not support the media queries that could help with these kinds of situation.
     
    max_gambit likes this.
  3. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Yes, set what you can in the style sheet or inline, then use geometry event to fix the rest, but use resolvedStyle rather than style to get the right values.
     
  4. max_gambit

    max_gambit

    Joined:
    Jan 10, 2020
    Posts:
    3
    Thanks for suggestions, I needed justify-content and rest I figured out with GeometryChangedEvent.
    Last obstacle I stumbled upon: I wanted to move the changes from code to uss file.
    But for some reason values are ignored :(

    Code (uss):
    1. .ContainerC
    2. {
    3.     min-height: 40px;
    4.     flex-direction: row;
    5.     align-items: center;
    6.     justify-content: flex-start; <-- this is for sure ignored i.e. :(
    7. }
    8.  
    9. .TextFieldB
    10. {
    11.     min-height: 40px; <-- this as well
    12.     white-space: normal;
    13.     align-self: stretch;
    14.     flex-wrap: wrap;
    15. }
     
  5. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    521
    The style will be overridden if there is a more specific selector, or if an inline style is set somewhere (can be in c#, on directly on the UXML of the element).
    You can use the UI debugger (window->UI Toolkit->Debugger) to see what is the resulting style applied to the element and the source (inherited, the applicable stylesheet, etc)

    Don't hesitate to share back any question you may have.