코린이 부트캠프 일상

유니티 간단한 데이터 저장 / PlayerPrefs

게발인개발자 2023. 12. 4. 22:43

유니티로 게임을 만들다 보면 유저의 데이터를 저장해야 할 일이 생긴다.

그중에서도 최고점수 등 저장이 필요한 상황이 나올 때 유니티에서 제공하는 간단한 데이터 저장 방법인

PlayerPrefs를 이용해 볼려고 한다.

 

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

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

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

Save() : 수정된 모든 preferences를 파일에 저장한다.

 

PlayerPrefs은 int, float, string 타입의 데이터를 저장하도록 Set 함수를 제공한다.

 

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

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

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

 

PlayerPrefs은 int, float, string 타입의 저장된 데이터를 불러오도록 Get 함수를 제공한다.

 

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

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

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

 

PlayerPrefs.HasKey("저장데이터명") // 저장된 데이터가 있는지 찾는다
Playerprefs.SetInt("저장데이터명") // 데이터에 int값 할당
PlaterPrefs.GetInt("저장데이터명") // 데이터에 할당한 int값 가져온다.
// int, float, string 다 가능

PlayerPrefs.DeleteAll() // PlayerPrefs에 할당한 데이터 전부 삭제
PlayerPrefs.DeleteKey("저장데이터명") // 해당 이름의 데이터 삭제