🤑 Indie game store🙌 Free games😂 Fun games😨 Horror games
👷 Game development🎨 Assets📚 Comics
🎉 Sales🎁 Bundles

[Solved!] Both shrink to fit and wrapping?

A topic by nospoone created Jul 25, 2017 Views: 475 Replies: 11
Viewing posts 1 to 12
(Edited 1 time)

Hi! First of all, thanks for STM - it saved me hours. :D

I am using the uGUI flavor of STM. I am trying to do dialog textboxes where my frame shrinks if the text is small enough (in length), or grows in height once it reaches a maximum width and makes the text wrap. I hope that makes sense.

I can achieve this relatively easily with uGUI by having this setup (which consists of Horizontal Layouts and Content Size Fitters) but I noticed that STM wasn't actually getting its values driven by these components.

That's fine, I thought, I'll just implement the wrapping myself. I have it working, except for one last detail. Here's how I am changing my text:

private void OnDialoguerTextPhase(DialoguerTextData data) {
    RectTransform dialogBoxTexTransform = (RectTransform) DialogBoxText.gameObject.transform;
    DialogBoxText.wrapText = false;
    DialogBoxText.Text = LocalizationManager.GetTermTranslation(data.text);
    if (dialogBoxTexTransform.sizeDelta.x > 500) {
        DialogBoxText.wrapText = true;
        dialogBoxTexTransform.sizeDelta = new Vector2(500, DialogBoxText.textMesh.bounds.size.y);

The problem here is that DialogBoxText.textMesh.bounds  seems to be set to the old bounds, even if I call Rebuild(). It gets updated after two calls to that method.

I've looked through the docs and couldn't find anything of help there... Maybe looping through the characters and identifying the actual mesh size? Do you have any advice on how to make this work?

Thanks again for an awesome asset! :D

To clarify, here's a small and big one side to side:

Ideally, I feed text in and the frame adjusts itself to the size.


Hey! This should be doable, I know other users have used the text mesh's bounds to get the right size, so I'll look into why that isn't updating. The textMesh variable there is the actual mesh used by STM.

That said, I know that accessing a variable from a mesh directly can cause garbage, so I have some variables in STM for getting bounds data (which I use for drawing the blue outline, in-editor.

I finally updated the docs to the latest version here, as I changed some bounds-related stuff since. http://supertextmesh.com/docs/SuperTextMesh.html

Check out the "topLeftBounds", "topRightBounds" etc variables in there, see if those work for you?

Thanks for the tip. I'll try 'em out, I didn't think of them because they said

for non-UI text

However the changelog said they did - I'll try and report back!

Developer (Edited 2 times)

I've been away from updates for so long, forgive me if I've forgotten stuff about my own asset!

Is it possible to just use the size of the recttransform, itself?

I've been away from updates for so long, forgive me if I've forgotten stuff about my own asset!

No problem whatsoever!

Is it possible to just use the size of the recttransform, itself?

Yes! The problem I had with this, however, is that it stops being updated if/when wrapping is enabled.

(Edited 1 time)

Hmm. Using bounds like so:

Vector3 size = DialogBoxText.topLeftBounds - DialogBoxText.bottomRightBounds;

yields correct height, but incorrect width:

I'll try using a VertexMod to get the correct bounds, to see if that works out...


Looking thru STM's code right now, try messing with the boolean on line 2510? It tells the mesh to only resize the rect when autoWrap is off. I forget why I have that in place, so hopefully it doesn't break everything else?

(Edited 3 times)
<img src="<a href="https://img.itch.zone/aW1nLzc0MzI5Ny5wbmc=/original/sodrsd.png">https://img.itch.zone/aW1nLzc0MzI5Ny5wbmc=/original/sodrsd.png</a>">RebuildTextInfo

Whoa! Removing that, combined with changing my setting code to:

private void OnDialoguerTextPhase(DialoguerTextData data) {
   RectTransform dialogBoxTextTransform = (RectTransform) DialogBoxText.gameObject.transform;
   DialogBoxText.text = LocalizationManager.GetTermTranslation(data.text);
   DialogFrame.sizeDelta = dialogBoxTextTransform.sizeDelta;

Makes it fit the text. However, how do I define how far it can go before it wraps?


Upon further inspection it seems like it makes it fit the longest word? 🤔


Edit 1

Looking at SuperTextMesh.cs:

if(uiMode && wrapText) return (float)((RectTransform)t).rect.width; //get wrap limit, within left and right bounds!

Seems like it's not respecting it in some way? Looking further...


Edit 2

Seems like AutoWrap gets reset somehow? Calling the code above calls RebuildTextInfo() three times with different AutoSize values:


Edit 3

Well, since SetMesh() overwrites the sizeDelta of the RectTransform, it changes the value to the new one (shrunk text), making it impossible to expand it to what it was set initially.

rect.sizeDelta = new Vector2(textMesh.bounds.size.x, textMesh.bounds.size.y);

Based on the information above, I was able to make this work by changing the AutoWrap property to this:

private float AutoWrap{ //get autowrap limit OR ui bounds
      if(uiMode && wrapText) return initialUIAutoWrap; //get wrap limit, within left and right bounds!
      return autoWrap;
private float initialUIAutoWrap = 0;

and adding this to OnEnable():

if (uiMode && wrapText) {
   initialUIAutoWrap = ((RectTransform) t).rect.width;

which yields the correct sizes in my tests! Success!

I have to figure out the correct padding values, but other than that, it is working! Thanks so much for the help!


Glad it worked out! I should probably mess with this more to have it built-in... I personally never touch Unity UI, so I'm always worried about getting the uGUI implementation wrong.

All good! Happy that I could solve my text worries :D

I've ended up implementing a bool to toggle the sizing:

As I needed another text to stay a certain size. That bool just replaces the one at line 2510 (that I commented out) and allows me to toggle the feature on or off.