Is there a way to actually calculate the GetPreferredValues of a string, but taking into account that we have a maximum width size to render it?
so for example i plan to render the text “Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s” but I have a maximum amount of width space (say 360 px) and infinite space in vertical
when I use GetPreferredValues for that text, with my font and settings it returns vextor2(2359.69995, 310.829987), so how I can restrict the horizontal space that we have when calculating the size, and that this horizontal space automatically calculates the vertical size?
In the latest preview release of the TMP package, I did modify the GetPreferredValues(string text, float width, float height) function where it is not restricted by the width.
Using the text you provided, the preferred values for a width of 360 using the default LiberationSans SDF would be (340.3, 330.0). The 340.3 corresponds to the longest line and 330 the height.
The latest preview release for Unity 2018.4 is version 1.6.0-preview.1, version 2.2.0-preview.1 for Unity 2019.4 and version 3.2.0-pre.1 for Unity 2020.x or newer.
I’ve just tried the 3.2.0-pre.1 package, but it doesn’t seem to have fixed my problem, which looks similar to unity_sergigil’s. It’s possible I’ve misunderstood the intent of GetPreferredValues. I have a string which ends up about 960px long, I want to call text.GetPreferredValues(900,0) and have the returned values be a width <= 900, and the height be enough for two lines.
In 3.0.6, word wrapping is just turned off when calculating preferred sizes (when GetPreferredWidth(Vector2) calls CalculatePreferredValues). In 3.2.0, the only values that will be passed in are TextWrappingModes.NoWrap or TextWrappingModes.PreserveWhitespaceNoWrap.
If I manually change GetPreferredWidth’s call to CalculatePreferredValues to pass TextWrappingModes.Normal, the method does what I expect when called with the above values.
I’ve spotted that GetPreferredValues(string, float, float) calls a version of GetPreferredWidth which takes the wrapping mode and uses it directly, so I suppose I could use that rather than GetPreferredValues(float, float).
As I say, I may have misunderstood what GetPreferredValues is trying to achieve (I’m not sure when the height value passed in actually ever has an impact on the result), but what I’m looking for is a function that will wrap the text to meet a max width constraint. If there’s something else that does that, I’d love to know!
(In other news, using 3.2.0 the glyphs are all just solid blocks, although they have the correct dimensions, so I can verify the wrapping is working. Rebuilding the font assets doesn’t seem to help.)
Import the updated TMP Essential Resources which includes updated shaders.
Why?
In 3.2.0, I had to change the UV layout for compatibility reasons with SRP. In previous releases, UV0 was a Vector2 which contained the coordinates of the glyphs in the atlas texture. UV2 contained the encoded coordinates of the face or outline texture in the X coordinate and the SDF Scale in the Y. In the previous versions of the shaders, the encoded data was unpacked in the vertex shader which is no longer possible / supported in ShaderGraph / SRP.
In 3.2.0, UV0 is now a Vector4 where X, Y are still the coordinates of the glyphs in the atlas texture. Z is the index of the atlas texture and W now contains the SDF Scale. UV2 remains a Vector2 where X and Y are the face and outline texture coordinates (no longer encoded).
The updated shaders in the TMP Essential Resources read the SDF Scale in UV0.w.
For those looking for a solution to this, it seems that the fix is ONLY on the overload of GetPreferredValues that takes text as an input. So use that one!