gPlayerPrefs(플레이어프렙스)



키-벨류 방식으로 유니티안에서 간단하게 저장하고 데이터를 불러올수 있다.

getter을 통해 저장하고 setter을 통해 불러온다. 저장가능 용량은 1MB이다.

간단하게 저장할 때 사용한다.(보안x)


사용법 먼저 알아보자


저장하기

PlayerPrefs.SetInt("score", getcoin);

매개 변수안에 key,value를 넣면 된다. 

Setfloat,Setint,Setstring 3가지가있다. 저장하려는 형식의 맞게 사용하면 된다

"score"라는 키값으로  벨류인 getcoin의 값을 저장하고 있다.

(아이템을 먹으면 1씩 증가하는 getcoin)



불러오기 

score = PlayerPrefs.GetInt("score");

불러 올때는 Getter을 사용하는데 score라는 키값으로 getcoin을 불러오는것이다.



적용하기

void Start() {

score = PlayerPrefs.GetInt("score");

getCoin();

}


void OnApplicationQuit() //종료 콜백함수

{

PlayerPrefs.SetInt("score", getcoin);

}

이런식으로 시작할 때 저장한 값을 불러오고 종료할때 값을 저장했다.


삭제하기

유니티를 꺼도 삭제되지 않는다 그렇기 때문에 키워드를 통해 지워줘야한다.

PlayerPrefs.DeleteAll

PlayerPrefs.DeleteKey("키이름")


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




반응형

+ Recent posts