유니티

[C#] PlayerPrefs

simstealer 2022. 7. 11. 14:31

PlayerPrefs는 어떤 수치를 컴퓨터에 저장하고 나중에 다시 불러오는 매서드를 제공하는 유니티에 내장된 클래스입니다.

 

값을 저장할 때는

PlayerPrefs.SetFloat(string key, float value);

 

값을 불러올 때는

PlayerPrefs.GetFloat(string key);

 

★ 키 값이 다르면 다른 행이 만들어져 저장 됩니다.

a 10
b 10

키 값이 같다면 값은 덮여 씌워집니다.

 

float 외에도 string과 int도 마찬가지로 사용가능합니다.

 

// string

PlayerPrefs.SetString(string key, string value);

PlayerPrefs.GetString(string key);

// int

PlayerPrefs.SetInt(string key, int value);

PlayerPrefs.GetInt(string key);

 

값이 비어 있을 경우에는 기본 값이 들어가 있습니다.

int, float = 0

string = "" 

 

해당 Key안에 값이 있는지 확인해보고 싶을 경우엔 PlayerPrefs.HasKey(string key); 로 확인해 볼 수 있습니다.

값이 존재하면 true, 값이 없다면 false를 반환합니다.