Deg-23 [Feat] 세이브 매니저 싱글톤 구현

This commit is contained in:
99jamin 2025-04-21 16:24:47 +09:00
parent f28f659e68
commit 3cf799e039
4 changed files with 32 additions and 9 deletions

BIN
Assets/KJM/KJM.unity (Stored with Git LFS)

Binary file not shown.

View File

@ -3,8 +3,9 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using CI.QuickSave; using CI.QuickSave;
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement;
public class SaveManager : MonoBehaviour public class SaveManager : Singleton<SaveManager>
{ {
private const string SaveFolder = "QuickSave"; private const string SaveFolder = "QuickSave";
private const string SaveFileName = "Save_Main.json"; private const string SaveFileName = "Save_Main.json";
@ -13,6 +14,7 @@ public class SaveManager : MonoBehaviour
private Save mainSave; private Save mainSave;
private Save backupSave; private Save backupSave;
void Start() void Start()
{ {
mainSave = new Save(); mainSave = new Save();
@ -33,7 +35,7 @@ public class SaveManager : MonoBehaviour
public void Save() public void Save()
{ {
if(JsonUtility.ToJson(mainSave) == JsonUtility.ToJson(GMTest.instance.ToSaveData())) //같은 상태를 저장하면 저장되지 않음. 백업 덮어쓰기 방지 if(JsonUtility.ToJson(mainSave) == JsonUtility.ToJson(StatManager.instance.ToSaveData())) //같은 상태는 저장되지 않음. 백업 덮어쓰기 방지.
return; return;
backupSave = LoadMain(); //메인 세이브를 백업 세이브에 로드 backupSave = LoadMain(); //메인 세이브를 백업 세이브에 로드
@ -55,7 +57,7 @@ public class SaveManager : MonoBehaviour
private void UpdateSaveInfo() private void UpdateSaveInfo()
{ {
mainSave = GMTest.instance.ToSaveData(); //스탯을 관리하는 클래스에 선언된 스탯 업데이트 함수를 호출 mainSave = StatManager.instance.ToSaveData(); //스탯을 관리하는 클래스에 선언된 스탯 업데이트 함수를 호출
} }
private void SaveMain() private void SaveMain()
@ -84,4 +86,9 @@ public class SaveManager : MonoBehaviour
.Read<Save>("Backup"); .Read<Save>("Backup");
} }
protected override void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
Save();
Debug.Log("자동저장");
}
} }

View File

@ -2,11 +2,15 @@ using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement;
using Random = UnityEngine.Random; using Random = UnityEngine.Random;
public class GMTest : MonoBehaviour /// <summary>
/// 테스트용 임시 클래스
/// </summary>
public class StatManager : MonoBehaviour
{ {
public static GMTest instance; public static StatManager instance;
public int attackLevel; public int attackLevel;
public int attackSpeedLevel; public int attackSpeedLevel;
@ -25,6 +29,9 @@ public class GMTest : MonoBehaviour
instance = this; instance = this;
} }
/// <summary>
/// 스탯값 변화
/// </summary>
public void ChangeValue() public void ChangeValue()
{ {
float floatValue = Random.Range(0f, 2f); float floatValue = Random.Range(0f, 2f);
@ -49,6 +56,10 @@ public class GMTest : MonoBehaviour
} }
/// <summary>
/// 스탯값 반환
/// </summary>
/// <returns></returns>
public Save ToSaveData() public Save ToSaveData()
{ {
return new Save return new Save
@ -71,4 +82,9 @@ public class GMTest : MonoBehaviour
} }
}; };
} }
public void SceneChange()
{
SceneManager.LoadScene("Main");
}
} }