본문 바로가기
개발/C#

유니티 C# 업적 시스템 만들기 간단 구현

by SPNK 2024. 2. 29.
반응형

코드 작성

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

public class AchievementSystem : MonoBehaviour
{
    [System.Serializable]
    public class Achievement
    {
        public string name;
        public string description;
        public bool isUnlocked;
        public bool showAlert;  // 알람을 띄울지 여부

        public void Unlock()
        {
            if (!isUnlocked)
            {
                isUnlocked = true;
                Debug.Log($"업적 달성: {name}");

                if (showAlert)
                {
                    ShowAchievementAlert();
                }
            }
        }

        private void ShowAchievementAlert()
        {
            // 알람을 띄우는 로직 추가 (예: UI 알람, 팝업 등)
            Debug.Log($"알람: {name} 달성!");
        }
    }

    public Achievement[] achievements;

    void Start()
    {
        // 업적 초기화
        foreach (Achievement achievement in achievements)
        {
            achievement.isUnlocked = false;
        }
    }

    public void CheckAchievements(string condition)
    {
        // 특정 조건을 만족하는 경우 해당 업적 달성
        foreach (Achievement achievement in achievements)
        {
            if (!achievement.isUnlocked && condition == achievement.name)
            {
                achievement.Unlock();
            }
        }
    }
}

 

게임 매니저

public class GameManager : MonoBehaviour
{
    public AchievementSystem achievementSystem;

    void Start()
    {
        // 게임 시작 시 업적 초기화
        achievementSystem.Start();
    }

    void Update()
    {
        // 예시: 특정 상황에서 업적 달성
        if (Input.GetKeyDown(KeyCode.Space))
        {
            achievementSystem.CheckAchievements("SpacePressed");
        }
        else if (Input.GetKeyDown(KeyCode.Escape))
        {
            achievementSystem.CheckAchievements("EscPressed");
        }
        // 추가적인 조건에 따라 업적 체크 가능
    }
}
반응형

댓글