Game Engine/Unity

[Unity] PlayerPrefs 클래스를 활용한 데이터 저장

양양줘 2025. 4. 8. 13:00

 

유니티에서 데이터를 저장하는 방식에는 여러가지가 있다.

 

데이터 저장 방식 용도
ScriptableObject 고정 데이터 구조, 에디터에서 사용
CSV / XML 스프레드시트 기반 데이터
SQLite / Firebase / PlayerPrefs 대규모, 온라인, 클라우드 저장
Addressables / Resources / StreamingAssets 외부 파일, 리소스 로딩
JSON / Binary / PlayerPrefs 세이브 파일, 설정

 

이중 PlayerPrefs는 가장 쉽고 간단하게

데이터를 저장하고 활용할 수 있다.

 

 

 

 

📌 PlayerPrefs (Player Preferences)

Unity에서 가장 간단하게 데이터를 저장하게 해주는 클래스로, 데이터를 암호화 하지 않고 로컬 레지스트리에 저장한다.

보통 간단한 게임이나, 설정관련 항목에만 PlayerPrefs로 데이터를 저장한다.

게임의 중요한 데이터는 DB로 관리해야한다! (데이터 구조, 보안 문제)

 

저장 가능한 데이터 종류

메서드 설명 예시
SetInt(string, int) 정수(int) 저장 GetInt(" HighScore ", 1000)
SetFloat(string, float) 실수(float) 저장 GetInt(" Volume ", 0.7f)
SetString(string, string) 문자열(string) 저장 GetInt(" Username ", "wooj")
GetInt(string) 저장한 정수 불러오기 GetInt("HighScore")
GetFloat(string) 저장한 실수 불러오기 GetFloat("Volume")
GetString(string) 저장한 문자열 불러오기 GetString("Username")
HasKey(string) 키가 저장돼 있는지 확인할 때 사용 HasKey("HighScore")
DeleteKey(string) 특정 키 데이터 삭제 DeleteKey("Volume")
DeleteAll() 모든 PlayerPrefs 데이터 삭제 (주의!) DeleteAll()

 

PlayerPrefs를 이용하여 스코어 저장하기

using UnityEngine;

public class SaveManager : MonoBehaviour
{
    void Start()
    {
        // 1. 최고 점수 저장하기
        int newScore = 1200;
        PlayerPrefs.SetInt("HighScore", newScore);
        PlayerPrefs.Save(); // 저장 강제 반영 (선택적)

        // 2. 최고 점수 불러오기
        int savedScore = PlayerPrefs.GetInt("HighScore", 0); // 기본값 0
        Debug.Log("불러온 최고 점수: " + savedScore);
    }
}

 

 


 

wooj22 - Overview

🎮 Game Programmer. wooj22 has 22 repositories available. Follow their code on GitHub.

github.com

 

 

양우정

 

www.youtube.com