all repos — RPG @ 62a0546261098a7868f5986f21adf997f247852f

Fully functional 3D turn based role playing game coded in C# and base Unity.

Assets/Scripts/Utility/SlowTextManager.cs (view raw)

 1using System.Collections;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using UnityEngine.UI;
 5
 6[RequireComponent(typeof(AudioSource))]
 7public class SlowTextManager : MonoBehaviour
 8{
 9    private float letterPause = 0.05f;
10    public AudioClip sound;
11    private AudioSource audioSource;
12    public Text text;
13
14    private string message;
15    
16    void Start()
17    {
18        audioSource = GetComponent<AudioSource>();
19        text = GetComponent<Text>();
20        message = text.text;
21        text.text = "";
22        StartCoroutine(TypeText());
23    }
24
25    IEnumerator TypeText()
26    {
27        foreach (char letter in message.ToCharArray())
28        {
29            text.text += letter;
30            if (sound)
31                audioSource.PlayOneShot(sound);
32            yield return 0;
33            yield return new WaitForSeconds(letterPause);
34        }
35    }
36}