Demigiant / dotween

A Unity C# animation engine. HOTween v2
http://dotween.demigiant.com
Other
2.3k stars 346 forks source link

Callback for when a DOText makes a character appear? #636

Open emredesu opened 1 year ago

emredesu commented 1 year ago

Hello, I would like to play a sound effect whenever a DOText tween makes a character appear, bit by bit. I tried OnUpdate and OnStepComplete, but those seem to fire way quite often compared to a character's appearance. Is there a method for this?

ThomasACampbell commented 4 months ago

Hello. I'm trying to do the same thing. Did you ever find a usable callback?

emredesu commented 4 months ago

Hello. I'm trying to do the same thing. Did you ever find a usable callback?

Hey, I couldn't find a usable callback so I ended up writing my own tweener. Here's the code:

    private readonly char[] punctuationCharacters = { ',', '.', ':', '!', '?', ';', '-' };

    IEnumerator TextTween(string targetText, float waitTimePerCharacter) {
        tweeningText = true;

        textTMP.SetText(targetText);
        textTMP.ForceMeshUpdate(); // Makes it so that textInfo.characterCount information is available right away.

        int charCount = textTMP.textInfo.characterCount;
        textTMP.maxVisibleCharacters = 0;

        for (int i = 0; i < charCount; i++) {
            textTMP.maxVisibleCharacters++;
            OnTextUpdated();
            yield return new WaitForSecondsRealtime(waitTimePerCharacter);

            // Pause for punctuation characters
            foreach (char punctuationChar in punctuationCharacters) {
                if (textTMP.textInfo.characterInfo[textTMP.maxVisibleCharacters - 1].character == punctuationChar)
                    yield return new WaitForSecondsRealtime(punctuationDelay);
            }
        }

        tweeningText = false;

        OnTextTweenComplete();
    }

    void OnTextUpdated() {
        // Do something when a character appears...
    }

    void OnTextTweenComplete() {
        // Do something when the text finishes tweening...
    } 

Will need some tinkering as I've copied & pasted it from my project and removed a lot of parts... but I hope you get the idea :p

ThomasACampbell commented 3 months ago

So very nice of you! Thank you for taking your time to share this :)

On Mon, Mar 25, 2024 at 10:15 PM emredesu @.***> wrote:

Hello. I'm trying to do the same thing. Did you ever find a usable callback?

Hey, I couldn't find a usable callback so I ended up writing my own tweener. Here's the code:

private readonly char[] punctuationCharacters = { ',', '.', ':', '!', '?', ';', '-' };

IEnumerator TextTween(string targetText, float waitTimePerCharacter) {
    tweeningText = true;

    textTMP.SetText(targetText);
    textTMP.ForceMeshUpdate(); // Makes it so that textInfo.characterCount information is available right away.

    int charCount = textTMP.textInfo.characterCount;
    textTMP.maxVisibleCharacters = 0;

    for (int i = 0; i < charCount; i++) {
        textTMP.maxVisibleCharacters++;
        OnTextUpdated();
        yield return new WaitForSecondsRealtime(waitTimePerCharacter);

        // Pause for punctuation characters
        foreach (char punctuationChar in punctuationCharacters) {
            if (textTMP.textInfo.characterInfo[textTMP.maxVisibleCharacters - 1].character == punctuationChar)
                yield return new WaitForSecondsRealtime(punctuationDelay);
        }
    }

    tweeningText = false;

    OnTextTweenComplete();
}

void OnTextUpdated() {
    // Do something when a character appears...
}

void OnTextTweenComplete() {
    // Do something when the text finishes tweening...
}

Will need some tinkering as I've copied & pasted it from my project and removed a lot of parts... but I hope you get the idea :p

— Reply to this email directly, view it on GitHub https://github.com/Demigiant/dotween/issues/636#issuecomment-2018931355, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGGGS5V3G32CG3A5TF4GWN3Y2CHY3AVCNFSM6AAAAAAY5BZM4WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMJYHEZTCMZVGU . You are receiving this because you commented.Message ID: @.***>