From 1fc809141c0f4e34e992384e7c4d02a1b4613110 Mon Sep 17 00:00:00 2001 From: Geri Date: Sat, 27 Jan 2024 14:55:14 +0100 Subject: [PATCH] fix: Now all characters are displaying properly --- Assets/Scenes/Main.unity | 2 +- Assets/Scripts/Messaging/Typer/TextTyper.cs | 17 ++++++----------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity index 1207e5c..5374b3c 100644 --- a/Assets/Scenes/Main.unity +++ b/Assets/Scenes/Main.unity @@ -891,7 +891,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 6ce276be77304236a23df45b19b4cc08, type: 3} m_Name: m_EditorClassIdentifier: - maxVisibleMessages: 3 + maxVisibleMessages: 5 messageHeight: 16 messagePrefab: {fileID: -6056302822753658535, guid: 6bf006df27e33a24088a28c9d46dbb43, type: 3} --- !u!1 &1284257431 diff --git a/Assets/Scripts/Messaging/Typer/TextTyper.cs b/Assets/Scripts/Messaging/Typer/TextTyper.cs index 1a38770..f54ea43 100644 --- a/Assets/Scripts/Messaging/Typer/TextTyper.cs +++ b/Assets/Scripts/Messaging/Typer/TextTyper.cs @@ -16,21 +16,16 @@ namespace Messaging.Typer { public IEnumerator AnimateTextIn(string processedMessage, Action onFinish) { float timeOfLastCharacter = 0; - TMP_TextInfo textInfo = _textBox.textInfo; - _textBox.maxVisibleCharacters = 0; _textBox.text = processedMessage; _textBox.ForceMeshUpdate(); - int charCount = textInfo.characterCount; - int visibleCharacterIndex = 0; while (true) { - if (ShouldShowNextCharacter(SecondsPerCharacter, timeOfLastCharacter)) { - if (visibleCharacterIndex <= charCount) { - visibleCharacterIndex++; - _textBox.maxVisibleCharacters = visibleCharacterIndex; + if (ShouldShowNextCharacter(timeOfLastCharacter)) { + if (_textBox.maxVisibleCharacters <= _textBox.textInfo.characterCount) { + _textBox.maxVisibleCharacters++; timeOfLastCharacter = Time.unscaledTime; - if (visibleCharacterIndex == charCount) { + if (_textBox.maxVisibleCharacters == _textBox.textInfo.characterCount) { onFinish?.Invoke(); break; } @@ -41,8 +36,8 @@ namespace Messaging.Typer { } } - private static bool ShouldShowNextCharacter(float secondsPerCharacter, float timeOfLastCharacter) { - return Time.unscaledTime - timeOfLastCharacter > secondsPerCharacter; + private static bool ShouldShowNextCharacter(float timeOfLastCharacter) { + return Time.unscaledTime - timeOfLastCharacter > SecondsPerCharacter; } } } \ No newline at end of file