Unity 입문 - 4 (개인 과제 제출)

이준호·2023년 11월 29일
0

📌 NPC대화

위 처럼 가까이 다가가면 팝업이 뜨고 대화하기를 누르면 밑에 대화 팝업이 뜨게 하였다.

대화는 딕셔너리를 사용했는데. 벨류값에 배열을 써서 대화를 더 만들려 했지만 시간이 부족하여 하지 못하였다.

public class NPCCollision : MonoBehaviour
{
    public GameObject NPCPopupPanel;
    public GameObject NPCConversationPanel;

    public int npcKeyValue;

    // NPC 대화 정보
    protected Dictionary<int, string> talkDate;
    protected Dictionary<int, Sprite> npcProfile;

    private void Awake()
    {
        // 초기화
        talkDate = new Dictionary<int, string>();
        npcProfile = new Dictionary<int, Sprite>();
        TalkDateSetting();
    }
    void TalkDateSetting()
    {
        // NPC 대사
        talkDate.Add(1000, "안녕하세요~ 오정호 튜터 입니다!ㅎㅎ");
        talkDate.Add(2000, "안녕하세요! 송지원 튜터 입니다.");
        talkDate.Add(3000, "안녕하세요. 이성언 튜터 입니다!");
        talkDate.Add(4000, "안녕하세요~! 강성훈 튜터 입니다.");
        talkDate.Add(5000, "안녕하세요. 김하연 튜터 입니다.!");
        talkDate.Add(6000, "안녕하세용 정승호 매니저 입니당.");
        talkDate.Add(7000, "한효승 매니저 입니다~~ 공부하세요.");
        talkDate.Add(8000, "안녕하세욤! 장윤서 매니저 입니다~");
        talkDate.Add(9000, "안녕하세요! 박준영 매니저 입니다~");

        // NPC Image
        npcProfile.Add(1000, Resources.Load<Sprite>("Image/OH Tuter"));
        npcProfile.Add(2000, Resources.Load<Sprite>("Image/Song Tuter"));
        npcProfile.Add(3000, Resources.Load<Sprite>("Image/Lee tuter"));
        npcProfile.Add(4000, Resources.Load<Sprite>("Image/Kang tuter"));
        npcProfile.Add(5000, Resources.Load<Sprite>("Image/White Tuter"));
        npcProfile.Add(6000, Resources.Load<Sprite>("Image/Jung Manager"));
        npcProfile.Add(7000, Resources.Load<Sprite>("Image/Han Manager"));
        npcProfile.Add(8000, Resources.Load<Sprite>("Image/Jang Manager"));
        npcProfile.Add(9000, Resources.Load<Sprite>("Image/Park Manager"));
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        // 판넬 켜주기
        NPCPopupPanel.SetActive(true);
        // 접촉한 npc 이름으로 판넬 텍스트 설정
        NPCPopupPanel.transform.GetChild(0).GetComponent<Text>().text = GameManager.instance.ContactNPCNameOutput();
        // Npc 대화 text에 만난 NPC의 Key값으로 Value받아오기
        NPCConversationPanel.transform.GetChild(0).GetComponent<Text>().text = talkDate[npcKeyValue];
        NPCConversationPanel.transform.GetChild(2).GetComponent<Image>().sprite = npcProfile[npcKeyValue];
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        // 판넬 꺼주기
        NPCPopupPanel.SetActive(false);
        // 대화 판넬도 꺼주기
        NPCConversationPanel.SetActive(false);
    }
}

NPC마다 Key값을 넣어놔서 그 key값에 해당하는 대화가 나오도록 하였다. 설계를 잘못해서 한곳에 기능이 너무 모여있는데 나중에는 설계를 더 세분화해서 해야겠다.

    // Player가 접촉한 NPC 이름 저장
    public void ContactNPCNameSave(string name)
    {
        contactName = name;
    }

    // Player가 접촉한 NPC 이름 내보내기
    public string ContactNPCNameOutput()
    {
        return contactName;
    }

GameManager에서 접촉한 NPC의 정보를 저장하여 불러오며 활용하였다.

public class PlayerCollision : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collision)
    {
        // NPC 이름을 가져온다.
        GameManager.instance.ContactNPCNameSave(collision.transform.parent.transform.GetComponent<TextMesh>().text);
    }
}

원래 설계했던 것보다 계속 규모가 커지고 코드나 오브젝트 간의 쓰임새가 모호해지면서 처음 설계처럼 안되고 많이 복잡해진 경향이 있다. 앞으로는 더 자세히 짜고 설계에 시간을 더 들이도록 하고 설계에 대해서 더 배우도록 해야겠다.

profile
No Easy Day

0개의 댓글