반응형



PlayerPrefs 


- 데이터를 간단하게 저장할 수 있게 해주는 클래스

- key값과 데이터를 넣어 저장할 수 있고 , 반대로 key값을 넣어 데이터를 꺼낼 수도 있다.


저장 가능한 데이터 타입

int , float , string


1. 데이터 세팅 방법

PlayerPrefs.SetInt("hero_hp",10); // hero_hp 라는 key 값으로 10의 정수형 데이터를 집어 넣음

이런식으로 SetFloat, SetString 으로 float 와 String 의 값을 넣으면 된다.


2. 데이터 얻어오는 방법

PlayerPrefs.GetInt("hero_hp"); // hero_hp key에 대응하는 값을 돌려준다. key 가 없을경우 기본값을 돌려준다. ( 0 )

마찬가지로 GetFloat , GetString 도 해주면 된다.


3. 데이터 삭제

 ㄱ PlayerPrefs.DeleteKey("hero_hp"); // key값을 넣어 해당 key값을 가진 값을 삭제

 ㄴ PlayerPrefs.DeleteAll();                // 모든 데이터를 삭제


4. 데이터의 유무 체크

PlayerPrefs.HasKey("hero_hp"); 

key값이 존재한다면 true를 반환, 없다면 false 반환



5. 저장 방법

PlayerPrefs.Save();를 호출하면 된다.


6. 저장되는 공간

ㄱ Mac일 경우 : 

 ~/Library/Preferences 폴더에 unity.[company name].[product name].plist 이름으로 저장된다.

ㄴ Windows일 경우 :

 HKCU\Software\[company name]\[product name] 레지스트리에 저장된다.


ㄷ Mac , Web일 경우

~/Library/Preferences/Unity/WebPlayerPrefs  바이너리 파일 아래에 저장된다.

Windows , Web일 경우


 %APPDATA%\Unity\WebPlayerPrefs 에 저장된다.

* Web 에서는 1mb의 용량제한이 있고 웹 플레이어 URL당 하나의 파일을 사용할 수 있다.

 제한이 초과될 경우 PlayerPrefsException 을 던진다.



6. 보안 문제

다른 플랫폼은 테스트해보지 못했으나 맥에선 해당 plist를 열어볼 수 있다. ( 수정은 불가능하지만 )

일단 열어볼 수 있다는 것 하나만으로 보안이 그렇게 높은건 같지 않고, 정 사용하려면 key 값과 데이터를 암호화 해서 사용하는게 좋을듯 하다.



PlayerPrefs

class in UnityEngine

Description

게임 세션(session)사이에 플레이어 preference를 저장하고, preference에 접근합니다.

Mac OS X 에서 PlayerPrefs는 ~/Library/Preferences 폴더에 unity.[company name].[product name].plist의 파일이름으로 저장되며,

On Mac OS X PlayerPrefs are stored in ~/Library/Preferences folder, in a file named unity.[company name].[product name].plist, where company and product names are the names set up in Project Settings. Windows 독립 플레이어에서 PlayerPrefs는 @@HKCU
Software
[company name]
[product name]@@ 키 아래의 레지스트리에 저장되며 standalone players.

웹 플레이어에서 PlayerPrefs는 Mac OS X에서 ~/Library/Preferences/Unity/WebPlayerPrefs아래에 HKCU\Software\[company name]\[product name] key, where company and product names are 웹 플레이어 URL 당, 한개의 preference 파일이 존재하며, 파일 크기는 1메가 바이트로 제한됩니다. 파일 크기가 이 제한 값을 초과하는 경우,

On Linux, PlayerPrefs can be found in ~/.config/unity3d/[CompanyName]/[ProductName] again using the company and product names specified in the Project Settings.

On Windows Store Apps, Player Prefs can be found in %userprofile%\AppData\Local\Packages\[ProductPackageId]>\LocalState\playerprefs.dat

On Windows Phone 8, Player Prefs can be found in application's local folder, See Also: Windows.Directory.localFolder

On Android data is stored (persisted) on the device. The data is saved in SharerPreferences. C#/JavaScript, Android Java and Native code can all access the PlayerPrefs data. The PlayerPrefs data is physically stored in /data/data/pkg-name/shared_prefs/pkg-name.xml.

WebPlayer

On Web players, PlayerPrefs are stored in binary files in the following locations:

Mac OS X: ~/Library/Preferences/Unity/WebPlayerPrefs

Windows: %APPDATA%\Unity\WebPlayerPrefs

웹 플레이어에서 PlayerPrefs는 Mac OS X에서 ~/Library/Preferences/Unity/WebPlayerPrefs아래에

There is one preference file per Web player URL and the file size is limited to 1 megabyte. If this limit is exceeded, SetInt, SetFloat and SetString will not store the value and throw a PlayerPrefsException.

Static Functions

DeleteAllpreference에서 모든 key와 값들을 제거합니다. 사용 시 경고가 뜹니다.
DeleteKey키와 대응하는 값을 삭제합니다.
GetFloatPreference 파일에 존재하는 /key/에 대응하는 값을 반환합니다.
GetIntPreference 파일에 존재하는 /key/에 대응하는 값을 반환합니다.
GetStringPreference 파일에 존재하는 /key/에 대응하는 값을 반환합니다.
HasKey키가 존재하는지 확인합니다.
Save수정된 모든 preferences를 디스크에 씁니다.
SetFloat/key/로 식별된 Preference의 값을 설정합니다.
SetInt/key/로 식별된 Preference의 값을 설정합니다.
SetString/key/로 식별된 Preference의 값을 설정합니다.


ref : http://moonausosigi.tistory.com/entry/%EC%A0%95%EB%A6%AC-playerprefs

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


반응형

+ Recent posts