Search Unity

Text Fields Background

Discussion in 'UI Toolkit' started by piggybank1974, Apr 11, 2021.

  1. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    I'm trying to figure out when the Text Fields background does not change its colour, even if I change it in the editor, it always stays that off white colour.
     
    Mj-Kkaya and Nexer8 like this.
  2. Smolty

    Smolty

    Joined:
    Nov 8, 2016
    Posts:
    17
    Hi,

    in order to change the color of the input field of the TextField, you need to change the background color of the TextInput element inside the TextField.

    Because the TextInput element is grayed out, you can't change it directly but you can change it using a selector in your Stylesheet.

    You can just use
    Code (CSharp):
    1. TextInput
    as a selector and change the background color there or you can alternativly use a child selector like this
    Code (CSharp):
    1. TextField > TextInput
    to be more specific.

    Haven't tried it at runtime, but it works in the Editor.
     
  3. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    @Dipity

    Thanks for the reply, I also thought that myself as I could see it was a node "container", but it's uneditable e.g everything is grey out event the unity-text-input field itself is grey out under the node, but actually the background is a dark grey colour but as I said uneditable.
     
  4. sebastiend-unity

    sebastiend-unity

    Unity Technologies

    Joined:
    Nov 9, 2015
    Posts:
    184
    Dipity is correct. If you are using the UI Builder:

    1. In your UI document, create a style sheet
    2. Add a new selector "TextField > TextInput" to that style sheet.
    3. Click and drag that new selector onto your TextField in the hierarchy of the UI document
    4. Click on the selector to make sure that the Style Inspector displays the selector properties.
    5. In the Style Inspector, go into the Background section and change the Color property. You should see the reflected change in the viewport.
     
  5. M_R_M

    M_R_M

    Joined:
    Jan 31, 2019
    Posts:
    26
    I appreciate the work and the intent going into UI Builder, but this is so insanely unintuitive and clumsy. I really hope this entire workflow is significantly streamlined and improved, because if this is the process for doing something as basic as changing a background color of a text field, there's no point in using it. I am spending very important development time just trying to figure out how to stop a text field from covering up a progress bar, and even that is just because I gave up trying to figure out why stylesheets do nothing to the elements they are attached to. I'm having flashbacks of working web development, except instead of stuff working when you tell it to, it does nothing.

    Yes, this is a preview package, but I expected some really basic functionality that is simply not functioning.

    Why are stylesheets not working on my elements? Why does the text field/template container whatever cover up the entire progress bar? Why can't you just make the greyed out elements in the parent functional without creating a new stylesheet (that doesn't work anyway)? Why are those elements not editable when these library controls are added in the first place? Did someone actually think that we didn't want to edit those elements by default?

    So many decisions that I have a lot of questions about, and at this point my confidence in this being a viable UI solution in the next 40 years is very low.

    I just don't get why everything little thing with Unity has to be such a fight. It's exhausting, and this was just another wasted day with nothing getting done in what should have been a simple process.
     
  6. M_R_M

    M_R_M

    Joined:
    Jan 31, 2019
    Posts:
    26
    Christ, I can't even edit a comment on the post I just made.

    Like I tried to say anyway, every little thing with Unity is a fight.
     
  7. gareth_untether

    gareth_untether

    Joined:
    Jan 5, 2018
    Posts:
    69
    Here's the code to be added to the .uss file to target the background of the TextField:

    Code (CSharp):
    1. TextField > TextInput {
    2.     background-color: rgba(255, 0, 0, 255);
    3.     border-width: 0;
    4. }
     
    tosydeveloper and Foge like this.