유니티 C# 우편함 시스템 간단 구현 Mailbox System

반응형

유니티 C# 우편함 시스템 간단 구현 Mailbox System

1. 데이터 구조 설계

using System;

[Serializable]
public class Mail
{
    public string sender;         // 발신자 이름
    public string title;          // 메일 제목
    public string content;        // 메일 내용
    public DateTime receivedDate; // 수신 날짜
    public bool isRead;           // 메일 읽음 여부
    public bool hasAttachment;    // 첨부 파일 여부

    // 생성자
    public Mail(string sender, string title, string content, bool hasAttachment)
    {
        this.sender = sender;
        this.title = title;
        this.content = content;
        this.receivedDate = DateTime.Now;
        this.isRead = false;
        this.hasAttachment = hasAttachment;
    }
}

 

2. 시스템 클래스

using System.Collections.Generic;
using UnityEngine;

public class MailboxSystem : MonoBehaviour
{
    public List<Mail> mailbox = new List<Mail>();

    // 메일 추가
    public void AddMail(string sender, string title, string content, bool hasAttachment)
    {
        Mail newMail = new Mail(sender, title, content, hasAttachment);
        mailbox.Add(newMail);
        Debug.Log("새로운 메일이 도착했습니다: " + title);
    }

    // 메일 삭제
    public void DeleteMail(Mail mail)
    {
        if (mailbox.Contains(mail))
        {
            mailbox.Remove(mail);
            Debug.Log("메일이 삭제되었습니다: " + mail.title);
        }
        else
        {
            Debug.LogError("메일을 찾을 수 없습니다.");
        }
    }

    // 모든 메일 목록 확인
    public void ShowAllMails()
    {
        Debug.Log("우편함에 있는 메일 목록:");
        foreach (Mail mail in mailbox)
        {
            string readStatus = mail.isRead ? "읽음" : "안 읽음";
            Debug.Log($"발신자: {mail.sender}, 제목: {mail.title}, 수신일: {mail.receivedDate}, 상태: {readStatus}");
        }
    }

    // 메일 읽기
    public void ReadMail(Mail mail)
    {
        if (mailbox.Contains(mail))
        {
            mail.isRead = true;
            Debug.Log($"메일 내용: {mail.content}");
        }
        else
        {
            Debug.LogError("메일을 찾을 수 없습니다.");
        }
    }

    // 우편함에 새로운 메일이 있는지 확인
    public bool HasUnreadMail()
    {
        foreach (Mail mail in mailbox)
        {
            if (!mail.isRead)
            {
                return true;
            }
        }
        return false;
    }
}

 

3. 사용 예시

using UnityEngine;

public class MailboxTest : MonoBehaviour
{
    private MailboxSystem mailboxSystem;

    void Start()
    {
        // MailboxSystem 컴포넌트를 가져옴
        mailboxSystem = GetComponent<MailboxSystem>();

        // 우편함에 메일 추가
        mailboxSystem.AddMail("게임 운영팀", "환영합니다!", "환영 메일입니다. 게임을 즐겨주세요.", false);
        mailboxSystem.AddMail("이벤트 팀", "이벤트 보상", "이벤트에 참여해주셔서 감사합니다. 보상이 첨부되었습니다.", true);

        // 메일 목록 표시
        mailboxSystem.ShowAllMails();

        // 메일 읽기
        Mail firstMail = mailboxSystem.mailbox[0];
        mailboxSystem.ReadMail(firstMail);

        // 읽지 않은 메일이 있는지 확인
        if (mailboxSystem.HasUnreadMail())
        {
            Debug.Log("읽지 않은 메일이 있습니다.");
        }
        else
        {
            Debug.Log("모든 메일을 읽었습니다.");
        }

        // 메일 삭제
        mailboxSystem.DeleteMail(firstMail);
        mailboxSystem.ShowAllMails();
    }
}
반응형