Typewriter effect and Size fitter

Announcements, support questions, and discussion for the Dialogue System.
User avatar
Shinos
Posts: 59
Joined: Tue Jan 17, 2023 10:51 am

Re: Typewriter effect and Size fitter

Post by Shinos »

Hi,
sorry if i didn't answer before, unfortunately i had some busy days, thanks for your help i'll try your script right now.
User avatar
Shinos
Posts: 59
Joined: Tue Jan 17, 2023 10:51 am

Re: Typewriter effect and Size fitter

Post by Shinos »

ok i tried it, but unless i'm doing something wrong it doesn't seem to work as intended:
here's a record : the red one works because it is using the old subclass with text and not textmeshpro, the green with the new suggestions and script does not scale the bubble with the text
https://youtube.com/shorts/zSFmxPjtq_U
User avatar
Tony Li
Posts: 22124
Joined: Thu Jul 18, 2013 1:27 pm

Re: Typewriter effect and Size fitter

Post by Tony Li »

Hi,

The script I provided in a previous reply expands vertically, not horizontally. It's just an example.
User avatar
Shinos
Posts: 59
Joined: Tue Jan 17, 2023 10:51 am

Re: Typewriter effect and Size fitter

Post by Shinos »

I see, but i tried more and it does not expand vertically if the text exceed it just goes out of the bubble speech, the bubble remains of the same size whatever happens both vertically and horizontallly.

PS: i have to apologize i tried it with the default prefab of the bubble subtitles and it indeed resize vertically, now i only have to understand what i did wrong with mine custom bubble and how to make it resize also horizontally.
thanks
User avatar
Tony Li
Posts: 22124
Joined: Thu Jul 18, 2013 1:27 pm

Re: Typewriter effect and Size fitter

Post by Tony Li »

Hi,

If you compare the Layout Elements and Horizontal/Vertical Layout Groups of the bubbles, you should be able to identify what's different in the bubble UIs.

Resizing horizontally is going to require a little more code. I think you'll need to call textComponent.ForceMeshUpdate() and then use Mathf.Min() to set the Layout Group's Min Width to the lesser value of textBounds.x and the maximum width you want your bubble to grow before line wrapping.
User avatar
Shinos
Posts: 59
Joined: Tue Jan 17, 2023 10:51 am

Re: Typewriter effect and Size fitter

Post by Shinos »

Hi,
so i was trying implementing your tips for the horizontal and this is the code now:

Code: Select all

using PixelCrushers.DialogueSystem;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class CustomTMProTypewriter : TextMeshProTypewriterEffect
{
    [SerializeField] private float maxBubbleWidth;

    private void Update()
    {
        if (isPlaying)
        {
            var bounds = textComponent.textBounds;
            layoutElement.preferredHeight = bounds.size.y;

           
            textComponent.ForceMeshUpdate(); 

            float preferredWidth = Mathf.Min(bounds.size.x, maxBubbleWidth);
           layoutElement.preferredWidth =preferredWidth;
        }
    }
but it doesn't work as intended, this is what happens actually ahaha
Senza titolo.png
Senza titolo.png (3.43 KiB) Viewed 941 times
User avatar
Tony Li
Posts: 22124
Joined: Thu Jul 18, 2013 1:27 pm

Re: Typewriter effect and Size fitter

Post by Tony Li »

Hi,

Try setting layoutElement.minWidth, too.
User avatar
Shinos
Posts: 59
Joined: Tue Jan 17, 2023 10:51 am

Re: Typewriter effect and Size fitter

Post by Shinos »

Hi,
tried but nothing, same result, setting minwidth just get the width stuck, and it gets updated only the vertical size the same as before, i need to resize horizontal size not vertical, (preferrable would be both)

the preferred height gets updated and it works flawessly, but the preferred width gets stuck at a value of 23 i don't know why. and it just resize vertically making it static in the horizontal axis
User avatar
Tony Li
Posts: 22124
Joined: Thu Jul 18, 2013 1:27 pm

Re: Typewriter effect and Size fitter

Post by Tony Li »

This is a bit complicated to do with TMPro. Here's a script:

Code: Select all

using PixelCrushers.DialogueSystem;
using UnityEngine;

public class CustomTMProTypewriter : TextMeshProTypewriterEffect
{
    [SerializeField] private float maxBubbleWidth;

    private void Update()
    {
        if (isPlaying)
        {
            layoutElement.minWidth = layoutElement.preferredWidth = maxBubbleWidth;

            Canvas.ForceUpdateCanvases();
            textComponent.ForceMeshUpdate();

            var bounds = textComponent.textBounds;
            var desiredX = Mathf.Min(bounds.size.x, maxBubbleWidth);
            var desiredY = bounds.size.y;

            layoutElement.minHeight = layoutElement.preferredHeight = desiredY;
            layoutElement.minWidth = layoutElement.preferredWidth = desiredX;

            textComponent.ForceMeshUpdate();
            Canvas.ForceUpdateCanvases();
        }
    }
}
You'll need to set up a Mask:

tmpromask.png
tmpromask.png (67.48 KiB) Viewed 912 times

And the first line will type by word, not by character.
User avatar
Shinos
Posts: 59
Joined: Tue Jan 17, 2023 10:51 am

Re: Typewriter effect and Size fitter

Post by Shinos »

Hi,
sorry if i did not answer before, i also tried this, but unless i did something wrong it didn't worked as intended, like i can't even see the words now, so maybe i screwed up something regarding the mask, but anyway they keep to be wrote only in vertical and the horizontal line keeps to stay at the same minimun lil size, so i just kinda gave up, cause this resulted to be a bigger issue to solve than i thought it was, and even if i still think it's not as cool as having the speech box to resize as the words gets typed i'll just use the default system as it was at the beginning, it's worth to say that without Text Mesh Pro the script you provided at the beginning worked, but since i do not have TextAnimator, to give a little fancy feel to my dialogues i really need TMPro, so that's it. I'm, sorry to have bothered you without coming to an actual "solution" but don't worry
Post Reply