본문 바로가기
개발/C#

유니티 C# 대화창 대화 시스템 간단 구현 (미연시 게임 만들기)

by SPNK 2024. 2. 29.
반응형

코드 작성

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class DatingSim : MonoBehaviour
{
    public Text dialogueText;  // UI 텍스트 컴포넌트
    public GameObject dialogueBox;  // 대화 상자 UI

    private string[] dialogueLines;  // 대화 문장 배열
    private int currentLineIndex = 0;  // 현재 대화 인덱스

    void Start()
    {
        // 대화 데이터 초기화 (실제 게임에서는 파일이나 데이터베이스에서 가져올 수 있음)
        dialogueLines = new string[]
        {
            "안녕하세요!",
            "만나서 반가워요.",
            "오늘 날씨가 참 좋네요."
        };

        // 시작 시 대화 상자 비활성화
        dialogueBox.SetActive(false);

        // 시작 시 대화 시작
        StartCoroutine(StartDialogue());
    }

    IEnumerator StartDialogue()
    {
        yield return new WaitForSeconds(1f);  // 시작 대기시간 (필요에 따라 조절)

        // 대화 상자 활성화
        dialogueBox.SetActive(true);

        // 대화 시작
        while (currentLineIndex < dialogueLines.Length)
        {
            dialogueText.text = dialogueLines[currentLineIndex];
            yield return new WaitUntil(() => Input.GetMouseButtonDown(0));  // 마우스 클릭 대기

            currentLineIndex++;  // 다음 대화로 넘어감

            // 모든 대화가 끝났을 때 대화 상자 비활성화
            if (currentLineIndex >= dialogueLines.Length)
            {
                dialogueBox.SetActive(false);
            }
        }
    }
}
반응형

댓글