2016년 5월 29일 일요일

UNITY - PlayerPrefs

점수를 저장하거나 스테이지를 저장하거나 하였다가 하는 그런 경우에 유용하게 쓰일수가 있다.

PlayerPref.Set자료형("문자열 키값", 저장할 값);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using UnityEngine;
using System.Collections;
 
public class csTest : MonoBehaviour {
    int currentScore; 
    int highScore; 
 
    string currentPlayerName; 
    string highScorePlayerName; 
 
    void SaveData(){
// 현재 점수를 저장
        PlayerPrefs.SetInt("Score", currentScore); 
// 최고점에 현재점수를 저장
        PlayerPrefs.SetInt("HighScore", currentScore); 
// 현재 이름을 저장
        PlayerPrefs.SetString("HighScoreName", currentPlayerName); 
    }
 
    void GetData(){
// 최고점은 highscore란에 저장된 현재의 점수를 가져온다
        highScore = PlayerPrefs.GetInt("HighScore"); 
// 저장된 현재 이름을 가져온다
        highScorePlayerName = PlayerPrefs.GetString(
"HighScoreName""N/A"); 
    }
}
cs
이런식으로 저장해 뒀다가 나중에 원하는 때에 불러올수가 있다.
SetInt
key에의해 식별되는 환경의 값을 설정합니다.
GetInt
만약 그것이 존재한다면 환경설정 파일에서 key에 해당하는 값을 반환합니다.
SetFloat
key에의해 식별되는 환경의 값을 설정합니다.
GetFloat
만약 그것이 존재한다면 환경설정 파일에서 key에 해당하는 값을 반환합니다.
SetString
key에의해 식별되는 환경의 값을 설정합니다.
GetString
만약 그것이 존재한다면 환경설정 파일에서 key에 해당하는 값을 반환합니다.
HasKey
만약 환경설정에서 key가 존재한다면 true를 반환합니다.
DeleteKey
환경설정으로부터 그것의 값에 해당하고 key를 제거합니다.
DeleteAll
환경설정으로부터 모든 키와 값을 제거합니다. 조심해서 사용하세요.
이렇게 다양한 옵션들이 있다.

댓글 없음:

댓글 쓰기