Wednesday, July 27, 2022
HomeGame Developmentunity - Getting textual content dimensions with TextGenerator.GetPreferredHeight/Width not working

unity – Getting textual content dimensions with TextGenerator.GetPreferredHeight/Width not working


I’m utilizing Unity 2021.3.3f1 and making an attempt to make a pop up with a sprite as a background, the place the dimensions of this sprite adjusts to the dimensions obligatory to suit the textual content.

From what I perceive, UnityEngine.UI.TextGenerator ought to have the ability to give me the mandatory dimensions of the sprite, given an enter string and a few TextGenerationSettings.

Right here is the code I’m utilizing.

public class PopUp : MonoBehaviour
{
    UnityEngine.UI.Textual content textual content;
    TextGenerationSettings textGenSetting;
    TextGenerator generator


    void Begin()
    {
        generator = new TextGenerator();
        textGenSetting.generationExtents = new Vector2(20f, 20f);
    }

    void Replace()
    {
        RecalculateText();
    }

    non-public void RecalculateText()
    {
        textGenSetting.fontSize = textual content.fontSize;
        textGenSetting.alignByGeometry = textual content.alignByGeometry;
        textGenSetting.colour = textual content.colour;
        textGenSetting.fontStyle = textual content.fontStyle;
        textGenSetting.resizeTextForBestFit = textual content.resizeTextForBestFit;
        textGenSetting.resizeTextMaxSize = textual content.resizeTextMaxSize;
        textGenSetting.resizeTextMinSize = textual content.resizeTextMinSize;
        textGenSetting.richText = textual content.supportRichText;
        textGenSetting.horizontalOverflow = textual content.horizontalOverflow;
        textGenSetting.verticalOverflow = textual content.verticalOverflow;
        textGenSetting.updateBounds = true;
        textGenSetting.font = textual content.font;
        textGenSetting.lineSpacing = textual content.lineSpacing;
        
 
        var newHeight = generator.GetPreferredHeight(textual content.textual content, textGenSetting);
        var newWidth = generator.GetPreferredWidth(textual content.textual content, textGenSetting);
        var newRect = generator.rectExtents;

        //Debug Log 1
        Debug.Log(string.Format("newWidth: {0}, newHeight: {1}, numLinesGenerated: {2}", newWidth, newHeight, generator.lineCount));
        
        //Debug Log 2
        Debug.Log(string.Format("newRectWidth: {0}, newRectHeight: {1}, numLinesGenerated: {2}", newRect.width, newRect.peak, generator.lineCount));
        
        //Debug Log 3
        Debug.Log(string.Format("generationExtents: width {0}, peak: {1}, numLinesGenerated: {2}", textGenSetting.generationExtents.x, textGenSetting.generationExtents.y, generator.lineCount));
    }
}

Since I am unsure which technique/discipline ought to be giving me the proper peak and width for any given textual content, I am printing a number of fields/technique outcomes to the console.

Nevertheless, none of these offers the specified end result.


Outputs of Debug Log 1

The road marked //Debug Log 1 above offers the next output when I’ve an empty string (“”), a string with one character (“a”), a string with two characters (“aa”), and an extended string (with loads of areas in between for potential line breaks)

Textual content is : . newWidth: 0, newHeight: 15, numLinesGenerated: 1

Textual content is : a. newWidth: 7, newHeight: 30, numLinesGenerated: 1

Textual content is : aa. newWidth: 14, newHeight: 45, numLinesGenerated: 1

Textual content is : Nam a tempor lorem. Ut eleifend, massa sed tincidunt blandit, risus nunc pharetra orci, at mattis turpis augue eu eros. Aliquam a faucibus dolor. Vestibulum ultrices turpis quis mi aliquam, a facilisis metus rutrum. Nulla sodales, ante vitae maximus fringilla, tellus erat ultricies felis, ac sodales felis lorem nec lorem. Fusce cursus turpis odio, vel tristique justo pulvinar sed. Nunc efficitur, ante ac mollis pretium, elit felis elementum lorem, vitae porttitor ex arcu vulputate ante. Donec elit purus, porta nec tortor vitae, semper suscipit sem. Ut ante dui, lobortis vel nulla vitae, condimentum laoreet magna. Maecenas aliquet aliquet nulla a fringilla. Proin rhoncus est eget nunc venenatis tempus. Praesent rhoncus est et velit gravida, non posuere justo scelerisque. Praesent ultrices lectus quis quam iaculis faucibus quis id sem. Quisque enim massa, molestie ac tortor sit amet, porta tristique diam. Suspendisse potenti. Fusce rutrum massa metus, eu facilisis nisi luctus vel.. newWidth: 5634, newHeight: 12675, numLinesGenerated: 1

