Last active
January 30, 2023 05:32
-
-
Save Maoyeedy/3f0a213fa75cd94b56f722dfa5c0d802 to your computer and use it in GitHub Desktop.
Unity Dialog Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using TMPro; | |
using UnityEngine; | |
public class DialogPlayer : MonoBehaviour | |
{ | |
public Dialog[] dialogs; | |
public TextMeshProUGUI dialogText, nameText; | |
public float characterInterval = 0.05f, autoNextLineTime = 2.5f; | |
private float _elapsedTime; | |
private int _index; | |
private void Start() | |
{ | |
dialogText.text = string.Empty; | |
nameText.text = string.Empty; | |
StartCoroutine(TypeLine()); | |
} | |
private void Update() | |
{ | |
_elapsedTime += Time.deltaTime; | |
if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Return) || _elapsedTime > autoNextLineTime) SwitchLine(); | |
} | |
private void SwitchLine() | |
{ | |
_elapsedTime = 0; | |
if (dialogText.text == dialogs[_index].dialog) | |
{ | |
NextLine(); | |
} | |
else | |
{ | |
StopAllCoroutines(); | |
dialogText.text = dialogs[_index].dialog; | |
} | |
} | |
private void NextLine() | |
{ | |
dialogText.text = string.Empty; | |
if (_index < dialogs.Length - 1) | |
{ | |
_index++; | |
StartCoroutine(TypeLine()); | |
nameText.text = dialogs[_index].name; | |
} | |
else | |
{ | |
gameObject.SetActive(false); | |
} | |
} | |
private IEnumerator TypeLine() | |
{ | |
foreach (char x in dialogs[_index].dialog) | |
{ | |
dialogText.text += x; | |
yield return new WaitForSeconds(characterInterval); | |
} | |
} | |
[Serializable] | |
public struct Dialog | |
{ | |
public string name; | |
public string dialog; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Utilizes TextMeshPro.