일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
- 개인 프로젝트 - 런앤건
- 2025년
- c++
- 2024년
- 4월
- 골드메탈
- 5월
- 다이나믹 프로그래밍
- 단계별로 풀어보기
- 프로그래머스
- 3월
- 2월
- 7월
- 코딩 기초 트레이닝
- 자료 구조
- 유니티 심화과정
- 1월
- 입문
- 2022년
- 개인 프로젝트
- C/C++
- 2023년
- 수학
- 유니티
- todolist
- 게임 엔진 공부
- 백준
- 기초
- 코딩 테스트
- 6월
- Today
- Total
기록 보관소
[Unity/유니티] 기초-뱀서라이크: 캐릭터 해금 시스템[14+] 본문
개요
유니티 독학을 위해 아래 링크의 골드메탈님의 영상들을 보고 직접 따라 해보면서 진행 상황을 쓰고 배웠던 점을 요약한다.
https://youtube.com/playlist?list=PLO-mt5Iu5TeYI4dbYwWP8JqZMC9iuUIW2
📚유니티 기초 강좌
유니티 게임 개발을 배우고 싶은 분들을 위한 기초 강좌
www.youtube.com
뱀서라이크: 캐릭터 해금 시스템[14+]
0. 시작하기에 앞서 애니메이터 컨트롤러 만들기
- 일단 시작하기에 앞서, 지난 시간에 만들지 않았던 애니메이터 컨트롤러를 Sprites에 있는 스프라이트들을 이용해 애니메이션을 만들고, 해당 애니메이션을 Override 해주면 쉽게 만들 수 있다.
- 애니메이터 컨트롤러 강의 영상은 아래 유튜브 영상을 통해서 보면 된다.
- 아래 골드메탈님 영상에서는 Farmer 2, 3에 대한 제작은 따로 없어서 그냥 따로 직접 해야한다. 그래서 지난 시간에 AC가 2개가 더 없어서 2개만 했었고, 오늘 필요한 것 같아서 따로 추가하게 되었다.
https://youtu.be/vizfd1TeRMI?si=jF8MddRHRwhPq2db
1. 추가 캐릭터 버튼
2. 잠금과 해금
- 해금은 조건 완료시 해당 캐릭터의 버튼을 활성화하고, Lock을 비활성화 하는 식으로 구현.
//ArchiveManager Script
using System; //Enum 사용을 위한 namespace
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ArchiveManager : MonoBehaviour {
public GameObject[] lockCharacter;
public GameObject[] unlockCharacter;
enum Archive { UnlockPotato, UnlockBean }
Archive[] archives;
void Awake() {
archives = (Archive[])Enum.GetValues(typeof(Archive));
if (!PlayerPrefs.HasKey("MyData")) {
Init();
}
}
void Init() {
PlayerPrefs.SetInt("MyData", 1);
foreach (Archive archive in archives) {
PlayerPrefs.SetInt(archive.ToString(), 0);
}
}
void Start() {
UnlockCharacter();
}
void UnlockCharacter() {
for (int index = 0; index < lockCharacter.Length; index++) {
string archiveName = archives[index].ToString();
bool isUnlock = PlayerPrefs.GetInt(archiveName) == 1;
lockCharacter[index].SetActive(!isUnlock);
unlockCharacter[index].SetActive(isUnlock);
}
}
}
- PlayerPrefs : 간단한 저장 기능을 제공하는 유니티 제공 클래스
- 테스트 실행이 끝난 뒤에 다시 변수를 0으로 하고, Clear All PlayerPrefs를 해주었다.
- 뒤늦게 알았는데, Achive를 실수로 Archive로 썼다. 위 캡쳐 이미지들에도 보이듯이 스크립트 파일 이름부터 내용, 오브젝트까지 전부 다 잘못 작성해버렸다... 다음 파트부터는 이 부분들을 다 고친 뒤에 진행했다.
3. 업적 달성 로직
//AchiveManager Script
using System; //Enum 사용을 위한 namespace
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AchiveManager : MonoBehaviour {
public GameObject[] lockCharacter;
public GameObject[] unlockCharacter;
enum Achive { UnlockPotato, UnlockBean }
Achive[] achives;
void Awake() {
achives = (Achive[])Enum.GetValues(typeof(Achive));
if (!PlayerPrefs.HasKey("MyData")) {
Init();
}
}
void Init() {
PlayerPrefs.SetInt("MyData", 1);
foreach (Achive Achive in achives) {
PlayerPrefs.SetInt(Achive.ToString(), 0);
}
}
void Start() {
UnlockCharacter();
}
void UnlockCharacter() {
for (int index = 0; index < lockCharacter.Length; index++) {
string AchiveName = achives[index].ToString();
bool isUnlock = PlayerPrefs.GetInt(AchiveName) == 1;
lockCharacter[index].SetActive(!isUnlock);
unlockCharacter[index].SetActive(isUnlock);
}
}
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 >= 10;
break;
case Achive.UnlockBean:
isAchive = GameManager.instance.gameTime == GameManager.instance.maxGameTime;
break;
}
if (isAchive && PlayerPrefs.GetInt(achive.ToString()) == 0) {
PlayerPrefs.SetInt(achive.ToString(), 1);
}
}
}
4. 해금 알려주기
//AchiveManager Script
using System; //Enum 사용을 위한 namespace
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AchiveManager : MonoBehaviour {
public GameObject[] lockCharacter;
public GameObject[] unlockCharacter;
public GameObject uiNotice;
enum Achive { UnlockPotato, UnlockBean }
Achive[] achives;
WaitForSecondsRealtime wait;
void Awake() {
achives = (Achive[])Enum.GetValues(typeof(Achive));
wait = new WaitForSecondsRealtime(5);
if (!PlayerPrefs.HasKey("MyData")) {
Init();
}
}
void Init() {
PlayerPrefs.SetInt("MyData", 1);
foreach (Achive Achive in achives) {
PlayerPrefs.SetInt(Achive.ToString(), 0);
}
}
void Start() {
UnlockCharacter();
}
void UnlockCharacter() {
for (int index = 0; index < lockCharacter.Length; index++) {
string AchiveName = achives[index].ToString();
bool isUnlock = PlayerPrefs.GetInt(AchiveName) == 1;
lockCharacter[index].SetActive(!isUnlock);
unlockCharacter[index].SetActive(isUnlock);
}
}
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 >= 10;
break;
case Achive.UnlockBean:
isAchive = GameManager.instance.gameTime == GameManager.instance.maxGameTime;
break;
}
if (isAchive && PlayerPrefs.GetInt(achive.ToString()) == 0) {
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;
uiNotice.SetActive(false);
}
}
'유니티 프로젝트 > 뱀서라이크' 카테고리의 다른 글
[Unity/유니티] 기초-뱀서라이크: 로직 보완하기[16] (0) | 2023.08.30 |
---|---|
[Unity/유니티] 기초-뱀서라이크: 편리한 오디오 시스템 구축[15] (0) | 2023.07.28 |
[Unity/유니티] 기초-뱀서라이크: 플레이 캐릭터 선택[14] (0) | 2023.07.21 |
[Unity/유니티] 기초-뱀서라이크: 게임 시작과 종료[13] (0) | 2023.07.18 |
[Unity/유니티] 기초-뱀서라이크: 레벨업 시스템[12] (0) | 2023.07.14 |