

using UnityEngine;
[CreateAssetMenu(fileName = "TalkBundle", menuName = "Scriptable Objects/TalkBundle")]
public class TalkBundle : ScriptableObject
{
[System.Serializable]
public class TalkData
{
///~~~생략~~~
}
[System.Serializable]
public class Choice //선택지
{
public string choiceText; //선택지 이름
public ActionType actionType; //선택지 종류
public TalkBundle nextTalkBundle; //다음 대화 목록
}
public TalkData[] talkdatas; //대화 목록
public Choice[] choices; //선택지 내용 목록
public enum ActionType //선택지 종류
{
ContinueTalk,
EndTalk,
}
}
선택지의 정보를 담고 있는 Choice 클래스 추가

인스펙터에서 선택지를 추가해준다
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class TalkManager : MonoBehaviour
{
[Header("[대화문용]")]
public TextMeshProUGUI talkText; //대화문 내용 텍스트
public RectTransform namePanelPosition; //이름 패널 위치
public TextMeshProUGUI nameText; //이름 텍스트
public RectTransform portraitPosition; //포트레잇 위치
public Image portraitImg; //포트에릿 이미지
public Animator talkPanelAnimator; //대화문 애니메이션
public GameManager gameManager; // GameManager 참조
private TalkBundle currentTalkBundle; // 현재 대화 번들
private int currentIndex = 0; //대화 인덱스
[Header("[선택지용]")]
public GameObject choicePanel; //선택지 패널
public Button choiceButtonPrefab; //선택지 버튼 프리팹
public void StartTalk(TalkBundle talkBundle) //대화 시작
{
///~~~생략~~~
}
public void NextTalk() //대화문 출력
{
int lastIndex = currentTalkBundle.talkdatas.Length - 1; //출력할 대사가 있는지, 없는지 확인용
// 아직 출력할 대사가 있다면
if (currentIndex <= lastIndex)
{
///~~~생략~~~
if (currentIndex == lastIndex) //만약 마지막 인덱스라면 선택지 보여주기
{
if (currentTalkBundle.choices != null && currentTalkBundle.choices.Length > 0)
{
ShowChoices(currentTalkBundle.choices);
}
}
currentIndex++; // 다음 대화로 진행
}
else
{
if (currentTalkBundle.choices == null || currentTalkBundle.choices.Length == 0)
{
EndTalk();
}
}
}
private void ShowChoices(TalkBundle.Choice[] choices) //선택지 보여주기
{
choicePanel.SetActive(true); //선택지 패널 활성화
foreach (Transform child in choicePanel.transform)
Destroy(child.gameObject); //선택지 패널 초기화
foreach (var choice in choices)
{
Button btn = Instantiate(choiceButtonPrefab, choicePanel.transform); //선택지 버튼 생성
btn.GetComponentInChildren<TextMeshProUGUI>().text = choice.choiceText; //선택지 버튼 텍스트
//선택지 버튼 클릭 시 실행될 함수 등록
btn.onClick.AddListener(() =>
{
choicePanel.SetActive(false);
HandleChoice(choice);
});
}
}
private void HandleChoice(TalkBundle.Choice choice) //선택지 버튼 클릭 시 실행되는 함수
{
switch (choice.actionType)
{
case TalkBundle.ActionType.ContinueTalk:
//다른 대화문 시작
StartTalk(choice.nextTalkBundle);
break;
case TalkBundle.ActionType.EndTalk:
//대화문 종료
EndTalk();
break;
}
}
private void EndTalk()
{
///~~~생략~~~
}
public bool IsTalking()
{
return gameManager.currentState == GameManager.GameState.Talking;
}
}
