/// <summary> /// Prints (reveals) provided text message over time using a managed text printer with the provided ID. /// </summary> /// <param name="printerId">ID of the managed text printer which should print the message.</param> /// <param name="text">Text of the message to print.</param> /// <param name="authorId">ID of a character actor to which the printed text belongs (if any).</param> /// <param name="speed">Text reveal speed (<see cref="BaseRevealSpeed"/> modifier).</param> /// <param name="cancellationToken">Token for task cancellation. The text will be revealed instantly when cancelled.</param> public async Task PrintTextAsync(string printerId, string text, string authorId = default, float speed = 1, CancellationToken cancellationToken = default) { var printer = await GetOrAddActorAsync(printerId); OnPrintTextStarted?.Invoke(new PrintTextArgs(printer, text, authorId, speed)); printer.AuthorId = authorId; printer.Text += text; var revealDelay = scriptPlayer.IsSkipActive ? 0 : Mathf.Lerp(Configuration.MaxRevealDelay, 0, BaseRevealSpeed * speed); await printer.RevealTextAsync(revealDelay, cancellationToken); printer.RevealProgress = 1f; // Make sure all the text is always revealed. if (scriptPlayer.IsAutoPlayActive) { var autoPlayDelay = revealDelay * text.Count(char.IsLetterOrDigit); var waitUntilTime = Time.time + autoPlayDelay; while (Time.time < waitUntilTime && !cancellationToken.IsCancellationRequested) { await waitForEndOfFrame; } } else { await waitForEndOfFrame; // Always wait at least one frame to prevent instant skipping. } OnPrintTextFinished?.Invoke(new PrintTextArgs(printer, text, authorId, speed)); }
public async UniTask PrintTextAsync(string printerId, string text, string authorId = default, float speed = 1, CancellationToken cancellationToken = default) { var printer = await GetOrAddActorAsync(printerId); if (cancellationToken.IsCancellationRequested) { return; } OnPrintTextStarted?.Invoke(new PrintTextArgs(printer, text, authorId, speed)); printer.AuthorId = authorId; printer.Text += text; var revealDelay = scriptPlayer.SkipActive ? 0 : Mathf.Lerp(Configuration.MaxRevealDelay, 0, BaseRevealSpeed * speed); await printer.RevealTextAsync(revealDelay, cancellationToken); // Don't return on cancellation here, otherwise OnPrintTextFinished is not invoked when skip or continue inputs are activated. OnPrintTextFinished?.Invoke(new PrintTextArgs(printer, text, authorId, speed)); }
public virtual async UniTask PrintTextAsync(string printerId, string text, string authorId = default, float speed = 1, CancellationToken cancellationToken = default) { var printer = await GetOrAddActorAsync(printerId); if (cancellationToken.CancelASAP) { return; } OnPrintTextStarted?.Invoke(new PrintTextArgs(printer, text, authorId, speed)); printer.AuthorId = authorId; printer.Text += text; var revealDelay = scriptPlayer.SkipActive ? 0 : Mathf.Lerp(Configuration.MaxRevealDelay, 0, BaseRevealSpeed * speed); await printer.RevealTextAsync(revealDelay, cancellationToken); if (cancellationToken.CancelASAP) { return; } OnPrintTextFinished?.Invoke(new PrintTextArgs(printer, text, authorId, speed)); }