protected void ProcessRPGMakerCodes() { rpgMakerTokens.Clear(); var source = textComponent.text; var result = string.Empty; if (!source.Contains("\\")) { return; } source = Tools.StripTags(source); int safeguard = 0; while (!string.IsNullOrEmpty(source) && safeguard < 9999) { safeguard++; RPGMakerTokenType token; if (PeelRPGMakerTokenFromFront(ref source, out token)) { int i = result.Length; if (!rpgMakerTokens.ContainsKey(i)) { rpgMakerTokens.Add(i, new List <RPGMakerTokenType>()); } rpgMakerTokens[i].Add(token); } else { result += source[0]; source = source.Remove(0, 1); } } textComponent.text = Regex.Replace(textComponent.text, @"\\[\.\,\^\<\>]", string.Empty); }
/// <summary> /// Plays the typewriter effect. /// </summary> public virtual IEnumerator Play(int fromIndex) { if ((textComponent != null) && (charactersPerSecond > 0)) { if (waitOneFrameBeforeStarting) { yield return(null); } fromIndex = StripRPGMakerCodes(Tools.StripTags(textComponent.text.Substring(0, fromIndex))).Length; ProcessRPGMakerCodes(); ProcessTags(); if (runtimeAudioSource != null) { runtimeAudioSource.clip = audioClip; } onBegin.Invoke(); paused = false; float delay = 1 / charactersPerSecond; float lastTime = DialogueTime.time; float elapsed = fromIndex / charactersPerSecond; textComponent.maxVisibleCharacters = fromIndex; textComponent.ForceMeshUpdate(); yield return(null); textComponent.maxVisibleCharacters = fromIndex; textComponent.ForceMeshUpdate(); TMPro.TMP_TextInfo textInfo = textComponent.textInfo; var parsedText = textComponent.GetParsedText(); int totalVisibleCharacters = textInfo.characterCount; // Get # of Visible Character in text object charactersTyped = fromIndex; int skippedCharacters = 0; while (charactersTyped < totalVisibleCharacters) { if (!paused) { var deltaTime = DialogueTime.time - lastTime; elapsed += deltaTime; var goal = (elapsed * charactersPerSecond) + skippedCharacters; while (charactersTyped < goal) { if (rpgMakerTokens.ContainsKey(charactersTyped)) { var tokens = rpgMakerTokens[charactersTyped]; for (int i = 0; i < tokens.Count; i++) { var token = tokens[i]; switch (token) { case RPGMakerTokenType.QuarterPause: yield return(DialogueTime.WaitForSeconds(quarterPauseDuration)); break; case RPGMakerTokenType.FullPause: yield return(DialogueTime.WaitForSeconds(fullPauseDuration)); break; case RPGMakerTokenType.SkipToEnd: charactersTyped = totalVisibleCharacters - 1; break; case RPGMakerTokenType.InstantOpen: var close = false; while (!close && charactersTyped < totalVisibleCharacters) { charactersTyped++; skippedCharacters++; if (rpgMakerTokens.ContainsKey(charactersTyped) && rpgMakerTokens[charactersTyped].Contains(RPGMakerTokenType.InstantClose)) { close = true; } } break; } } } var typedCharacter = (0 <= charactersTyped && charactersTyped < parsedText.Length) ? parsedText[charactersTyped] : ' '; if (charactersTyped < totalVisibleCharacters && !IsSilentCharacter(typedCharacter)) { PlayCharacterAudio(typedCharacter); } onCharacter.Invoke(); charactersTyped++; textComponent.maxVisibleCharacters = charactersTyped; if (IsFullPauseCharacter(typedCharacter)) { yield return(DialogueTime.WaitForSeconds(fullPauseDuration)); } else if (IsQuarterPauseCharacter(typedCharacter)) { yield return(DialogueTime.WaitForSeconds(quarterPauseDuration)); } } } textComponent.maxVisibleCharacters = charactersTyped; HandleAutoScroll(); //---Uncomment the line below to debug: //Debug.Log(textComponent.text.Substring(0, charactersTyped).Replace("<", "[").Replace(">", "]") + " (typed=" + charactersTyped + ")"); lastTime = DialogueTime.time; var delayTime = DialogueTime.time + delay; int delaySafeguard = 0; while (DialogueTime.time < delayTime && delaySafeguard < 999) { delaySafeguard++; yield return(null); } } } Stop(); }