(UI)생명 게이지 점수 구현 - PlayerPrefs로 저장 및 불러오기
2023. 8. 29.

GameManger 수정
MonsterController 수정
몬스터 사망시 점수 증가

 

PlayerPrefs를 활용한 스코어 저장

 

PlayerPrefs

https://docs.unity3d.com/kr/530/ScriptReference/PlayerPrefs.html

 

UnityEngine.PlayerPrefs - Unity 스크립팅 API

Stores and accesses player preferences between game sessions.

docs.unity3d.com

 

 

PlayerPrefs은 <Key Value>로 데이터를 저장하는 클래스이다.

int, float, string 타입의 데이터를 저장한다. Key값은 string이며, Key는 Value를 찾기 위한 식별자이다.

 

PlayerPrefs은 데이터를 메모리 상에 저장하여, 이를 하드 드라이브에 저장한다. 메모리 상의 저장은 임시적인 저장이므로, 저장이 이루어지지 않을 수 있다.

DeleteAll() : 모든 데이터를 삭제한다.  이 함수를 사용할 경우 경고 메시지가 출력

DeleteKey(String Key) : Key와 대응하는 값을 삭제

HasKey(String Key) : Key가 존재하는지 확인

Save() : 수정된 모든 preferences를 파일에 저장( 메모리 상에 저장된 데이터를 하드 드라이브에 저장)

SetInt(string Key, int value) :  Key 값으로 int 형 데이터를 저장

SetFloat(string Key, float value) : Key 값으로 float 형 데이터를 저장

SetString(string Key, string value) : Key 값으로 string 형 데이터를 저장

GetInt(string Key) : Key 값으로 저장된  int 형 데이터를 불러옴

GetFloat(string Key) : Key 값으로 저장된 float 형 데이터를 불러옴

GetString(string Key) : Key 값으로 저장된 string 형 데이터를 불러옴

 

간단한 예제
GameManger 코드 수정

PlayerPrefs.SetInt로 점수를 저장해 보았다.

-이전 플레이의 점수가 저장되어 시작하자 마자 100점이 뜬다

 

myoskin