Outputs of Debug Log 2

This is the identical for //Debug Log 2

Textual content is: . newRectWidth: 0, newRectHeight: 15, numLinesGenerated: 1

Textual content is: a. newRectWidth: 7, newRectHeight: 15, numLinesGenerated: 1

Textual content is: aa. newRectWidth: 14, newRectHeight: 15, numLinesGenerated: 1

Textual content is: Nam a tempor lorem. Ut eleifend, massa sed tincidunt blandit, risus nunc pharetra orci, at mattis turpis augue eu eros. Aliquam a faucibus dolor. Vestibulum ultrices turpis quis mi aliquam, a facilisis metus rutrum. Nulla sodales, ante vitae maximus fringilla, tellus erat ultricies felis, ac sodales felis lorem nec lorem. Fusce cursus turpis odio, vel tristique justo pulvinar sed. Nunc efficitur, ante ac mollis pretium, elit felis elementum lorem, vitae porttitor ex arcu vulputate ante. Donec elit purus, porta nec tortor vitae, semper suscipit sem. Ut ante dui, lobortis vel nulla vitae, condimentum laoreet magna. Maecenas aliquet aliquet nulla a fringilla. Proin rhoncus est eget nunc venenatis tempus. Praesent rhoncus est et velit gravida, non posuere justo scelerisque. Praesent ultrices lectus quis quam iaculis faucibus quis id sem. Quisque enim massa, molestie ac tortor sit amet, porta tristique diam. Suspendisse potenti. Fusce rutrum massa metus, eu facilisis nisi luctus vel.. newRectWidth: 5634, newRectHeight: 15, numLinesGenerated: 1

Outputs of Debug Log 3

And right here for Debug Log 3

Textual content is: . generationExtents: width 20, peak: 20, numLinesGenerated: 1

Textual content is: a. generationExtents: width 20, peak: 20, numLinesGenerated: 1

Textual content is: aa. generationExtents: width 20, peak: 20, numLinesGenerated: 1

Textual content is: Nam a tempor lorem. Ut eleifend, massa sed tincidunt blandit, risus nunc pharetra orci, at mattis turpis augue eu eros. Aliquam a faucibus dolor. Vestibulum ultrices turpis quis mi aliquam, a facilisis metus rutrum. Nulla sodales, ante vitae maximus fringilla, tellus erat ultricies felis, ac sodales felis lorem nec lorem. Fusce cursus turpis odio, vel tristique justo pulvinar sed. Nunc efficitur, ante ac mollis pretium, elit felis elementum lorem, vitae porttitor ex arcu vulputate ante. Donec elit purus, porta nec tortor vitae, semper suscipit sem. Ut ante dui, lobortis vel nulla vitae, condimentum laoreet magna. Maecenas aliquet aliquet nulla a fringilla. Proin rhoncus est eget nunc venenatis tempus. Praesent rhoncus est et velit gravida, non posuere justo scelerisque. Praesent ultrices lectus quis quam iaculis faucibus quis id sem. Quisque enim massa, molestie ac tortor sit amet, porta tristique diam. Suspendisse potenti. Fusce rutrum massa metus, eu facilisis nisi luctus vel.. generationExtents: width 20, peak: 20, numLinesGenerated: 1

Conclusion

Debug Log 1

generator.GetPreferredWidth provides 7 to the popular width for every character (and 4 for every area) and generator.GetPreferredHeight provides 15 to the popular peak for every character (no concept why) and neither GetPreferredWidth nor GetPreferredHeight calculate line Breaks in any smart approach

Debug Log 2

generator.rectExtents.width will get incremente by 7 for every character (and 4 for every area), however generator.rectExtents.peak is just not incremented in any respect (so once more, no line breaks or legitimate peak readings)

Debug Log 3

This (most likely anticipated) at all times outputs 20,20 (i.e., no matter textGenSetting.generationExtents is about to). I attempted this anyway since I believed possibly generator.Populate() would replace this worth of the textGenSetting struct.

Query: Am I lacking one thing? Or is that this not directly anticipated behaviour? How can I get this to work, or, alternatively, use another solution to auto-layout textual content and get the extents of mentioned textual content in order that I can modify my sprites to suit this textual content?

Listed here are my textual content settings, for full data.

enter image description here

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments