[유니티]Webgl Playerpref 저장 관련

jh Seo·2025년 3월 7일
0

유니티

목록 보기
65/65

개요

webgl빌드에서 Playerpref를 이용해 간단한 저장을 사용 중이였다.
스크립터블 오브젝트를 상속받아

public class SaveLoadSO : ScriptableObject
{
    public virtual void Save()
    {

    }
    public virtual void Load()

    {
    }
    public virtual void Clear()
    {

    }
}

SaveLoadSO란 이름의 저장 관련 SO클래스를 하나 만들고,
저장이 필요한 스크립터블오브젝트는 이 클래스를 상속받아 Save Load를 구현했다.

그리고 SaveAllSO(); 함수에서 모든 SaveLoadSO의 Save함수를 호출하는식으로 저장을 구현했다.

문제점

monobehaviour를 상속받은 savemanager를

public void OnDisable()
{
    SaveAllSO();
}
public void OnDestroy()
{
    SaveAllSO();
}

private void OnApplicationQuit()
{
    SaveAllSO();
}

이런 식으로 구현했더니 webgl 게임 페이지를 종료하거나 뒤로가기 f5를 눌러도
저장이 안되는 현상이 발생했다.

이유

많은 시간을 들여 찾아본 결과 unity discussion에서 유니티 스태프 분께서 대답해준 글을 봤다.

In WebGL you should always execute PlayerPrefs.Save() during gameplay to ensure the data is saved to the database (for example, each time when user confirms the change). You should not expect PlayerPrefs to be saved when user closes the window (i.e. in OnApplicationQuit etc.), because all indexedDB operations are asynchronous, and there is no reliable way to perform any asynchronous operation when user closes the window. Success of such operation depends on browser implementation and timing, so you should always assume that PlayerPrefs are not saved at the moment when the window is closed.

webgl에서는 gameplay도중에 PlayerPrefs.Save() 를 실행하라고 한다.

indexedDB에 playerprefs값이 저장이되는데 이게 비동기로 진행되는 과정이라
비동기 작업이 window가 꺼질때 실행될거란 보장이 없다고한다.
따라서 윈도우 꺼질때 onApplicationQuit같은 함수에서 저장될거란 보장이 없으니
도중에 해야하는것 같다!

Webgl에서 PlayerPrefs 저장 위치

WebGL: On WebGL, PlayerPrefs are stored using the browser's IndexedDB API.

해당 브라우저의 indexedDB API사용해서 저장된다고 한다.
크롬같은 경우는 f12눌러 개발자도구 열고 Application 탭에 indexed DB를 확인하는 칸이 있다.

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.

file size 최대가 1메가바이트라고한다.

이번 프로젝트 저장은 몇 안되는 float값을 저장해서 메가바이트는 커녕 1킬로바이트도 택도없다.

레퍼런스

Unity-Discussion

profile
코딩 창고!

0개의 댓글

관련 채용 정보