Delay problem of Unity Text length calculation

Foreword: How do you do the automatic line wrapping     of text and the adaptation of picture text in Unity ?

    Today, there is a requirement in the game project. I want each sub-task box of the task to be dynamically modified according to the text of the task description. For this requirement, I have two solutions:
    1. Use Unity's layout component Vertical Layout Group to control it together with ContentSizeFitter. This method is easy and labor-saving, but it is not easy to expand for some special needs.
    2. Calculate the size according to the content, and get the total number of lines according to the number of words in each line. Then set the sizeDelta of the text box and the sizeDelta of the corresponding background box. This kind of scalability is much better, but it needs to analyze and control the length and width by yourself, which is more troublesome.

    I used the first method to do this that I encountered today, but the first method is not suitable for doing other operations when the adaptation is satisfied, because we usually do the operation immediately after the assignment, And when we assign a value to the text of Text, we will not get the real text size immediately (guess that the value of text is assigned in this frame, but the preferredWidth is not immediately calculated and changed, it may be calculated in the next frame, and this When the layout component is not laid out for the real size, so the size of the current frame may be wrong). And after assigning a value here, I still need to get the size of this Element, so what I get is wrong.

    The idea I came up with at the beginning was to use coroutines, and do operations when the correct Element size is obtained in the next frame. Players should not be able to see it in one frame. But the coroutine also has several problems:
    1. A thread is opened every time the coroutine runs, which is very expensive.
    2. You can't perfectly control the execution after one frame. If the time is set too long, it is too obvious. If it is too short, it is limited by the data that the machine may get or wrong data, which is not very safe. (The size of the Text may not be correct after one frame).
    3. The code is difficult to understand and difficult to maintain in the future.

    After my colleagues and I thought about it, we decided to start with the layout component that controls the size. We found that ContentSizeFitter has a callback function when changing the size. With this, we can write a class to inherit the ContentSizeFitter class, rewrite the method of the parent class, and add a delegate to the subclass. When we change the Element When you need the correct size of the Element to do other things, you can assign a value to this delegate to let it execute. In this way, the scalability of this layout component is much better, and players can customize it freely. Actually verified it, it is still relatively easy to use.

ContentSizeFitter changes the size of the callback function

insert image description here

Overridden subclass of CustomContentSizeFitter

insert image description here

The actual effect of the game is the effect after changing the text on the right (additional operations have also been successfully completed)

insert image description hereinsert image description here

Element's settings interface

insert image description here

Element sub-object text setting

insert image description here

Digression

    When I was doing chat adaptation in the past, I needed to adapt the background of the title according to the title of each speaking player, and adapt the size of the chat box according to the text sent. At that time, I used the second method. I calculated the size in real time and then manually changed the sizeDelta of the picture and text box, but that size was also problematic. In order to catch up with the progress chart, I directly used the coroutine, so the player was speaking In the short gap after that, you can see that the background image of your title is dynamically changed. The experience is not very good, but it barely meets the needs of planning. People always come up with solutions, but now that I think about it, I still can’t let myself go for convenience. If I let myself go, I will miss an opportunity to learn!

Guess you like

Origin blog.csdn.net/l1606468155/article/details/103994985