Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- public class DialogueWindow : MonoBehaviour
- {
- public const float kMaxTextTime = 0.1f;
- public static int TextSpeed = 2;
- public TMP_Text Text;
- public string CurrentText;
- public GameObject Cursor;
- Animator Anim;
- protected enum TextStateType
- {
- None,
- Writing,
- Waiting
- }
- TextStateType TextState;
- private void Start()
- {
- Anim = GetComponent<Animator>();
- }
- public void Show(string dialogue)
- {
- Anim.SetBool("Open", true);
- CurrentText = dialogue;
- }
- public void Close()
- {
- Anim.SetBool("Open", false);
- }
- public void OnDialogueOpen()
- {
- StartCoroutine(DisplayText());
- }
- public void OnDialogueClosed()
- {
- StopAllCoroutines();
- Text.text = "";
- TextState = TextStateType.None;
- Cursor.SetActive(false);
- }
- protected IEnumerator DisplayText()
- {
- Text.text = "";
- TextState = TextStateType.Writing;
- Cursor.SetActive(false);
- string originalText = CurrentText;
- string displayedText = "";
- int alphaIndex = 0;
- foreach (char c in CurrentText.ToCharArray())
- {
- alphaIndex++;
- Text.text = originalText;
- displayedText = Text.text.Insert(alphaIndex, "<color=#00000000>");
- Text.text = displayedText;
- yield return new WaitForSecondsRealtime(kMaxTextTime / TextSpeed);
- }
- TextState = TextStateType.Waiting;
- //Cursor.SetActive(true);
- yield return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment