Unity/유니티 개발
[유니티 개발] 타이핑 모션이 들어간 대화창 만들기
도이(doi)
2021. 12. 28. 12:04
728x90
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[System.Serializable]
public class Dialogue
{
public List<string> sentences;
}
public class DialogueSystem : MonoBehaviour
{
public Text txtSentence;
public Dialogue info;
Queue<string> sentences = new Queue<string>();
private void Start()
{
Begin(info);
}
//대화 시작
public void Begin(Dialogue info)
{
sentences.Clear();
foreach(var sentence in info.sentences)
{
sentences.Enqueue(sentence);
}
Next();
}
//버튼 클릭 시 다음 대화로 넘어감
public void Next()
{
if(sentences.Count == 0)
{
End();
return;
}
txtSentence.text = string.Empty;
StopAllCoroutines();
StartCoroutine(TypeSentence(sentences.Dequeue()));
}
//타이핑 모션 함수
IEnumerator TypeSentence(string sentence)
{
foreach(var letter in sentence)
{
txtSentence.text += letter;
yield return new WaitForSeconds(0.1f);
}
}
//대화 끝
private void End()
{
if (sentences != null)
{
Debug.Log("end");
}
}
}
728x90
반응형