[HOWTO] How To: Adjust Typewriter To Audio Length
Posted: Thu Aug 03, 2023 9:17 am
By default, the typewriter runs at a constant characters/second speed specified by the typewriter effect's Characters Per Second value. If you're using the AudioWait() sequencer command to play audio with each line and need to vary the typewriter speed to match the audio length, you can use an OnConversationLine() method to change it. Something like:
Notes:
Code: Select all
void OnConversationLine(Subtitle subtitle)
{
// Regex might be more concise, but this is a simple way to get the audio clip name:
var audioWaitPos = subtitle.sequence.IndexOf("AudioWait(");
if (audioWaitPos == -1) return; // No audio.
var audioWaitEndPos = subtitle.sequence.Indexof(")", audioWaitPos);
var clipNamePos = audioWaitPos + "AudioWait(".Length;
var clipNameLength = audioWaitEndPos - clipNamePos;
var audioClipName = subtitle.sequence.Substring(clipNamePos, clipNameLength);
var audioClip = DialogueManager.LoadAsset(audioClipName, typeof(AudioClip));
// Determine which subtitle panel we'll use:
DialogueActor dialogueActor;
var panel = DialogueManager.standardDialogueUI.conversationUIElements.standardSubtitleControls.GetPanel(subtitle, out dialogueActor);
// Set the subtitle panel's typewriter speed:
var numChars = subtitle.formattedText.text.Length; // (See note below.)
panel.GetTypewriter().charactersPerSecond = numChars / audioClip.length;
}
- Doesn't account for RPG Maker-style speed codes in text or rich text/TextMesh Pro tags.
- Doesn't account for using Addressables with async loading; tries to load audio clip immediately.