본문 바로가기

Unity 공부/Unity_2D

[Day 14] 캐릭터 해금 시스템

[ 캐릭터 해금 시스템 ] --> 도전과제를 달성하면 캐릭터가 해금되는 로직 구현!

  • Character Group의 Character 1을 Ctrl + D로 복사하여 2개 추가
        - 각각 이름을 Character 2,3으로 변경
        - 이름에 맞게 Source Sprite 변경
  • Character 2\Text Name의 Text = 감자농부
        - Text Adv의 Text = 데미지 20% 증가
        - Icon의 색에 맞게 Character 2의 색상 변경(보라)
        - Text Name, Adv의 Outline 색상을 Character 2 보다 조금 어둡게 설정 
        - Character OnClick 이벤트의 함수 매개변수를 2로 변경
  • Character 3\Text Name의 Text = 콩농부
        - Text Adv의 Text = 회전체, 관통 1 증가
        - Icon의 색에 맞게 Character 3의 색상 변경(주황)
        - Text Name, Adv의 Outline 색상을 Character 3보다 조금 어둡게 설정
        - Character OnClick 이벤트의 함수 매개변수를 3으로 변경
  • Character Group의 Width = 102 | Height = 102
        - Spacing X = 2 | Y = 2
  • Character 2를 Ctrl + D로 복사 후 이름을 Character 2 (Lock)으로 변경
        - Character 2 (Lock)의 색상을 회색으로 변경
        - Icon의 색상 검은색으로 변경
        - Text Name 삭제
        - Text Adv의 Text = 해금 조건...ex)언데드 10마리 처치 시 합류
        - Text Adv의 색상 검은색으로 변경
        - Line Spacing = 1.2
        - Outline Component 삭제
        - Button Component 삭제
        - Pos Y = -12
  • Character 2 (Lock)을 Ctrl + D로 복사 후 이름 Character 3 (Lock)으로 변경
        - Text Adv의 Text = 해금 조건...ex)아무 캐릭터로 생존 시 합류
        - Icon을 Character 3으로 변경
  • C# 스크립트 생성(Achive Manager)
    ㅡ아래는 완성된 코드ㅡ
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AchiveManager : MonoBehaviour
{
    public GameObject[] lockCharactor; // 잠긴 캐릭터
    public GameObject[] unlockCharactor; // 잠금 해제된 캐릭터
    public GameObject uiNotice; // Notice를 넣을 변수
    
    enum Achive { UnlockPotato, UnlockBean } // 열거형
    Achive[] achives;
    WaitForSecondsRealtime wait;

    private void Awake()
    {
        achives = (Achive[])Enum.GetValues(typeof(Achive));// 명시적 타입 변경
        wait = new WaitForSecondsRealtime(5); // 5초 쉼
        if (!PlayerPrefs.HasKey("MyData")) // 데이터가 없으면
        {
            Init();
        }
    }

    void Init()
    {
        PlayerPrefs.SetInt("MyData", 1);

        foreach(Achive achive in achives)
        {
            PlayerPrefs.SetInt(achive.ToString(),0); // 모두 초기화

        }
    }

    private void Start()
    {
        UnlockCharactor();    
    }

    void UnlockCharactor()
    {
        for(int index=0; index<lockCharactor.Length; index++) // 배열 순회하며 업적 이름 가져오기
        {
            string achiveName = achives[index].ToString();
            bool isUnlock = PlayerPrefs.GetInt(achiveName) == 1;
            lockCharactor[index].SetActive(!isUnlock);
            unlockCharactor[index].SetActive(isUnlock);
        }
    }

    private void LateUpdate()
    {
        foreach(Achive achive in achives)
        {
            CheckAchive(achive);
        }
    }

    void CheckAchive(Achive achive)
    {
        bool isAchive = false;

        switch(achive)
        {
            case Achive.UnlockPotato: // 감자농부 해금 조건
                isAchive = GameManager.instance.kill >= 100;
                break;
                case Achive.UnlockBean: // 콩농부 해금 조건
                isAchive = GameManager.instance.gameTime == GameManager.instance.maxGameTime;
                break;
        }

        if(isAchive && PlayerPrefs.GetInt(achive.ToString()) == 0) // 업적 달성 and 해금이 되지 않았을 때
        {
            PlayerPrefs.SetInt(achive.ToString(), 1); // 업적 해금

            for(int index=0; index<uiNotice.transform.childCount; index++) // 자식 오브젝트 개수만큼 루프
            {
                bool isActive = index == (int)achive; // 순번이 맞으면 활성화
                uiNotice.transform.GetChild(index).gameObject.SetActive(isActive); // 오브젝트 활성화
            }

            StartCoroutine(NoticeRoutine());
        }
    }

    IEnumerator NoticeRoutine()
    {
        uiNotice.SetActive(true); // 오브젝트 활성화

        yield return wait; // wait return

        uiNotice.SetActive(false);
    }
}
  • Create Empty로 새로운 오브젝트 만들기(AchiveManager)
        - 작성한 AchiveManager 스크립트 넣어주기
        - Lock Character에 Character 2,3(Lock) 넣어주기
        - Unlock Character에 Character 2,3 넣어주기
  • Canvas에 UI\Image 생성(Notice)
        - Source Image를 Panel로 변경
        - Width = 50 | Height = 27
        - 앵커 클릭 후 Shift + Alt 입력 중 상단 우측 모서리 이미지 선택
        - Pos X = -3 | Pos Y = -20
        - Add Component(Shadow)
  • Notice 안에 Create Empty로 새로운 오브젝트 생성(UnlockPotato)
        - 앵커 클릭 후 Shift + Alt 입력 중 전체 확장 이미지 선택
  • UnlockPotato에 우클릭 후 Ui\Image로 새로운 이미지 생성(Icon)
        - Source Image를 Potato에 맞는 Stand 0 이미지로 설정
        - Set Native Size 클릭
        - 앵커 클릭 후 Shift + Alt 입력 중 왼쪽 중단 이미지 선택
        - Pos X = 1 | Pos Y = 1
  • UnlockPotato에 우클릭 후 Ui\Legacy\Text로 새로운 텍스트 생성(Icon)
        - 앵커 클릭 후 Shift + Alt 입력 중 양옆으로 꽉찬 중단 이미지 선택
        - Left = 20 | Right = 1
        - Font Size = 5, Font = neodgm
        - Text = 감자농부가 다음 전투부터 합류합니다!
  • UnlockPotato를 Ctrl + D로 복사 후 UnlockBean으로 이름 변경
        - Source Image를 Bean에 맞는 Stand 0 이미지로 설정
        - Text = 콩농부가 다음 전투부터 합류합니다!
  • Notice, UnlockPotato, Bean 모두 비활성화
  • AchiveManager 오브젝트에 Notice 넣어주기(드래그&드랍)

(+) 업적 해금 테스트 전에 Edit\Clear All PlayerPrefs 해주기

 

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 

★Slow and Steady★

'Unity 공부 > Unity_2D' 카테고리의 다른 글

[Day 16] 로직 보완  (0) 2023.07.26
[Day 15] 오디오 시스템 구축  (0) 2023.07.25
[Day 13] 플레이 캐릭터 선택  (0) 2023.07.19
[Day 12] 게임 시작과 종료 구현  (0) 2023.07.18
[Day 11] 레벨업 시스템  (0) 2023.07.17