This happens in the Editor. I don’t know if it happens with Runtime UIs too.
So I upgraded a project from Unity 2022.3 to Unity 6 and some layouts got messed up. It turns out that now empty Label elements have no height. That’s not a huge problem, It can be handled.
But while I was investigating this, I found that extra spaces at the end get trimmed. If a label is only white spaces, it disappears. I have some labels with dynamic text and it’s very important to be able to notice those empty spaces.
This doesn’t happen in TextField’s own TextElement, so I’ve tried looking at the internal code to configure my Labels the same as the TextField’s TextElement, but it doesn’t work.
Does anybody have an idea for preventing Labels from trimming spaces?
Thank you very much. I really appreciate it.
I was able to do it by casting the Label to ITextEdition
and setting its isReadOnly
property to false. It doesn’t seem to turn it into an editable field or have other undesired effects at a glance, but it’s very ugly as a solution.
Ideally I think Labels should leave our white spaces alone. If people don’t want them there, people can remove them from their text by themselves. Or it’d be nice to at least have an option to disable this weird behavior.
EDIT
Turns out this can also be achieved by setting the style white-space
property to pre
. It’s a lot cleaner. The modes pre
and pre-wrap
are new in Unity 6. One may think to avoid pre
because, in css, it shows new lines at explicit line-breaks, but it turns out no white-space mode ignores explicit line-breaks in UITK Labels. So there’s no point on using no-wrap
, unless you need the space trimming behavior.
Well, thank you for your time, people from the future. I hope this helps you in some way.
Yes turning white-space
to preserve is the way to go. In previous version of Unity we were not systematic in what constituted a whitespace, in Unity 6 we did align with the web standards on this regard, and added the option to configure this.
1 Like