개발/Unity
[유니티] Text 타이핑 효과 내기
이내내
2021. 8. 18. 18:41
글자를 한 글자씩 화면에 출력하면 일종의 타이핑 효과를 낼 수 있다.
public Text textSpeaker;
public Text textContent;
void Start()
{
StartCoroutine(PrintText("주인공", "배고프다.");
}
IEnumerator PrintText(string speaker, string content)
{
textSpeaker.text = speaker;
// content = content.Replace("\\n", "\n");
for(int i=0; i < content.Length; i++)
{
textContent.text += content[i];
yield return new WaitForSeconds(0.1f);
}
yield return new WaitForSeconds(2f);
textContent.text = "";
}
textSpeaker / textContent : 하이어라키에 text로 존재하며 각각 화자, 대사를 담당한다.
PrintText 안의 for문이 한 글자를 content(대사)의 한 글자를 0.1초 간격으로 출력하여 타이핑 효과를 낸다.
주석 처리된 Replace 부분은 만약 content를 public으로 사용해 에디터 상에서 대사를 수정할 때 "\\n"을 "\n"으로 교체하여 게임뷰 상에서 줄바꿈을 할 수 있게 한다.